diff --git a/api/watchlist/handler.go b/api/watchlist/handler.go index a000fe8..bab69f2 100644 --- a/api/watchlist/handler.go +++ b/api/watchlist/handler.go @@ -57,6 +57,35 @@ func (h *Handler) HandleUpdateWatchlist(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusOK) } +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 := middleware.GetUser(r.Context()) + if user == nil { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "failed to get file from request", http.StatusBadRequest) + return + } + defer file.Close() + + if err := h.service.ImportWatchlist(r.Context(), user.ID, file); err != nil { + log.Printf("import failed: %v", err) + http.Error(w, "import failed: "+err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("HX-Redirect", "/watchlist") + w.WriteHeader(http.StatusOK) +} + func (h *Handler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.Request) { user := middleware.GetUser(r.Context()) if user == nil { diff --git a/internal/server/routes.go b/internal/server/routes.go index 8092e0d..aa01dac 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -135,6 +135,7 @@ func NewRouter(cfg Config) http.Handler { // Watchlist Endpoints mux.HandleFunc("/api/watchlist/card", watchlistHandler.HandleCardWatchlist) + mux.HandleFunc("/api/watchlist/import", watchlistHandler.HandleImportWatchlist) mux.HandleFunc("/api/watchlist", watchlistHandler.HandleUpdateWatchlist) mux.HandleFunc("/api/watchlist/", watchlistHandler.HandleDeleteWatchlist) mux.HandleFunc("/api/continue-watching/", watchlistHandler.HandleDeleteContinueWatching)