From f37fd782afa40ed3e8167588736e56541e99dec5 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 21 Apr 2026 08:01:25 -0400 Subject: [PATCH] feat(api): GET /api/artists/{id} with nested albums --- internal/api/library.go | 42 ++++++++++++++++- internal/api/library_test.go | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/internal/api/library.go b/internal/api/library.go index bee2af24..dd7effc4 100644 --- a/internal/api/library.go +++ b/internal/api/library.go @@ -91,9 +91,47 @@ func (h *handlers) handleGetAlbum(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, detail) } -// handleGetArtist is implemented in Task 8. +// handleGetArtist implements GET /api/artists/{id}. Returns artist + albums; +// each album carries its own track_count (one count query per album, same +// pattern as subsonic). At M1 sizes (albums-per-artist << 100) this is fine. func (h *handlers) handleGetArtist(w http.ResponseWriter, r *http.Request) { - writeErr(w, http.StatusNotImplemented, "not_implemented", "stub") + id, ok := parseUUID(chi.URLParam(r, "id")) + if !ok { + writeErr(w, http.StatusBadRequest, "bad_request", "invalid artist id") + return + } + q := dbq.New(h.pool) + artist, err := q.GetArtistByID(r.Context(), id) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + writeErr(w, http.StatusNotFound, "not_found", "artist not found") + return + } + h.logger.Error("api: get artist failed", "err", err) + writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed") + return + } + albums, err := q.ListAlbumsByArtist(r.Context(), id) + if err != nil { + h.logger.Error("api: list albums by artist failed", "err", err) + writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed") + return + } + refs := make([]AlbumRef, 0, len(albums)) + for _, a := range albums { + count, cerr := q.CountTracksByAlbum(r.Context(), a.ID) + if cerr != nil { + h.logger.Error("api: count tracks failed", "err", cerr) + writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed") + return + } + refs = append(refs, albumRefFrom(a, artist.Name, int(count), 0)) + } + detail := ArtistDetail{ + ArtistRef: artistRefFrom(artist, len(albums)), + Albums: refs, + } + writeJSON(w, http.StatusOK, detail) } // handleListArtists is implemented in Task 9. diff --git a/internal/api/library_test.go b/internal/api/library_test.go index 6a66f7aa..0a48216e 100644 --- a/internal/api/library_test.go +++ b/internal/api/library_test.go @@ -164,3 +164,93 @@ func TestHandleGetAlbum_NotFound(t *testing.T) { t.Errorf("status = %d, want 404", w.Code) } } + +func TestHandleGetArtist_HappyPath(t *testing.T) { + h, pool := testHandlers(t) + truncateLibrary(t, pool) + artist := seedArtist(t, pool, "Beatles") + album1 := seedAlbum(t, pool, artist.ID, "Abbey Road", 1969) + album2 := seedAlbum(t, pool, artist.ID, "Let It Be", 1970) + seedTrack(t, pool, album1.ID, artist.ID, "Something", 3, 183_000) + seedTrack(t, pool, album2.ID, artist.ID, "Get Back", 1, 189_000) + + req := httptest.NewRequest(http.MethodGet, "/api/artists/"+uuidToString(artist.ID), nil) + w := httptest.NewRecorder() + newLibraryRouter(h).ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("status = %d body = %s", w.Code, w.Body.String()) + } + var got ArtistDetail + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("decode: %v body=%s", err, w.Body.String()) + } + if got.Name != "Beatles" { + t.Errorf("name = %q", got.Name) + } + if got.AlbumCount != 2 { + t.Errorf("album_count = %d, want 2", got.AlbumCount) + } + if len(got.Albums) != 2 { + t.Fatalf("albums len = %d, want 2", len(got.Albums)) + } + // ListAlbumsByArtist orders by release_date then sort_title; Abbey Road (1969) first. + if got.Albums[0].Title != "Abbey Road" { + t.Errorf("first album = %q, want Abbey Road", got.Albums[0].Title) + } + for _, a := range got.Albums { + if a.ArtistName != "Beatles" { + t.Errorf("album.artist_name = %q", a.ArtistName) + } + if a.CoverURL == "" { + t.Error("album missing cover_url") + } + if a.TrackCount != 1 { + t.Errorf("album.track_count = %d, want 1", a.TrackCount) + } + } +} + +func TestHandleGetArtist_NoAlbums(t *testing.T) { + h, pool := testHandlers(t) + truncateLibrary(t, pool) + artist := seedArtist(t, pool, "Solo") + + req := httptest.NewRequest(http.MethodGet, "/api/artists/"+uuidToString(artist.ID), nil) + w := httptest.NewRecorder() + newLibraryRouter(h).ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("status = %d", w.Code) + } + var got ArtistDetail + _ = json.Unmarshal(w.Body.Bytes(), &got) + if got.Albums == nil { + t.Error("albums = nil, want []") + } + if got.AlbumCount != 0 { + t.Errorf("album_count = %d, want 0", got.AlbumCount) + } +} + +func TestHandleGetArtist_BadUUID(t *testing.T) { + h, _ := testHandlers(t) + req := httptest.NewRequest(http.MethodGet, "/api/artists/not-a-uuid", nil) + w := httptest.NewRecorder() + newLibraryRouter(h).ServeHTTP(w, req) + if w.Code != http.StatusBadRequest { + t.Errorf("status = %d, want 400", w.Code) + } +} + +func TestHandleGetArtist_NotFound(t *testing.T) { + h, pool := testHandlers(t) + truncateLibrary(t, pool) + req := httptest.NewRequest(http.MethodGet, + "/api/artists/00000000-0000-0000-0000-000000000001", nil) + w := httptest.NewRecorder() + newLibraryRouter(h).ServeHTTP(w, req) + if w.Code != http.StatusNotFound { + t.Errorf("status = %d, want 404", w.Code) + } +}