Updated add path for the new table layout
This commit is contained in:
1 parent
be2e0857e4
commit
fbad8a4390
2 files changed
+42
-33
No files matched your search
+41
-33
@@ -158,54 +158,62 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
@user_router.post("/add/anime")
|
@user_router.post("/add/anime")
|
||||||
def add_anime(anime: AnimeCreate, user_id: UserId, db=Depends(get_db)):
|
def add_anime(
|
||||||
logger.info(f"[FastAPI] Received request to track anilist_id={anime.anilist_id}, user_id={user_id}")
|
anime: AnimeCreate,
|
||||||
|
user_id: UserId,
|
||||||
|
db=Depends(get_db),
|
||||||
|
):
|
||||||
|
logger.info("[FastAPI] Received request to track anilist_id=%s, user_id=%s", anime.anilist_id, user_id)
|
||||||
|
|
||||||
|
with db.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT id, active FROM seadex_state WHERE anilist_id = %s",
|
||||||
|
(anime.anilist_id,),
|
||||||
|
)
|
||||||
|
known = cursor.fetchone()
|
||||||
|
|
||||||
|
last_updated = None
|
||||||
|
with SeaDexEntry() as seadex:
|
||||||
|
if known is None:
|
||||||
anime_name = get_anime_name(anime.anilist_id)
|
anime_name = get_anime_name(anime.anilist_id)
|
||||||
logger.debug(f"[FastAPI] Found name={anime_name} for Anilist id={anime.anilist_id}")
|
last_updated = check_for_update(seadex, anime.anilist_id)
|
||||||
|
else:
|
||||||
|
anime_name, active = known
|
||||||
|
if not active:
|
||||||
|
last_updated = check_for_update(seadex, anime.anilist_id)
|
||||||
|
|
||||||
with db.cursor() as cursor:
|
with db.cursor() as cursor:
|
||||||
try:
|
try:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT id FROM seadex_state WHERE anilist_id = %s AND user_id = %s",
|
"INSERT INTO seadex_state (anilist_id, anime_name, last_updated) "
|
||||||
(anime.anilist_id, user_id),
|
"VALUES (%s, %s, %s) "
|
||||||
|
"ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), active = 1, "
|
||||||
|
"last_updated = COALESCE(VALUES(last_updated), last_updated)",
|
||||||
|
(anime.anilist_id, anime_name, last_updated),
|
||||||
)
|
)
|
||||||
|
seadex_state_id = cursor.lastrowid
|
||||||
if cursor.fetchone():
|
|
||||||
logger.debug("[FastAPI] Found existing entry, updating name and setting to active")
|
|
||||||
cursor.execute(
|
|
||||||
"UPDATE seadex_state SET anime_name = %s, active = 1 "
|
|
||||||
"WHERE anilist_id = %s AND user_id=%s",
|
|
||||||
(anime_name, anime.anilist_id, user_id),
|
|
||||||
)
|
|
||||||
db.commit()
|
|
||||||
return {
|
|
||||||
"status": "ok",
|
|
||||||
"detail": f"Updated existing entry name={anime_name}, "
|
|
||||||
f"Anilist ID={anime.anilist_id}",
|
|
||||||
}
|
|
||||||
|
|
||||||
with SeaDexEntry() as seadex:
|
|
||||||
current = check_for_update(seadex, anime.anilist_id)
|
|
||||||
logger.debug(f"[FastAPI] Inserting new anime with last_updated={current}")
|
|
||||||
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"INSERT INTO seadex_state (anilist_id, anime_name, last_updated, user_id) "
|
"INSERT INTO seadex_state_subscriptions (user_id, seadex_state_id, custom_name) "
|
||||||
"VALUES (%s, %s, %s, %s)",
|
"VALUES (%s, %s, %s) "
|
||||||
(anime.anilist_id, anime_name, current, user_id),
|
"ON DUPLICATE KEY UPDATE active = 1, custom_name = VALUES(custom_name)",
|
||||||
|
(user_id, seadex_state_id, anime.custom_name or None),
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
return {
|
|
||||||
"status": "ok",
|
|
||||||
"detail": f"Created new entry name={anime_name}, "
|
|
||||||
f"Anilist ID={anime.anilist_id}",
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
logger.exception(f"[FastAPI] Failed for anilist_id={anime.anilist_id}, name={anime_name}, user_id={user_id}")
|
logger.exception(
|
||||||
|
"[FastAPI] Failed for anilist_id=%s, name=%s, user_id=%s",
|
||||||
|
anime.anilist_id, anime_name, user_id,
|
||||||
|
)
|
||||||
raise HTTPException(status_code=500) from e
|
raise HTTPException(status_code=500) from e
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"[FastAPI] Tracking anilist_id=%s name=%r for user_id=%s (state_id=%s)",
|
||||||
|
anime.anilist_id, anime_name, user_id, seadex_state_id,
|
||||||
|
)
|
||||||
|
return {"status": "ok", "detail": f"Tracking name={anime.custom_name or anime_name}, Anilist ID={anime.anilist_id}"}
|
||||||
|
|
||||||
@user_router.post("/delete/anime")
|
@user_router.post("/delete/anime")
|
||||||
def remove_anime(anime: AnimeCreate, user_id: UserId, db=Depends(get_db)):
|
def remove_anime(anime: AnimeCreate, user_id: UserId, db=Depends(get_db)):
|
||||||
logger.info(f"[FastAPI] Received request to deactivate tracking for anilist_id={anime.anilist_id}, user_id={user_id}")
|
logger.info(f"[FastAPI] Received request to deactivate tracking for anilist_id={anime.anilist_id}, user_id={user_id}")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from pydantic import BaseModel, Field, computed_field
|
|||||||
|
|
||||||
class AnimeCreate(BaseModel):
|
class AnimeCreate(BaseModel):
|
||||||
anilist_id: int = Field(gt=0)
|
anilist_id: int = Field(gt=0)
|
||||||
|
custom_name: str | None = Field(default=None, max_length=255)
|
||||||
|
|
||||||
class AnimeEntry(BaseModel):
|
class AnimeEntry(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
|
|||||||
Reference in new issue
Block a user