diff --git a/alembic/versions/20250417_072248_ea3bfabb_update_anime.py b/alembic/versions/20250417_072248_ea3bfabb_update_anime.py new file mode 100644 index 0000000..5992138 --- /dev/null +++ b/alembic/versions/20250417_072248_ea3bfabb_update_anime.py @@ -0,0 +1,30 @@ +"""create table for animes +Revision ID: 9a3c27d5ff4b +Revises: 0001 +Create Date: 2023-05-25 16:32:10.868973 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func + +# revision identifiers, used by Alembic. +revision = '9a3c27d5ff4b' +down_revision = '0001' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'animes', + sa.Column('id', sa.String(36), primary_key=True, server_default=sa.text("(uuid())")), + sa.Column('name', sa.String(), nullable=False, unique=True), + sa.Column('created_at', sa.DateTime(), server_default=func.now(), nullable=False), + sa.Column('updated_at', sa.DateTime(), server_default=func.now(), nullable=False, onupdate=func.now()), + sa.Index('ix_animes_name', 'name', unique=True) + ) + + +def downgrade(): + op.drop_table('animes') \ No newline at end of file diff --git a/endpoints/anime.post.py b/endpoints/anime.post.py index e69de29..28f5087 100644 --- a/endpoints/anime.post.py +++ b/endpoints/anime.post.py @@ -0,0 +1,16 @@ +from fastapi import APIRouter, status +from schemas.anime import AnimeCreate, AnimeSchema +from helpers.anime_helpers import create_anime +from sqlalchemy.orm import Session +from fastapi import Depends +from core.database import get_db + +router = APIRouter() + +@router.post("/anime", status_code=status.HTTP_201_CREATED, response_model=AnimeSchema) +async def create_new_anime( + anime_data: AnimeCreate, + db: Session = Depends(get_db) +): + new_anime = create_anime(db=db, anime_data=anime_data) + return new_anime \ No newline at end of file diff --git a/helpers/anime_helpers.py b/helpers/anime_helpers.py new file mode 100644 index 0000000..5b1877b --- /dev/null +++ b/helpers/anime_helpers.py @@ -0,0 +1,53 @@ +from typing import List, Optional +from sqlalchemy.orm import Session +from sqlalchemy.exc import IntegrityError +from models.anime import Anime +from schemas.anime import AnimeCreate, AnimeSchema + +def get_all_animes(db: Session) -> List[AnimeSchema]: + """ + Retrieves all animes from the database. + + Args: + db (Session): The database session. + + Returns: + List[AnimeSchema]: A list of all anime objects. + """ + animes = db.query(Anime).all() + return [AnimeSchema.from_orm(anime) for anime in animes] + +def get_anime_by_name(db: Session, name: str) -> Optional[AnimeSchema]: + """ + Retrieves an anime by its name. + + Args: + db (Session): The database session. + name (str): The name of the anime to retrieve. + + Returns: + Optional[AnimeSchema]: The anime object if found, otherwise None. + """ + anime = db.query(Anime).filter(Anime.name == name).first() + return AnimeSchema.from_orm(anime) if anime else None + +def create_anime(db: Session, anime_data: AnimeCreate) -> AnimeSchema: + """ + Creates a new anime in the database. + + Args: + db (Session): The database session. + anime_data (AnimeCreate): The data for the anime to create. + + Returns: + AnimeSchema: The newly created anime object. + """ + try: + db_anime = Anime(**anime_data.dict()) + db.add(db_anime) + db.commit() + db.refresh(db_anime) + return AnimeSchema.from_orm(db_anime) + except IntegrityError: + db.rollback() + raise ValueError(f"Anime with name '{anime_data.name}' already exists.") \ No newline at end of file diff --git a/models/anime.py b/models/anime.py new file mode 100644 index 0000000..71d17c1 --- /dev/null +++ b/models/anime.py @@ -0,0 +1,13 @@ +from sqlalchemy import Column, String, DateTime +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class Anime(Base): + __tablename__ = "animes" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String, nullable=False, unique=True, index=True) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/schemas/anime.py b/schemas/anime.py new file mode 100644 index 0000000..3b0db52 --- /dev/null +++ b/schemas/anime.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +import uuid + +class AnimeBase(BaseModel): + name: str = Field(..., description="Name of the anime") + +class AnimeCreate(AnimeBase): + pass + +class AnimeUpdate(AnimeBase): + name: Optional[str] = Field(None, description="Name of the anime") + +class AnimeSchema(AnimeBase): + id: uuid.UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file