feat: Generated endpoint endpoints/fairy-tale.post.py via AI for Anime
This commit is contained in:
parent
ebc3d4cfec
commit
a8abb64f4a
30
alembic/versions/20250418_084234_242ce6b4_update_anime.py
Normal file
30
alembic/versions/20250418_084234_242ce6b4_update_anime.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"""create table for animes
|
||||||
|
Revision ID: 8b3d9c7f1a6b
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-25 15:44:42.603570
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '8b3d9c7f1a6b'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_name = "animes"
|
||||||
|
op.create_table(
|
||||||
|
table_name,
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now())
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_animes_name'), table_name, ['name'], unique=True)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
table_name = "animes"
|
||||||
|
op.drop_index(op.f('ix_animes_name'), table_name=table_name)
|
||||||
|
op.drop_table(table_name)
|
@ -0,0 +1,14 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from typing import List
|
||||||
|
from schemas.anime import AnimeSchema
|
||||||
|
from helpers.anime_helpers import create_animes_in_bulk
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/fairy-tale", status_code=status.HTTP_201_CREATED, response_model=List[AnimeSchema])
|
||||||
|
async def create_animes(anime_names: List[str], db: Session = Depends(get_db)):
|
||||||
|
new_animes = create_animes_in_bulk(db, anime_names)
|
||||||
|
return new_animes
|
66
helpers/anime_helpers.py
Normal file
66
helpers/anime_helpers.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
from typing import List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.anime import Anime
|
||||||
|
from schemas.anime import AnimeCreate, AnimeSchema
|
||||||
|
|
||||||
|
def create_anime(db: Session, anime_data: AnimeCreate) -> Anime:
|
||||||
|
"""
|
||||||
|
Creates a new anime in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
anime_data (AnimeCreate): The data for the anime to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Anime: The newly created anime object.
|
||||||
|
"""
|
||||||
|
db_anime = Anime(**anime_data.dict())
|
||||||
|
db.add(db_anime)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_anime)
|
||||||
|
return db_anime
|
||||||
|
|
||||||
|
def get_animes_by_names(db: Session, anime_names: List[str]) -> List[Anime]:
|
||||||
|
"""
|
||||||
|
Retrieves animes from the database by their names.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
anime_names (List[str]): A list of anime names to search for.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Anime]: A list of anime objects matching the provided names.
|
||||||
|
"""
|
||||||
|
return db.query(Anime).filter(Anime.name.in_(anime_names)).all()
|
||||||
|
|
||||||
|
def create_animes_in_bulk(db: Session, anime_names: List[str]) -> List[Anime]:
|
||||||
|
"""
|
||||||
|
Creates multiple animes in the database from a list of names.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
anime_names (List[str]): A list of anime names to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Anime]: A list of newly created anime objects.
|
||||||
|
"""
|
||||||
|
existing_animes = get_animes_by_names(db, anime_names)
|
||||||
|
existing_names = [anime.name for anime in existing_animes]
|
||||||
|
new_anime_names = [name for name in anime_names if name not in existing_names]
|
||||||
|
new_animes = [Anime(name=name) for name in new_anime_names]
|
||||||
|
db.add_all(new_animes)
|
||||||
|
db.commit()
|
||||||
|
return new_animes + existing_animes
|
||||||
|
|
||||||
|
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]
|
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="Anime name")
|
||||||
|
|
||||||
|
class AnimeCreate(AnimeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AnimeUpdate(AnimeBase):
|
||||||
|
name: Optional[str] = Field(None, description="Anime name")
|
||||||
|
|
||||||
|
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