generated from admin/template
podex-api completed with swagger & docker compose
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy import or_, cast, String
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import models, schemas
|
||||
from ..database import get_db
|
||||
from ..errors import AppError
|
||||
|
||||
router = APIRouter(prefix="/api/v1/pokemons", tags=["pokemons"])
|
||||
|
||||
|
||||
@router.get("", response_model=schemas.PokemonListResponse)
|
||||
def list_pokemons(
|
||||
search: Optional[str] = Query(default=None),
|
||||
sort_by: str = Query(default="id"),
|
||||
order: str = Query(default="asc"),
|
||||
limit: int = Query(default=20),
|
||||
offset: int = Query(default=0),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
errors = []
|
||||
if sort_by not in ("id", "name"):
|
||||
errors.append(
|
||||
{
|
||||
"field": "sort_by",
|
||||
"message": "sort_by must be one of: 'id', 'name'",
|
||||
"value": sort_by,
|
||||
}
|
||||
)
|
||||
if order not in ("asc", "desc"):
|
||||
errors.append(
|
||||
{
|
||||
"field": "order",
|
||||
"message": "order must be one of: 'asc', 'desc'",
|
||||
"value": order,
|
||||
}
|
||||
)
|
||||
if limit < 1 or limit > 100:
|
||||
errors.append(
|
||||
{
|
||||
"field": "limit",
|
||||
"message": "limit must not be greater than 100",
|
||||
"value": limit,
|
||||
}
|
||||
)
|
||||
if offset < 0:
|
||||
errors.append(
|
||||
{
|
||||
"field": "offset",
|
||||
"message": "offset must be greater than or equal to 0",
|
||||
"value": offset,
|
||||
}
|
||||
)
|
||||
if errors:
|
||||
raise AppError(
|
||||
400, "VALIDATION_FAILED", "Validation failed for query parameters", errors
|
||||
)
|
||||
|
||||
query = db.query(models.Pokemon)
|
||||
|
||||
if search:
|
||||
term = f"%{search.strip()}%"
|
||||
query = query.filter(
|
||||
or_(
|
||||
models.Pokemon.name.ilike(term),
|
||||
cast(models.Pokemon.id, String).ilike(term),
|
||||
)
|
||||
)
|
||||
|
||||
column = models.Pokemon.id if sort_by == "id" else models.Pokemon.name
|
||||
query = query.order_by(column.desc() if order == "desc" else column.asc())
|
||||
|
||||
total = query.count()
|
||||
rows = query.offset(offset).limit(limit).all()
|
||||
|
||||
return schemas.PokemonListResponse(
|
||||
total=total,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
data=[
|
||||
schemas.PokemonListItem(
|
||||
id=r.id, name=r.name, image_url=r.image_url
|
||||
)
|
||||
for r in rows
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{id_or_name}", response_model=schemas.PokemonDetailResponse)
|
||||
def get_pokemon(id_or_name: str, db: Session = Depends(get_db)):
|
||||
pokemon = None
|
||||
|
||||
if id_or_name.isdigit():
|
||||
pokemon = db.query(models.Pokemon).filter(models.Pokemon.id == int(id_or_name)).first()
|
||||
|
||||
if pokemon is None:
|
||||
pokemon = (
|
||||
db.query(models.Pokemon)
|
||||
.filter(models.Pokemon.name.ilike(id_or_name))
|
||||
.first()
|
||||
)
|
||||
|
||||
if pokemon is None:
|
||||
raise AppError(
|
||||
404,
|
||||
"POKEMON_NOT_FOUND",
|
||||
f"Pokemon with identifier '{id_or_name}' was not found",
|
||||
)
|
||||
|
||||
type_rows = (
|
||||
db.query(models.Type)
|
||||
.join(models.PokemonType, models.PokemonType.type_id == models.Type.id)
|
||||
.filter(models.PokemonType.pokemon_id == pokemon.id)
|
||||
.all()
|
||||
)
|
||||
|
||||
types = [
|
||||
schemas.TypeItem(
|
||||
id=t.id,
|
||||
code=(t.name or "").upper() if t.name else None,
|
||||
name=t.name,
|
||||
color_hex=t.color_hex,
|
||||
)
|
||||
for t in type_rows
|
||||
]
|
||||
|
||||
color_hex = type_rows[0].color_hex if type_rows else None
|
||||
|
||||
stats_row = (
|
||||
db.query(models.PokemonStat)
|
||||
.filter(models.PokemonStat.pokemon_id == pokemon.id)
|
||||
.first()
|
||||
)
|
||||
|
||||
stats = schemas.StatsBlock(
|
||||
hp=stats_row.hp if stats_row else None,
|
||||
atk=stats_row.atk if stats_row else None,
|
||||
def_=stats_row.def_ if stats_row else None,
|
||||
satk=stats_row.satk if stats_row else None,
|
||||
sdef=stats_row.sdef if stats_row else None,
|
||||
spd=stats_row.spd if stats_row else None,
|
||||
)
|
||||
|
||||
move_names = [
|
||||
m.name
|
||||
for m in (
|
||||
db.query(models.Move)
|
||||
.join(models.PokemonMove, models.PokemonMove.move_id == models.Move.id)
|
||||
.filter(models.PokemonMove.pokemon_id == pokemon.id)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
|
||||
about = schemas.AboutBlock(
|
||||
weight_kg=float(pokemon.weight_kg) if pokemon.weight_kg is not None else None,
|
||||
height_m=float(pokemon.height_m) if pokemon.height_m is not None else None,
|
||||
moves=move_names,
|
||||
description=pokemon.description,
|
||||
)
|
||||
|
||||
prev_id = (
|
||||
db.query(models.Pokemon.id)
|
||||
.filter(models.Pokemon.id < pokemon.id)
|
||||
.order_by(models.Pokemon.id.desc())
|
||||
.limit(1)
|
||||
.scalar()
|
||||
)
|
||||
next_id = (
|
||||
db.query(models.Pokemon.id)
|
||||
.filter(models.Pokemon.id > pokemon.id)
|
||||
.order_by(models.Pokemon.id.asc())
|
||||
.limit(1)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
return schemas.PokemonDetailResponse(
|
||||
id=pokemon.id,
|
||||
name=pokemon.name,
|
||||
image_url=pokemon.image_url,
|
||||
color_hex=color_hex,
|
||||
types=types,
|
||||
about=about,
|
||||
stats=stats,
|
||||
navigation=schemas.NavigationBlock(prev_id=prev_id, next_id=next_id),
|
||||
)
|
||||
Reference in New Issue
Block a user