88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
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 |