From 6c69b394934361798b80e74e5996a57220021c4e Mon Sep 17 00:00:00 2001 From: Dave Walker Date: Fri, 10 Nov 2023 07:41:26 +0000 Subject: [PATCH] Allow artist and album endpoints to filter by wishlist --- .../Controllers/AlbumsController.cs | 35 ++++++++++++++++--- .../Controllers/ArtistsController.cs | 29 +++++++++++++-- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/MusicCatalogue.Api/Controllers/AlbumsController.cs b/src/MusicCatalogue.Api/Controllers/AlbumsController.cs index 19c748f..db11b8d 100644 --- a/src/MusicCatalogue.Api/Controllers/AlbumsController.cs +++ b/src/MusicCatalogue.Api/Controllers/AlbumsController.cs @@ -18,6 +18,11 @@ public AlbumsController(IMusicCatalogueFactory factory) _factory = factory; } + /// + /// Return album details given an ID + /// + /// + /// [HttpGet] [Route("{id}")] public async Task> GetAlbumByIdAsync(int id) @@ -32,11 +37,28 @@ public async Task> GetAlbumByIdAsync(int id) return album; } + /// + /// Return a list of albums for the specified artist, filtering for items that are on/not on + /// the wishlist based on the arguments + /// + /// + /// + /// [HttpGet] - [Route("artist/{artistId}")] - public async Task>> GetAlbumsByArtistAsync(int artistId) + [Route("artist/{artistId}/{wishlist}")] + public async Task>> GetAlbumsByArtistAsync(int artistId, bool wishlist) { - List albums = await _factory.Albums.ListAsync(x => x.ArtistId == artistId); + List albums; + + // Get the albums matching the specified criteria - the wish list flag is either null, false or true + if (wishlist) + { + albums = await _factory.Albums.ListAsync(x => (x.ArtistId == artistId) && (x.IsWishListItem == true)); + } + else + { + albums = await _factory.Albums.ListAsync(x => (x.ArtistId == artistId) && (x.IsWishListItem != true)); + } if (!albums.Any()) { @@ -47,7 +69,7 @@ public async Task>> GetAlbumsByArtistAsync(int a } /// - /// + /// Update an album from a template contained in the request body /// /// /// @@ -75,6 +97,11 @@ public async Task> UpdateAlbumAsync([FromBody] Album templat return album; } + /// + /// Delete an album and its tracks given an album ID + /// + /// + /// [HttpDelete] [Route("{id}")] public async Task DeleteAlbum(int id) diff --git a/src/MusicCatalogue.Api/Controllers/ArtistsController.cs b/src/MusicCatalogue.Api/Controllers/ArtistsController.cs index 174e0f5..d1adf3b 100644 --- a/src/MusicCatalogue.Api/Controllers/ArtistsController.cs +++ b/src/MusicCatalogue.Api/Controllers/ArtistsController.cs @@ -18,13 +18,31 @@ public ArtistsController(IMusicCatalogueFactory factory) _factory = factory; } + /// + /// Return a list of all the artists in the catalogue + /// + /// [HttpGet] - [Route("")] - public async Task>> GetArtistsAsync() + [Route("{wishlist:bool}")] + public async Task>> GetArtistsAsync(bool wishlist) { // Get a list of all artists in the catalogue List artists = await _factory.Artists.ListAsync(x => true); + // The artist list includes the albums by that artist, so where an artist has any albums + // filter them according to the wish list filter + foreach (var artist in artists) + { + if ((artist.Albums != null) && artist.Albums.Any()) + { + var filtered = artist.Albums.Where(x => (x.IsWishListItem ?? false) == wishlist).ToList(); + artist.Albums = filtered; + } + } + + // Remove any artists with no albums + artists.RemoveAll(x => (x.Albums == null) || ((x.Albums != null) && !x.Albums.Any())); + // If there are no artists, return a no content response if (!artists.Any()) { @@ -37,8 +55,13 @@ public async Task>> GetArtistsAsync() return artists; } + /// + /// Return artist details given an artist ID + /// + /// + /// [HttpGet] - [Route("{id}")] + [Route("{id:int}")] public async Task> GetArtistByIdAsync(int id) { var artist = await _factory.Artists.GetAsync(x => x.Id == id);