Added listing active and inactive anime
This commit is contained in:
1 parent
938067560d
commit
5d665d3310
1 file changed
+40
-1
+40
-1
@@ -5,7 +5,7 @@ from datetime import UTC, datetime
|
|||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field, computed_field
|
||||||
from seadex import EntryNotFoundError, SeaDexEntry
|
from seadex import EntryNotFoundError, SeaDexEntry
|
||||||
|
|
||||||
from anilist_query import AniListError, AnimeNotFoundError, MissingAnimeName, get_anime_name
|
from anilist_query import AniListError, AnimeNotFoundError, MissingAnimeName, get_anime_name
|
||||||
@@ -25,6 +25,22 @@ logger = setup_logger(__name__)
|
|||||||
class AnimeCreate(BaseModel):
|
class AnimeCreate(BaseModel):
|
||||||
anilist_id: int = Field(gt=0)
|
anilist_id: int = Field(gt=0)
|
||||||
|
|
||||||
|
class AnimeEntry(BaseModel):
|
||||||
|
name: str
|
||||||
|
last_updated: str | None = None
|
||||||
|
|
||||||
|
class Counts(BaseModel):
|
||||||
|
active: int
|
||||||
|
inactive: int
|
||||||
|
|
||||||
|
class AnimeList(BaseModel):
|
||||||
|
active: list[AnimeEntry] = Field(default_factory=list)
|
||||||
|
inactive: list[AnimeEntry] = Field(default_factory=list)
|
||||||
|
|
||||||
|
@computed_field
|
||||||
|
@property
|
||||||
|
def counts(self) -> Counts:
|
||||||
|
return Counts(active=len(self.active),inactive=len(self.inactive))
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
@@ -233,6 +249,29 @@ def remove_anime(anime: AnimeCreate, db=Depends(get_db)):
|
|||||||
logger.debug(f"[FastAPI] Tracking was already deactivated for anilist_id={anime.anilist_id}")
|
logger.debug(f"[FastAPI] Tracking was already deactivated for anilist_id={anime.anilist_id}")
|
||||||
return {"status": "ok", "detail": f"Entry {anime.anilist_id} was already inactive"}
|
return {"status": "ok", "detail": f"Entry {anime.anilist_id} was already inactive"}
|
||||||
|
|
||||||
|
@api.get("/list/anime", response_model=AnimeList)
|
||||||
|
def list_active_and_inactive_anime(db=Depends(get_db)):
|
||||||
|
logger.info(f"[FastAPI] Received request to list anime")
|
||||||
|
with db.cursor() as cursor:
|
||||||
|
try:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT anime_name, active, last_updated " \
|
||||||
|
"FROM seadex_state ORDER BY anime_name"
|
||||||
|
)
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"[FastAPI] Failed to list anime")
|
||||||
|
raise HTTPException(status_code=500) from e
|
||||||
|
|
||||||
|
anime_list=AnimeList ()
|
||||||
|
for name, is_active, last_updated in rows:
|
||||||
|
entry = AnimeEntry(
|
||||||
|
name=name,
|
||||||
|
last_updated=None if last_updated is None else str(last_updated),
|
||||||
|
)
|
||||||
|
(anime_list.active if is_active else anime_list.inactive).append(entry)
|
||||||
|
return anime_list
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def start_servers():
|
async def start_servers():
|
||||||
|
|||||||
Reference in new issue
Block a user