diff --git a/internal/api/api.go b/internal/api/api.go index 88929eed..659bcfea 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -46,6 +46,7 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev authed.Use(auth.RequireUser(pool)) authed.Post("/auth/logout", h.handleLogout) authed.Get("/me", h.handleGetMe) + authed.Get("/me/system-playlists-status", h.handleGetSystemPlaylistsStatus) authed.Get("/me/listenbrainz", h.handleGetListenBrainz) authed.Put("/me/listenbrainz", h.handlePutListenBrainz) authed.Get("/me/history", h.handleGetMyHistory) diff --git a/internal/api/me_system_playlists.go b/internal/api/me_system_playlists.go new file mode 100644 index 00000000..67f46ec8 --- /dev/null +++ b/internal/api/me_system_playlists.go @@ -0,0 +1,52 @@ +package api + +import ( + "errors" + "net/http" + + "github.com/jackc/pgx/v5" + + "git.fabledsword.com/bvandeusen/minstrel/internal/auth" + "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" +) + +// systemPlaylistsStatusResp is the wire shape for the home page's placeholder +// state lookup. Fields are nullable to distinguish "no row exists yet" (all +// nulls) from "row exists with these values". +type systemPlaylistsStatusResp struct { + InFlight bool `json:"in_flight"` + LastRunAt *string `json:"last_run_at"` + LastError *string `json:"last_error"` +} + +// handleGetSystemPlaylistsStatus implements GET /api/me/system-playlists-status. +// Returns the caller's system_playlist_runs row, or zero-values when no row +// exists yet (i.e. the user has never had a build attempted). +func (h *handlers) handleGetSystemPlaylistsStatus(w http.ResponseWriter, r *http.Request) { + caller, ok := auth.UserFromContext(r.Context()) + if !ok { + writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session") + return + } + + row, err := dbq.New(h.pool).GetSystemPlaylistRun(r.Context(), caller.ID) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + writeJSON(w, http.StatusOK, systemPlaylistsStatusResp{}) + return + } + h.logger.Error("api: get system playlists status", "err", err) + writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed") + return + } + + resp := systemPlaylistsStatusResp{InFlight: row.InFlight} + if row.LastRunAt.Valid { + s := row.LastRunAt.Time.Format("2006-01-02T15:04:05Z07:00") + resp.LastRunAt = &s + } + if row.LastError != nil { + resp.LastError = row.LastError + } + writeJSON(w, http.StatusOK, resp) +} diff --git a/internal/api/me_system_playlists_test.go b/internal/api/me_system_playlists_test.go new file mode 100644 index 00000000..4e594b40 --- /dev/null +++ b/internal/api/me_system_playlists_test.go @@ -0,0 +1,91 @@ +package api + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/go-chi/chi/v5" +) + +// newMeSystemPlaylistsRouter mounts only the route under test. Mirrors the +// per-handler routers in admin_covers_test.go. +func newMeSystemPlaylistsRouter(h *handlers) chi.Router { + r := chi.NewRouter() + r.Get("/api/me/system-playlists-status", h.handleGetSystemPlaylistsStatus) + return r +} + +func TestMeSystemPlaylistsStatus_NoSession401(t *testing.T) { + h, _ := testHandlers(t) + req := httptest.NewRequest(http.MethodGet, "/api/me/system-playlists-status", nil) + rec := httptest.NewRecorder() + newMeSystemPlaylistsRouter(h).ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Errorf("status = %d, want 401", rec.Code) + } +} + +func TestMeSystemPlaylistsStatus_NoRowReturnsDefault(t *testing.T) { + if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" { + t.Skip("MINSTREL_TEST_DATABASE_URL not set") + } + h, pool := testHandlers(t) + user := seedUser(t, pool, "alice", "pw", false) + req := httptest.NewRequest(http.MethodGet, "/api/me/system-playlists-status", nil) + req = withUser(req, user) + rec := httptest.NewRecorder() + newMeSystemPlaylistsRouter(h).ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String()) + } + var resp systemPlaylistsStatusResp + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode: %v", err) + } + if resp.InFlight { + t.Errorf("in_flight = true, want false") + } + if resp.LastRunAt != nil { + t.Errorf("last_run_at = %v, want nil", resp.LastRunAt) + } + if resp.LastError != nil { + t.Errorf("last_error = %v, want nil", resp.LastError) + } +} + +func TestMeSystemPlaylistsStatus_RowReturnsValues(t *testing.T) { + if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" { + t.Skip("MINSTREL_TEST_DATABASE_URL not set") + } + h, pool := testHandlers(t) + user := seedUser(t, pool, "bob", "pw", false) + errMsg := "fetch failed" + if _, err := pool.Exec(context.Background(), ` + INSERT INTO system_playlist_runs (user_id, in_flight, last_error) + VALUES ($1, true, $2) + `, user.ID, errMsg); err != nil { + t.Fatalf("seed runs: %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "/api/me/system-playlists-status", nil) + req = withUser(req, user) + rec := httptest.NewRecorder() + newMeSystemPlaylistsRouter(h).ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String()) + } + var resp systemPlaylistsStatusResp + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("decode: %v", err) + } + if !resp.InFlight { + t.Errorf("in_flight = false, want true") + } + if resp.LastError == nil || *resp.LastError != errMsg { + t.Errorf("last_error = %v, want %q", resp.LastError, errMsg) + } +}