diff --git a/src/anilist_errors.py b/src/anilist_errors.py index 41233f8..02113a1 100644 --- a/src/anilist_errors.py +++ b/src/anilist_errors.py @@ -57,7 +57,6 @@ class AniListApiError(AniListError): def __init__(self, detail: str): super().__init__(f"API error: {detail}") - class RateLimitedError(AniListError): """AniList asked us to slow down. Stop the run instead of continuing.""" status_code = 503 @@ -70,4 +69,13 @@ class RateLimitedError(AniListError): @property def headers(self) -> dict[str, str]: - return {"Retry-After": str(self.retry_after)} if self.retry_after is not None else {} \ No newline at end of file + return {"Retry-After": str(self.retry_after)} if self.retry_after is not None else {} + +class NoSuchSubscription(AniListError): + """No anime subscription exists in the database for this ID.""" + status_code = 422 + code = "sea_no_such_subscription" + client_visible = True + def __init__(self, anilist_id: int): + self.anilist_id = anilist_id + super().__init__(f"No subscription exists for anilist_id: {anilist_id}") \ No newline at end of file diff --git a/src/routers/user.py b/src/routers/user.py index 79947aa..30e9ee0 100644 --- a/src/routers/user.py +++ b/src/routers/user.py @@ -3,6 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException from seadex import SeaDexEntry from service_auth import UserId +from anilist_errors import NoSuchSubscription from anilist_query import get_anime_name from auth_config import Caller, auth from db_module import ( @@ -163,4 +164,46 @@ def list_active_and_inactive_anime( logger.exception("[FastAPI] Failed to list anime") raise HTTPException(status_code=500) from e - return anime_list \ No newline at end of file + return anime_list + +@user_router.post("/anime/rename") +def rename_anime( + anime: AnimeCreate, + user_id: UserId, + db=Depends(get_db), + ): + logger.info("[FastAPI] Received request add custom name for anilist_id=%s, user_id=%s", anime.anilist_id, user_id) + try: + with db.cursor() as cursor: + cursor.execute( + "SELECT st.seadex_state_id " + "FROM seadex_state_subscriptions AS st " + "JOIN seadex_state AS s ON st.seadex_state_id = s.id " + "WHERE s.anilist_id = %s AND st.user_id = %s", + (anime.anilist_id, user_id), + ) + row = cursor.fetchone() + if row is not None: + cursor.execute( + "UPDATE seadex_state_subscriptions SET custom_name = %s " + "WHERE user_id = %s AND seadex_state_id = %s", + (anime.custom_name, user_id, row[0]), + ) + db.commit() + except Exception as e: + db.rollback() + logger.exception( + "[FastAPI] Failed to change custom_name anilist_id=%s for user_id=%s, custom_name=%s", + anime.anilist_id, user_id, anime.custom_name, + ) + raise HTTPException(status_code=500) from e + + if row is None: + logger.warning( + "User user_id=%s tried to add a custom name=%s for anilist_id=%s which does not exist", + user_id, anime.custom_name, anime.anilist_id, + ) + raise NoSuchSubscription(anime.anilist_id) + + logger.info("[FastAPI] Successfully added custom name for anilist_id=%s, user_id=%s", anime.anilist_id, user_id) + return {"status": "ok", "detail": f"Display name set to {anime.custom_name}" if anime.custom_name else "Display name removed"} \ No newline at end of file