Updated delete path for the new table layout
This commit is contained in:
1 parent
fbad8a4390
commit
84da6184fd
2 files changed
+53
-26
No files matched your search
+50
-26
@@ -18,7 +18,7 @@ from db_module import (
|
||||
)
|
||||
from handlers import register_handlers
|
||||
from metrics_server import metrics_api
|
||||
from models import AnimeCreate, AnimeEntry, AnimeList
|
||||
from models import AnimeCreate, AnimeEntry, AnimeList, AnimeRemove
|
||||
from secret_manager import cleanup_secret_files
|
||||
from send_notifications import NotificationClient, NotificationError, NotificationUnavailableError
|
||||
from simple_logger_handler import LOG_LEVEL, setup_logger
|
||||
@@ -104,7 +104,7 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
||||
logger.info(f"[FastAPI] Anime: {anime_name} updated, Anilist ID: {anilist_id}")
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"SELECT user_id, anime_name as custom_name "
|
||||
"SELECT user_id, custom_name "
|
||||
"FROM seadex_state_subscriptions "
|
||||
"WHERE seadex_state_id = %s AND active = 1",
|
||||
(seadex_state_id,),
|
||||
@@ -215,39 +215,63 @@ def add_anime(
|
||||
return {"status": "ok", "detail": f"Tracking name={anime.custom_name or anime_name}, Anilist ID={anime.anilist_id}"}
|
||||
|
||||
@user_router.post("/delete/anime")
|
||||
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}")
|
||||
def remove_anime(
|
||||
anime: AnimeRemove,
|
||||
user_id: UserId,
|
||||
db=Depends(get_db)
|
||||
):
|
||||
logger.info(
|
||||
"[FastAPI] Received request to deactivate anilist_id=%s for user_id=%s",
|
||||
anime.anilist_id, user_id,
|
||||
)
|
||||
with db.cursor() as cursor:
|
||||
try:
|
||||
cursor.execute(
|
||||
"SELECT active FROM seadex_state WHERE anilist_id = %s AND user_id = %s",
|
||||
(anime.anilist_id, user_id),
|
||||
"SELECT id FROM seadex_state "
|
||||
"WHERE anilist_id = %s FOR UPDATE",
|
||||
(anime.anilist_id,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row and row[0]:
|
||||
cursor.execute(
|
||||
"UPDATE seadex_state SET active = 0 "
|
||||
"WHERE anilist_id = %s AND user_id = %s",
|
||||
(anime.anilist_id, user_id),
|
||||
)
|
||||
db.commit()
|
||||
logger.debug(f"[FastAPI] Successfully deactivated tracking for anilist_id={anime.anilist_id}, user_id={user_id}")
|
||||
return {
|
||||
"status": "ok",
|
||||
"detail": f"Deactivated entry {anime.anilist_id}",
|
||||
}
|
||||
known = cursor.fetchone()
|
||||
|
||||
if known is None:
|
||||
db.rollback()
|
||||
logger.info("[FastAPI] Anime anilist_id=%s is not tracked, nothing to do", anime.anilist_id)
|
||||
return {"status": "ok", "detail": f"Stopped tracking Anilist ID={anime.anilist_id}"}
|
||||
|
||||
seadex_state_id = known[0]
|
||||
cursor.execute(
|
||||
"UPDATE seadex_state_subscriptions SET active = 0 "
|
||||
"WHERE user_id = %s AND seadex_state_id = %s",
|
||||
(user_id, seadex_state_id),
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
"SELECT 1 from seadex_state_subscriptions "
|
||||
"WHERE seadex_state_id = %s AND active = 1 LIMIT 1",
|
||||
(seadex_state_id,),
|
||||
)
|
||||
|
||||
if cursor.fetchone() is None:
|
||||
cursor.execute(
|
||||
"UPDATE seadex_state SET active = 0 WHERE id = %s",
|
||||
(seadex_state_id,),
|
||||
)
|
||||
logger.info("[FastAPI] No active subscribers left, deactivated state_id=%s", seadex_state_id)
|
||||
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.exception(f"[FastAPI] Failed removeing tracking for id={anime.anilist_id}, user_id={user_id}")
|
||||
logger.exception(
|
||||
"[FastAPI] Failed to unsubscribe anime anilist_id=%s for user_id=%s",
|
||||
anime.anilist_id, user_id,
|
||||
)
|
||||
raise HTTPException(status_code=500) from e
|
||||
|
||||
if row is None:
|
||||
logger.debug(f"[FastAPI] No tracked entry for anilist_id={anime.anilist_id}, user_id={user_id}")
|
||||
raise HTTPException(status_code=404, detail="No tracked entry for this AniList ID")
|
||||
if not row[0]:
|
||||
logger.debug(f"[FastAPI] Tracking was already deactivated for anilist_id={anime.anilist_id}, user_id={user_id}")
|
||||
return {"status": "ok", "detail": f"Entry {anime.anilist_id} was already inactive"}
|
||||
logger.info(
|
||||
"[FastAPI] Successfully deactivated tracking for anilist_id=%s for user_id=%s",
|
||||
anime.anilist_id, user_id,
|
||||
)
|
||||
return {"status": "ok", "detail": f"Stopped tracking Anilist ID={anime.anilist_id}"}
|
||||
|
||||
@user_router.get("/list/anime", response_model=AnimeList)
|
||||
def list_active_and_inactive_anime(user_id: UserId, db=Depends(get_db)):
|
||||
|
||||
Reference in new issue
Block a user