Added rename path to rename the custom name of active and inactive subscriptions
Generate openapi.json / generate-docs (push) Successful in 47s
Generate openapi.json / generate-docs (push) Successful in 47s
This commit is contained in:
1 parent
a5960e4d8d
commit
a169d5f6d6
2 files changed
+52
-1
No files matched your search
@@ -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
|
||||
@@ -71,3 +70,12 @@ 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 {}
|
||||
|
||||
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}")
|
||||
@@ -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 (
|
||||
@@ -164,3 +165,45 @@ def list_active_and_inactive_anime(
|
||||
raise HTTPException(status_code=500) from e
|
||||
|
||||
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"}
|
||||
Reference in new issue
Block a user