refactor: extract watchlist feature to its own domain slice

This commit is contained in:
2026-04-06 22:37:20 +02:00
parent 376b6f6418
commit afc0a1d218
4 changed files with 193 additions and 166 deletions

View File

@@ -1,30 +1,26 @@
package handlers
package watchlist
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
"malago/internal/database"
"malago/internal/middleware"
"malago/internal/templates"
)
type WatchlistHandler struct {
db database.Querier
type Handler struct {
svc *Service
}
func NewWatchlistHandler(db database.Querier) *WatchlistHandler {
return &WatchlistHandler{db: db}
func NewHandler(svc *Service) *Handler {
return &Handler{svc: svc}
}
func (h *WatchlistHandler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -49,7 +45,7 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.
animeImage := r.FormValue("anime_image")
status := r.FormValue("status")
log.Printf("watchlist add: id=%s, title=%s, title_en=%s, title_jp=%s", animeIDStr, animeTitle, animeTitleEnglish, animeTitleJapanese)
log.Printf("watchlist add: id=%s, title=%s", animeIDStr, animeTitle)
animeID, err := strconv.ParseInt(animeIDStr, 10, 64)
if err != nil {
@@ -57,28 +53,16 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.
return
}
// Ensure the anime exists in our local DB first (foreign key constraint)
_, err = h.db.UpsertAnime(r.Context(), database.UpsertAnimeParams{
ID: animeID,
req := AddRequest{
AnimeID: animeID,
TitleOriginal: animeTitle,
TitleEnglish: sql.NullString{String: animeTitleEnglish, Valid: animeTitleEnglish != ""},
TitleJapanese: sql.NullString{String: animeTitleJapanese, Valid: animeTitleJapanese != ""},
ImageUrl: animeImage,
})
if err != nil {
http.Error(w, fmt.Sprintf("failed to save anime reference: %v", err), http.StatusInternalServerError)
return
TitleEnglish: animeTitleEnglish,
TitleJapanese: animeTitleJapanese,
ImageURL: animeImage,
Status: status,
}
// Now insert/update the watchlist entry
entryID := uuid.New().String()
_, err = h.db.UpsertWatchListEntry(r.Context(), database.UpsertWatchListEntryParams{
ID: entryID,
UserID: user.ID,
AnimeID: animeID,
Status: status,
})
if err != nil {
if err := h.svc.AddEntry(r.Context(), user.ID, req); err != nil {
http.Error(w, fmt.Sprintf("failed to update watchlist: %v", err), http.StatusInternalServerError)
return
}
@@ -86,7 +70,7 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.
templates.WatchlistDropdown(int(animeID), animeTitle, animeTitleEnglish, animeTitleJapanese, animeImage, status).Render(r.Context(), w)
}
func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -99,7 +83,6 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.
return
}
// Parse the path to get anime ID (path is /api/watchlist/{id} possibly with query params)
path := r.URL.Path[len("/api/watchlist/"):]
animeID, err := strconv.ParseInt(path, 10, 64)
if err != nil {
@@ -107,29 +90,17 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.
return
}
// Get anime info before deleting (for dropdown refresh on anime page)
anime, err := h.db.GetAnime(r.Context(), animeID)
if err != nil {
http.Error(w, "anime not found", http.StatusNotFound)
return
}
err = h.db.DeleteWatchListEntry(r.Context(), database.DeleteWatchListEntryParams{
UserID: user.ID,
AnimeID: animeID,
})
anime, err := h.svc.RemoveEntry(r.Context(), user.ID, animeID)
if err != nil {
http.Error(w, fmt.Sprintf("failed to delete from watchlist: %v", err), http.StatusInternalServerError)
return
}
// If called from watchlist page, just return empty (hx-swap="delete" handles removal)
if r.URL.Query().Get("from") == "watchlist" {
w.WriteHeader(http.StatusOK)
return
}
// Extract nullable strings
titleEnglish := ""
if anime.TitleEnglish.Valid {
titleEnglish = anime.TitleEnglish.String
@@ -139,11 +110,10 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.
titleJapanese = anime.TitleJapanese.String
}
// Otherwise return updated dropdown for anime page
templates.WatchlistDropdown(int(animeID), anime.TitleOriginal, titleEnglish, titleJapanese, anime.ImageUrl, "").Render(r.Context(), w)
}
func (h *WatchlistHandler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -162,7 +132,7 @@ func (h *WatchlistHandler) HandleGetWatchlist(w http.ResponseWriter, r *http.Req
return
}
entries, err := h.db.GetUserWatchList(r.Context(), user.ID)
entries, err := h.svc.GetUserWatchlist(r.Context(), user.ID)
if err != nil {
http.Error(w, fmt.Sprintf("failed to fetch watchlist: %v", err), http.StatusInternalServerError)
return
@@ -183,22 +153,7 @@ func (h *WatchlistHandler) HandleGetWatchlist(w http.ResponseWriter, r *http.Req
templates.Watchlist(filteredEntries, layout, statusFilter).Render(r.Context(), w)
}
// WatchlistExportEntry represents a single entry in the export format
type WatchlistExportEntry struct {
AnimeID int64 `json:"anime_id"`
Title string `json:"title"`
ImageURL string `json:"image_url"`
Status string `json:"status"`
UpdatedAt string `json:"updated_at"`
}
// WatchlistExport is the full export format
type WatchlistExport struct {
ExportedAt string `json:"exported_at"`
Entries []WatchlistExportEntry `json:"entries"`
}
func (h *WatchlistHandler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -210,33 +165,18 @@ func (h *WatchlistHandler) HandleExportWatchlist(w http.ResponseWriter, r *http.
return
}
entries, err := h.db.GetUserWatchList(r.Context(), user.ID)
export, err := h.svc.Export(r.Context(), user.ID)
if err != nil {
http.Error(w, fmt.Sprintf("failed to fetch watchlist: %v", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("failed to export: %v", err), http.StatusInternalServerError)
return
}
export := WatchlistExport{
ExportedAt: time.Now().UTC().Format(time.RFC3339),
Entries: make([]WatchlistExportEntry, len(entries)),
}
for i, entry := range entries {
export.Entries[i] = WatchlistExportEntry{
AnimeID: entry.AnimeID,
Title: entry.DisplayTitle(),
ImageURL: entry.ImageUrl,
Status: entry.Status,
UpdatedAt: entry.UpdatedAt.Format(time.RFC3339),
}
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", "attachment; filename=malago-watchlist.json")
json.NewEncoder(w).Encode(export)
}
func (h *WatchlistHandler) HandleImportWatchlist(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleImportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -248,7 +188,6 @@ func (h *WatchlistHandler) HandleImportWatchlist(w http.ResponseWriter, r *http.
return
}
// Parse multipart form (max 10MB)
if err := r.ParseMultipartForm(10 << 20); err != nil {
http.Error(w, "failed to parse form", http.StatusBadRequest)
return
@@ -261,37 +200,15 @@ func (h *WatchlistHandler) HandleImportWatchlist(w http.ResponseWriter, r *http.
}
defer file.Close()
var export WatchlistExport
var export ExportData
if err := json.NewDecoder(file).Decode(&export); err != nil {
http.Error(w, "invalid JSON format", http.StatusBadRequest)
return
}
imported := 0
for _, entry := range export.Entries {
// Upsert anime - store title as original (we don't know which type it is from export)
_, err := h.db.UpsertAnime(r.Context(), database.UpsertAnimeParams{
ID: entry.AnimeID,
TitleOriginal: entry.Title,
TitleEnglish: sql.NullString{},
TitleJapanese: sql.NullString{},
ImageUrl: entry.ImageURL,
})
if err != nil {
continue
}
// Upsert watchlist entry
_, err = h.db.UpsertWatchListEntry(r.Context(), database.UpsertWatchListEntryParams{
ID: uuid.New().String(),
UserID: user.ID,
AnimeID: entry.AnimeID,
Status: entry.Status,
})
if err != nil {
continue
}
imported++
if _, err := h.svc.Import(r.Context(), user.ID, export); err != nil {
http.Error(w, "failed to import", http.StatusInternalServerError)
return
}
w.Header().Set("HX-Redirect", "/watchlist")

View File

@@ -0,0 +1,164 @@
package watchlist
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/google/uuid"
"malago/internal/database"
)
type Service struct {
db database.Querier
}
func NewService(db database.Querier) *Service {
return &Service{db: db}
}
type AddRequest struct {
AnimeID int64
TitleOriginal string
TitleEnglish string
TitleJapanese string
ImageURL string
Status string
}
func (s *Service) AddEntry(ctx context.Context, userID string, req AddRequest) error {
if req.AnimeID == 0 {
return fmt.Errorf("invalid anime ID")
}
_, err := s.db.UpsertAnime(ctx, database.UpsertAnimeParams{
ID: req.AnimeID,
TitleOriginal: req.TitleOriginal,
TitleEnglish: sql.NullString{String: req.TitleEnglish, Valid: req.TitleEnglish != ""},
TitleJapanese: sql.NullString{String: req.TitleJapanese, Valid: req.TitleJapanese != ""},
ImageUrl: req.ImageURL,
})
if err != nil {
return fmt.Errorf("failed to save anime reference: %w", err)
}
entryID := uuid.New().String()
_, err = s.db.UpsertWatchListEntry(ctx, database.UpsertWatchListEntryParams{
ID: entryID,
UserID: userID,
AnimeID: req.AnimeID,
Status: req.Status,
})
if err != nil {
return fmt.Errorf("failed to update watchlist: %w", err)
}
return nil
}
func (s *Service) RemoveEntry(ctx context.Context, userID string, animeID int64) (database.Anime, error) {
if animeID == 0 {
return database.Anime{}, fmt.Errorf("invalid anime ID")
}
anime, err := s.db.GetAnime(ctx, animeID)
if err != nil {
return database.Anime{}, fmt.Errorf("anime not found: %w", err)
}
err = s.db.DeleteWatchListEntry(ctx, database.DeleteWatchListEntryParams{
UserID: userID,
AnimeID: animeID,
})
if err != nil {
return database.Anime{}, fmt.Errorf("failed to delete from watchlist: %w", err)
}
return anime, nil
}
func (s *Service) GetUserWatchlist(ctx context.Context, userID string) ([]database.GetUserWatchListRow, error) {
entries, err := s.db.GetUserWatchList(ctx, userID)
if err != nil {
return nil, fmt.Errorf("failed to fetch watchlist: %w", err)
}
return entries, nil
}
type ExportEntry struct {
AnimeID int64 `json:"anime_id"`
Title string `json:"title"`
ImageURL string `json:"image_url"`
Status string `json:"status"`
UpdatedAt string `json:"updated_at"`
}
type ExportData struct {
ExportedAt string `json:"exported_at"`
Entries []ExportEntry `json:"entries"`
}
// displayTitle returns the best available title
func displayTitle(e database.GetUserWatchListRow) string {
if e.TitleEnglish.Valid && e.TitleEnglish.String != "" {
return e.TitleEnglish.String
}
if e.TitleJapanese.Valid && e.TitleJapanese.String != "" {
return e.TitleJapanese.String
}
return e.TitleOriginal
}
func (s *Service) Export(ctx context.Context, userID string) (ExportData, error) {
entries, err := s.GetUserWatchlist(ctx, userID)
if err != nil {
return ExportData{}, err
}
export := ExportData{
ExportedAt: time.Now().UTC().Format(time.RFC3339),
Entries: make([]ExportEntry, len(entries)),
}
for i, entry := range entries {
export.Entries[i] = ExportEntry{
AnimeID: entry.AnimeID,
Title: displayTitle(entry),
ImageURL: entry.ImageUrl,
Status: entry.Status,
UpdatedAt: entry.UpdatedAt.Format(time.RFC3339),
}
}
return export, nil
}
func (s *Service) Import(ctx context.Context, userID string, export ExportData) (int, error) {
imported := 0
for _, entry := range export.Entries {
_, err := s.db.UpsertAnime(ctx, database.UpsertAnimeParams{
ID: entry.AnimeID,
TitleOriginal: entry.Title,
TitleEnglish: sql.NullString{},
TitleJapanese: sql.NullString{},
ImageUrl: entry.ImageURL,
})
if err != nil {
continue // skip failures and keep going
}
_, err = s.db.UpsertWatchListEntry(ctx, database.UpsertWatchListEntryParams{
ID: uuid.New().String(),
UserID: userID,
AnimeID: entry.AnimeID,
Status: entry.Status,
})
if err != nil {
continue
}
imported++
}
return imported, nil
}

View File

@@ -1,56 +0,0 @@
package handlers
import (
"net/http"
"malago/internal/auth"
"malago/internal/templates"
)
type AuthHandler struct {
authService *auth.Service
}
func NewAuthHandler(authService *auth.Service) *AuthHandler {
return &AuthHandler{authService: authService}
}
// Render the login/register pages here (assuming you have these templates)
func (h *AuthHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
username := r.FormValue("username")
password := r.FormValue("password")
session, err := h.authService.Login(r.Context(), username, password)
if err != nil {
// Just handle generically for now, perhaps via HTMX toast
http.Error(w, "invalid credentials", http.StatusUnauthorized)
return
}
auth.SetSessionCookie(w, session.ID, session.ExpiresAt)
// HTMX-friendly redirect to root or previous page
w.Header().Set("HX-Redirect", "/")
http.Redirect(w, r, "/", http.StatusFound)
}
func (h *AuthHandler) HandleLogout(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_id")
if err == nil {
_ = h.authService.Logout(r.Context(), cookie.Value)
}
auth.ClearSessionCookie(w)
w.Header().Set("HX-Redirect", "/")
http.Redirect(w, r, "/", http.StatusFound)
}
func (h *AuthHandler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
templates.Login().Render(r.Context(), w)
}

View File

@@ -5,6 +5,7 @@ import (
"malago/internal/database"
"malago/internal/features/auth"
"malago/internal/features/watchlist"
"malago/internal/handlers"
"malago/internal/jikan"
"malago/internal/middleware"
@@ -20,7 +21,8 @@ func NewRouter(cfg Config) http.Handler {
mux := http.NewServeMux()
authHandler := auth.NewHandler(cfg.AuthService)
watchlistHandler := handlers.NewWatchlistHandler(cfg.DB)
watchlistSvc := watchlist.NewService(cfg.DB)
watchlistHandler := watchlist.NewHandler(watchlistSvc)
animeHandler := handlers.NewAnimeHandler(cfg.JikanClient, cfg.DB)
// Serve static files