✨ feat: Add new endpoints/new-one-piece.post.py endpoint for Character 🚀 📦 with updated dependencies
This commit is contained in:
parent
0a543494cf
commit
9de8e7c556
@ -0,0 +1,33 @@
|
|||||||
|
"""create table for characters
|
||||||
|
|
||||||
|
Revision ID: 2f1a3d7b8ec1
|
||||||
|
Revises: 0001
|
||||||
|
Create Date: 2023-05-29 15:44:46.862124
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2f1a3d7b8ec1'
|
||||||
|
down_revision = '0001'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'characters',
|
||||||
|
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||||
|
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||||
|
sa.Column('bounty', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.Integer(), default=func.now()),
|
||||||
|
sa.Column('updated_at', sa.Integer(), default=func.now(), onupdate=func.now()),
|
||||||
|
sa.Index('ix_characters_name', 'name', unique=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('characters')
|
@ -0,0 +1,20 @@
|
|||||||
|
from fastapi import APIRouter, status
|
||||||
|
from schemas.character import CharacterCreate, CharacterSchema
|
||||||
|
from helpers.character_helpers import create_character, get_character_by_name
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/new-one-piece", status_code=status.HTTP_201_CREATED, response_model=CharacterSchema)
|
||||||
|
async def create_new_character(
|
||||||
|
character_data: CharacterCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
existing_character = get_character_by_name(db, character_data.name)
|
||||||
|
if existing_character:
|
||||||
|
return {"detail": f"Character with name '{character_data.name}' already exists."}
|
||||||
|
|
||||||
|
new_character = create_character(db, character_data)
|
||||||
|
return new_character
|
85
helpers/character_helpers.py
Normal file
85
helpers/character_helpers.py
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
from typing import Optional, List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models.character import Character
|
||||||
|
from schemas.character import CharacterCreate, CharacterSchema
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
def get_character_by_id(db: Session, character_id: UUID) -> Optional[Character]:
|
||||||
|
"""
|
||||||
|
Retrieves a character by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
character_id (UUID): The ID of the character to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Character]: The character object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Character).filter(Character.id == character_id).first()
|
||||||
|
|
||||||
|
def get_character_by_name(db: Session, character_name: str) -> Optional[Character]:
|
||||||
|
"""
|
||||||
|
Retrieves a character by its name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
character_name (str): The name of the character to retrieve.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Character]: The character object if found, otherwise None.
|
||||||
|
"""
|
||||||
|
return db.query(Character).filter(Character.name == character_name).first()
|
||||||
|
|
||||||
|
def create_character(db: Session, character_data: CharacterCreate) -> Character:
|
||||||
|
"""
|
||||||
|
Creates a new character in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
character_data (CharacterCreate): The data for the character to create.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Character: The newly created character object.
|
||||||
|
"""
|
||||||
|
existing_character = get_character_by_name(db, character_data.name)
|
||||||
|
if existing_character:
|
||||||
|
raise ValueError(f"Character with name '{character_data.name}' already exists.")
|
||||||
|
|
||||||
|
db_character = Character(**character_data.dict())
|
||||||
|
db.add(db_character)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_character)
|
||||||
|
return db_character
|
||||||
|
|
||||||
|
def get_all_characters(db: Session) -> List[CharacterSchema]:
|
||||||
|
"""
|
||||||
|
Retrieves all characters from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[CharacterSchema]: A list of all character objects.
|
||||||
|
"""
|
||||||
|
characters = db.query(Character).all()
|
||||||
|
return [CharacterSchema.from_orm(character) for character in characters]
|
||||||
|
|
||||||
|
def search_characters(db: Session, name: Optional[str] = None, bounty: Optional[int] = None) -> List[CharacterSchema]:
|
||||||
|
"""
|
||||||
|
Searches for characters based on name and/or bounty.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
name (Optional[str]): The name of the character to search for.
|
||||||
|
bounty (Optional[int]): The bounty of the character to search for.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[CharacterSchema]: A list of character objects matching the search criteria.
|
||||||
|
"""
|
||||||
|
query = db.query(Character)
|
||||||
|
if name:
|
||||||
|
query = query.filter(Character.name.ilike(f"%{name}%"))
|
||||||
|
if bounty:
|
||||||
|
query = query.filter(Character.bounty == bounty)
|
||||||
|
characters = query.all()
|
||||||
|
return [CharacterSchema.from_orm(character) for character in characters]
|
14
models/character.py
Normal file
14
models/character.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from sqlalchemy import Column, String, Integer
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from core.database import Base
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class Character(Base):
|
||||||
|
__tablename__ = "characters"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
bounty = Column(Integer, nullable=False)
|
||||||
|
created_at = Column(Integer, default=func.now())
|
||||||
|
updated_at = Column(Integer, default=func.now(), onupdate=func.now())
|
@ -7,3 +7,6 @@ sqlalchemy>=1.4.0
|
|||||||
python-dotenv>=0.19.0
|
python-dotenv>=0.19.0
|
||||||
bcrypt>=3.2.0
|
bcrypt>=3.2.0
|
||||||
alembic>=1.13.1
|
alembic>=1.13.1
|
||||||
|
jose
|
||||||
|
passlib
|
||||||
|
pydantic
|
||||||
|
22
schemas/character.py
Normal file
22
schemas/character.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
class CharacterBase(BaseModel):
|
||||||
|
name: str = Field(..., description="Character's name")
|
||||||
|
bounty: int = Field(..., description="Character's bounty")
|
||||||
|
|
||||||
|
class CharacterCreate(CharacterBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CharacterUpdate(CharacterBase):
|
||||||
|
name: Optional[str] = Field(None, description="Character's name")
|
||||||
|
bounty: Optional[int] = Field(None, description="Character's bounty")
|
||||||
|
|
||||||
|
class CharacterSchema(CharacterBase):
|
||||||
|
id: UUID
|
||||||
|
created_at: int
|
||||||
|
updated_at: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user