diff --git a/alembic/versions/20250427_195240_383be846_update_bounty_character.py b/alembic/versions/20250427_195240_383be846_update_bounty_character.py new file mode 100644 index 0000000..6470867 --- /dev/null +++ b/alembic/versions/20250427_195240_383be846_update_bounty_character.py @@ -0,0 +1,33 @@ +"""create table for bounty_characters + +Revision ID: 2b4c5d6e7f8a +Revises: 0001 +Create Date: 2023-05-23 15:52:24.890279 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import func +import uuid + +# revision identifiers, used by Alembic. +revision = '2b4c5d6e7f8a' +down_revision = '0001' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'bounty_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.DateTime(), server_default=func.now()), + sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()), + sa.Index('ix_bounty_characters_name', 'name', unique=True) + ) + + +def downgrade(): + op.drop_table('bounty_characters') \ No newline at end of file diff --git a/endpoints/one-piece.post.py b/endpoints/one-piece.post.py index e69de29..335b6f7 100644 --- a/endpoints/one-piece.post.py +++ b/endpoints/one-piece.post.py @@ -0,0 +1,15 @@ +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from core.database import get_db +from schemas.bounty_character import BountyCharacterCreate, BountyCharacterSchema +from helpers.bounty_character_helpers import create_bounty_character + +router = APIRouter() + +@router.post("/one-piece", status_code=201, response_model=BountyCharacterSchema) +async def create_bounty_character_endpoint( + character_data: BountyCharacterCreate, + db: Session = Depends(get_db) +): + new_character = create_bounty_character(db, character_data) + return new_character \ No newline at end of file diff --git a/helpers/bounty_character_helpers.py b/helpers/bounty_character_helpers.py new file mode 100644 index 0000000..d55d98e --- /dev/null +++ b/helpers/bounty_character_helpers.py @@ -0,0 +1,88 @@ +from typing import Optional, List +from uuid import UUID +from sqlalchemy.orm import Session +from models.bounty_character import BountyCharacter +from schemas.bounty_character import BountyCharacterCreate, BountyCharacterSchema + +def get_bounty_character_by_id(db: Session, character_id: UUID) -> Optional[BountyCharacter]: + """ + Retrieves a single bounty character by its ID. + + Args: + db (Session): The database session. + character_id (UUID): The ID of the bounty character to retrieve. + + Returns: + Optional[BountyCharacter]: The bounty character object if found, otherwise None. + """ + return db.query(BountyCharacter).filter(BountyCharacter.id == character_id).first() + +def get_all_bounty_characters(db: Session) -> List[BountyCharacterSchema]: + """ + Retrieves all bounty characters from the database. + + Args: + db (Session): The database session. + + Returns: + List[BountyCharacterSchema]: A list of all bounty character objects. + """ + characters = db.query(BountyCharacter).all() + return [BountyCharacterSchema.from_orm(character) for character in characters] + +def create_bounty_character(db: Session, character_data: BountyCharacterCreate) -> BountyCharacter: + """ + Creates a new bounty character in the database. + + Args: + db (Session): The database session. + character_data (BountyCharacterCreate): The data for the bounty character to create. + + Returns: + BountyCharacter: The newly created bounty character object. + """ + db_character = BountyCharacter(**character_data.dict()) + db.add(db_character) + db.commit() + db.refresh(db_character) + return db_character + +def update_bounty_character(db: Session, character_id: UUID, character_data: BountyCharacterCreate) -> BountyCharacter: + """ + Updates an existing bounty character in the database. + + Args: + db (Session): The database session. + character_id (UUID): The ID of the bounty character to update. + character_data (BountyCharacterCreate): The updated data for the bounty character. + + Returns: + BountyCharacter: The updated bounty character object. + """ + db_character = get_bounty_character_by_id(db, character_id) + if not db_character: + raise ValueError(f"Bounty character with ID {character_id} not found") + update_data = character_data.dict(exclude_unset=True) + for field, value in update_data.items(): + setattr(db_character, field, value) + db.commit() + db.refresh(db_character) + return db_character + +def delete_bounty_character(db: Session, character_id: UUID) -> bool: + """ + Deletes a bounty character from the database. + + Args: + db (Session): The database session. + character_id (UUID): The ID of the bounty character to delete. + + Returns: + bool: True if the bounty character was successfully deleted, False otherwise. + """ + db_character = get_bounty_character_by_id(db, character_id) + if not db_character: + return False + db.delete(db_character) + db.commit() + return True \ No newline at end of file diff --git a/models/bounty_character.py b/models/bounty_character.py new file mode 100644 index 0000000..af4828b --- /dev/null +++ b/models/bounty_character.py @@ -0,0 +1,14 @@ +from sqlalchemy import Column, String, Integer, DateTime +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.sql import func +from core.database import Base +import uuid + +class BountyCharacter(Base): + __tablename__ = "bounty_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(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 596e6f3..db12c92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,6 @@ sqlalchemy>=1.4.0 python-dotenv>=0.19.0 bcrypt>=3.2.0 alembic>=1.13.1 +jose +passlib +pydantic diff --git a/schemas/bounty_character.py b/schemas/bounty_character.py new file mode 100644 index 0000000..fe3e213 --- /dev/null +++ b/schemas/bounty_character.py @@ -0,0 +1,27 @@ +from pydantic import BaseModel, Field +from typing import Optional +from datetime import datetime +from uuid import UUID + +# Base schema for BountyCharacter +class BountyCharacterBase(BaseModel): + name: str = Field(..., description="Name of the bounty character") + bounty: int = Field(..., description="Bounty amount on the character's head") + +# Schema for creating a new BountyCharacter +class BountyCharacterCreate(BountyCharacterBase): + pass + +# Schema for updating an existing BountyCharacter +class BountyCharacterUpdate(BountyCharacterBase): + name: Optional[str] = Field(None, description="Name of the bounty character") + bounty: Optional[int] = Field(None, description="Bounty amount on the character's head") + +# Schema for representing a BountyCharacter in responses +class BountyCharacterSchema(BountyCharacterBase): + id: UUID + created_at: datetime + updated_at: datetime + + class Config: + orm_mode = True \ No newline at end of file