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

@@ -0,0 +1,216 @@
package watchlist
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"malago/internal/database"
"malago/internal/middleware"
"malago/internal/templates"
)
type Handler struct {
svc *Service
}
func NewHandler(svc *Service) *Handler {
return &Handler{svc: svc}
}
func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User)
if !ok || user == nil {
w.Header().Set("HX-Redirect", "/login")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid form", http.StatusBadRequest)
return
}
animeIDStr := r.FormValue("anime_id")
animeTitle := r.FormValue("anime_title")
animeTitleEnglish := r.FormValue("anime_title_english")
animeTitleJapanese := r.FormValue("anime_title_japanese")
animeImage := r.FormValue("anime_image")
status := r.FormValue("status")
log.Printf("watchlist add: id=%s, title=%s", animeIDStr, animeTitle)
animeID, err := strconv.ParseInt(animeIDStr, 10, 64)
if err != nil {
http.Error(w, "invalid anime ID", http.StatusBadRequest)
return
}
req := AddRequest{
AnimeID: animeID,
TitleOriginal: animeTitle,
TitleEnglish: animeTitleEnglish,
TitleJapanese: animeTitleJapanese,
ImageURL: animeImage,
Status: status,
}
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
}
templates.WatchlistDropdown(int(animeID), animeTitle, animeTitleEnglish, animeTitleJapanese, animeImage, status).Render(r.Context(), w)
}
func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User)
if !ok || user == nil {
w.Header().Set("HX-Redirect", "/login")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
path := r.URL.Path[len("/api/watchlist/"):]
animeID, err := strconv.ParseInt(path, 10, 64)
if err != nil {
http.Error(w, "invalid anime ID", http.StatusBadRequest)
return
}
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 r.URL.Query().Get("from") == "watchlist" {
w.WriteHeader(http.StatusOK)
return
}
titleEnglish := ""
if anime.TitleEnglish.Valid {
titleEnglish = anime.TitleEnglish.String
}
titleJapanese := ""
if anime.TitleJapanese.Valid {
titleJapanese = anime.TitleJapanese.String
}
templates.WatchlistDropdown(int(animeID), anime.TitleOriginal, titleEnglish, titleJapanese, anime.ImageUrl, "").Render(r.Context(), w)
}
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
layout := r.URL.Query().Get("view")
if layout != "grid" && layout != "table" {
layout = "table"
}
statusFilter := r.URL.Query().Get("status")
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User)
if !ok || user == nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
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
}
var filteredEntries []database.GetUserWatchListRow
if statusFilter != "" && statusFilter != "all" {
for _, entry := range entries {
if entry.Status == statusFilter {
filteredEntries = append(filteredEntries, entry)
}
}
} else {
statusFilter = "all"
filteredEntries = entries
}
templates.Watchlist(filteredEntries, layout, statusFilter).Render(r.Context(), w)
}
func (h *Handler) HandleExportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User)
if !ok || user == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
export, err := h.svc.Export(r.Context(), user.ID)
if err != nil {
http.Error(w, fmt.Sprintf("failed to export: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", "attachment; filename=malago-watchlist.json")
json.NewEncoder(w).Encode(export)
}
func (h *Handler) HandleImportWatchlist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
user, ok := r.Context().Value(middleware.UserContextKey).(*database.User)
if !ok || user == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if err := r.ParseMultipartForm(10 << 20); err != nil {
http.Error(w, "failed to parse form", http.StatusBadRequest)
return
}
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "no file uploaded", http.StatusBadRequest)
return
}
defer file.Close()
var export ExportData
if err := json.NewDecoder(file).Decode(&export); err != nil {
http.Error(w, "invalid JSON format", http.StatusBadRequest)
return
}
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")
w.WriteHeader(http.StatusOK)
}

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
}