feat: add watchlist import api endpoint

This commit is contained in:
2026-05-06 23:56:44 +02:00
parent fdf1a8b568
commit 5ac6645e51
2 changed files with 30 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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)