|
| 1 | +from typing import Annotated |
| 2 | + |
| 3 | +from api.deps import SessionDep, get_db_session |
| 4 | +from excepts import DatabaseEntryNotFound, get_error_content |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, Query, status |
| 6 | +from models import Hero, HeroCreate, HeroPublic, HeroUpdate |
| 7 | +from sqlmodel import select |
| 8 | +from utils.log import get_logger |
| 9 | + |
| 10 | +logger = get_logger() |
| 11 | +router = APIRouter() |
| 12 | + |
| 13 | + |
| 14 | +@router.post( |
| 15 | + path="/", |
| 16 | + summary="Create a new hero", |
| 17 | + status_code=status.HTTP_200_OK, |
| 18 | + response_description="Returns the created hero", |
| 19 | + response_model=HeroPublic, |
| 20 | + dependencies=[Depends(get_db_session)], |
| 21 | +) |
| 22 | +def create_hero(hero: HeroCreate, session: SessionDep): |
| 23 | + db_hero = Hero.model_validate(hero) |
| 24 | + session.add(db_hero) |
| 25 | + session.commit() |
| 26 | + session.refresh(db_hero) |
| 27 | + return db_hero |
| 28 | + |
| 29 | + |
| 30 | +@router.get( |
| 31 | + path="/", |
| 32 | + summary="Retrieve a list of heroes", |
| 33 | + status_code=status.HTTP_200_OK, |
| 34 | + response_description="Returns a list of heroes", |
| 35 | + response_model=list[HeroPublic], |
| 36 | + dependencies=[Depends(get_db_session)], |
| 37 | +) |
| 38 | +def read_heroes( |
| 39 | + session: SessionDep, |
| 40 | + offset: int = 0, |
| 41 | + limit: Annotated[int, Query(le=100)] = 100, |
| 42 | +) -> list[Hero]: |
| 43 | + heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() |
| 44 | + return list(heroes) |
| 45 | + |
| 46 | + |
| 47 | +@router.get( |
| 48 | + path="/{hero_id}", |
| 49 | + summary="Retrieve a hero by ID", |
| 50 | + status_code=status.HTTP_200_OK, |
| 51 | + response_description="Returns the hero with the specified ID", |
| 52 | + response_model=HeroPublic, |
| 53 | + dependencies=[Depends(get_db_session)], |
| 54 | +) |
| 55 | +def read_hero(hero_id: int, session: SessionDep): |
| 56 | + try: |
| 57 | + hero = session.get(Hero, hero_id) |
| 58 | + if not hero: |
| 59 | + raise DatabaseEntryNotFound(f"Hero with ID {hero_id} not found") |
| 60 | + return hero |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + error = get_error_content(e) |
| 64 | + error_message = error.message |
| 65 | + |
| 66 | + logger.error( |
| 67 | + error_message, |
| 68 | + exc_info=True, |
| 69 | + stack_info=True, |
| 70 | + ) |
| 71 | + |
| 72 | + raise HTTPException( |
| 73 | + status_code=error.http_status_code, |
| 74 | + detail=error_message, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@router.patch( |
| 79 | + path="/{hero_id}", |
| 80 | + summary="Update a hero by ID", |
| 81 | + status_code=status.HTTP_200_OK, |
| 82 | + response_description="Returns the updated hero", |
| 83 | + response_model=HeroPublic, |
| 84 | + dependencies=[Depends(get_db_session)], |
| 85 | +) |
| 86 | +def update_hero(hero_id: int, hero: HeroUpdate, session: SessionDep): |
| 87 | + try: |
| 88 | + hero_db = session.get(Hero, hero_id) |
| 89 | + if not hero_db: |
| 90 | + raise DatabaseEntryNotFound(f"Hero with ID {hero_id} not found") |
| 91 | + hero_data = hero.model_dump(exclude_unset=True) |
| 92 | + hero_db.sqlmodel_update(hero_data) |
| 93 | + session.add(hero_db) |
| 94 | + session.commit() |
| 95 | + session.refresh(hero_db) |
| 96 | + return hero_db |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + error = get_error_content(e) |
| 100 | + error_message = error.message |
| 101 | + |
| 102 | + logger.error( |
| 103 | + error_message, |
| 104 | + exc_info=True, |
| 105 | + stack_info=True, |
| 106 | + ) |
| 107 | + |
| 108 | + raise HTTPException( |
| 109 | + status_code=error.http_status_code, |
| 110 | + detail=error_message, |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@router.delete( |
| 115 | + path="/{hero_id}", |
| 116 | + summary="Delete a hero by ID", |
| 117 | + status_code=status.HTTP_200_OK, |
| 118 | + response_description="Indicates whether the hero was successfully deleted", |
| 119 | + dependencies=[Depends(get_db_session)], |
| 120 | +) |
| 121 | +def delete_hero(hero_id: int, session: SessionDep): |
| 122 | + try: |
| 123 | + hero = session.get(Hero, hero_id) |
| 124 | + if not hero: |
| 125 | + raise DatabaseEntryNotFound("Hero not found") |
| 126 | + session.delete(hero) |
| 127 | + session.commit() |
| 128 | + return {"ok": True} |
| 129 | + except Exception as e: |
| 130 | + error = get_error_content(e) |
| 131 | + error_message = error.message |
| 132 | + |
| 133 | + logger.error( |
| 134 | + error_message, |
| 135 | + exc_info=True, |
| 136 | + stack_info=True, |
| 137 | + ) |
| 138 | + |
| 139 | + raise HTTPException( |
| 140 | + status_code=error.http_status_code, |
| 141 | + detail=error_message, |
| 142 | + ) |
0 commit comments