refactor: simplify watchlist handler

This commit is contained in:
2026-04-14 22:23:07 +02:00
parent 7bf75205bd
commit 697d0cc32f

View File

@@ -5,6 +5,7 @@ import (
"errors" "errors"
"log" "log"
"net/http" "net/http"
"sort"
"strconv" "strconv"
"mal/internal/database" "mal/internal/database"
@@ -20,14 +21,22 @@ func NewHandler(svc *Service) *Handler {
return &Handler{svc: svc} return &Handler{svc: svc}
} }
func requireMethod(w http.ResponseWriter, r *http.Request, method string) bool {
if r.Method == method {
return true
}
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return false
}
func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) { func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if !requireMethod(w, r, http.MethodPost) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User) user := middleware.GetUser(r.Context())
if !ok || user == nil { if user == nil {
w.Header().Set("HX-Redirect", "/login") w.Header().Set("HX-Redirect", "/login")
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
@@ -79,13 +88,12 @@ func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request)
} }
func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) { func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete { if !requireMethod(w, r, http.MethodDelete) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User) user := middleware.GetUser(r.Context())
if !ok || user == nil { if user == nil {
w.Header().Set("HX-Redirect", "/login") w.Header().Set("HX-Redirect", "/login")
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
@@ -131,8 +139,7 @@ func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request)
} }
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) { func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if !requireMethod(w, r, http.MethodGet) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
@@ -152,8 +159,8 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
sortOrder = "desc" sortOrder = "desc"
} }
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User) user := middleware.GetUser(r.Context())
if !ok || user == nil { if user == nil {
http.Redirect(w, r, "/login", http.StatusFound) http.Redirect(w, r, "/login", http.StatusFound)
return return
} }
@@ -191,13 +198,12 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
} }
func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) { func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if !requireMethod(w, r, http.MethodGet) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User) user := middleware.GetUser(r.Context())
if !ok || user == nil { if user == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
@@ -215,13 +221,12 @@ func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request)
} }
func (h *Handler) HandleImportWatchlist(w http.ResponseWriter, r *http.Request) { func (h *Handler) HandleImportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if !requireMethod(w, r, http.MethodPost) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User) user := middleware.GetUser(r.Context())
if !ok || user == nil { if user == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
@@ -280,12 +285,5 @@ func (h *Handler) sortEntries(entries []database.GetUserWatchListRow, sortBy, so
} }
} }
// Simple bubble sort for small lists sort.SliceStable(entries, less)
for i := range len(entries) {
for j := i + 1; j < len(entries); j++ {
if less(j, i) {
entries[i], entries[j] = entries[j], entries[i]
}
}
}
} }