From 0cc92077556b186b18fedad3bea57ffb6673aa6b Mon Sep 17 00:00:00 2001 From: mkelvers Date: Mon, 1 Jun 2026 22:15:21 +0200 Subject: [PATCH] refactor: dedupe watchlist ids --- internal/watchlist/handler.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/watchlist/handler.go b/internal/watchlist/handler.go index c210ca1..abf2d2c 100644 --- a/internal/watchlist/handler.go +++ b/internal/watchlist/handler.go @@ -57,14 +57,12 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(c *gin.Context) { func (h *WatchlistHandler) HandleDeleteWatchlist(c *gin.Context) { userID := server.CurrentUserID(c) - animeID, err := strconv.ParseInt(c.Param("id"), 10, 64) - - if err != nil || animeID <= 0 { - server.RespondHTMLOrJSONError(c, http.StatusBadRequest, "invalid anime id") + animeID, ok := parseAnimeIDParam(c) + if !ok { return } - err = h.svc.RemoveEntry(c.Request.Context(), userID, animeID) + err := h.svc.RemoveEntry(c.Request.Context(), userID, animeID) if err != nil { server.RespondError( c, @@ -84,14 +82,12 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(c *gin.Context) { func (h *WatchlistHandler) HandleDeleteContinueWatching(c *gin.Context) { userID := server.CurrentUserID(c) - animeID, err := strconv.ParseInt(c.Param("id"), 10, 64) - - if err != nil || animeID <= 0 { - server.RespondHTMLOrJSONError(c, http.StatusBadRequest, "invalid anime id") + animeID, ok := parseAnimeIDParam(c) + if !ok { return } - err = h.svc.DeleteContinueWatching(c.Request.Context(), userID, animeID) + err := h.svc.DeleteContinueWatching(c.Request.Context(), userID, animeID) if err != nil { server.RespondError( c, @@ -108,6 +104,16 @@ func (h *WatchlistHandler) HandleDeleteContinueWatching(c *gin.Context) { c.Status(http.StatusOK) } +func parseAnimeIDParam(c *gin.Context) (int64, bool) { + animeID, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil || animeID <= 0 { + server.RespondHTMLOrJSONError(c, http.StatusBadRequest, "invalid anime id") + return 0, false + } + + return animeID, true +} + func (h *WatchlistHandler) HandleGetWatchlist(c *gin.Context) { user := server.CurrentUser(c) userID := server.CurrentUserID(c)