feat: Generated endpoint endpoints/anime.post.py via AI for Anime
This commit is contained in:
parent
368aa9d944
commit
335dea0562
30
alembic/versions/20250417_072248_ea3bfabb_update_anime.py
Normal file
30
alembic/versions/20250417_072248_ea3bfabb_update_anime.py
Normal file
@ -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')
|
@ -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
|
53
helpers/anime_helpers.py
Normal file
53
helpers/anime_helpers.py
Normal file
@ -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.")
|
13
models/anime.py
Normal file
13
models/anime.py
Normal file
@ -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())
|
21
schemas/anime.py
Normal file
21
schemas/anime.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user