Skip to content

Commit

Permalink
Allow artist and album endpoints to filter by wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Nov 10, 2023
1 parent d08f96b commit 6c69b39
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
35 changes: 31 additions & 4 deletions src/MusicCatalogue.Api/Controllers/AlbumsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public AlbumsController(IMusicCatalogueFactory factory)
_factory = factory;
}

/// <summary>
/// Return album details given an ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id}")]
public async Task<ActionResult<Album>> GetAlbumByIdAsync(int id)
Expand All @@ -32,11 +37,28 @@ public async Task<ActionResult<Album>> GetAlbumByIdAsync(int id)
return album;
}

/// <summary>
/// Return a list of albums for the specified artist, filtering for items that are on/not on
/// the wishlist based on the arguments
/// </summary>
/// <param name="artistId"></param>
/// <param name="wishlist"></param>
/// <returns></returns>
[HttpGet]
[Route("artist/{artistId}")]
public async Task<ActionResult<IEnumerable<Album>>> GetAlbumsByArtistAsync(int artistId)
[Route("artist/{artistId}/{wishlist}")]
public async Task<ActionResult<IEnumerable<Album>>> GetAlbumsByArtistAsync(int artistId, bool wishlist)
{
List<Album> albums = await _factory.Albums.ListAsync(x => x.ArtistId == artistId);
List<Album> 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())
{
Expand All @@ -47,7 +69,7 @@ public async Task<ActionResult<IEnumerable<Album>>> GetAlbumsByArtistAsync(int a
}

/// <summary>
///
/// Update an album from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
Expand Down Expand Up @@ -75,6 +97,11 @@ public async Task<ActionResult<Album>> UpdateAlbumAsync([FromBody] Album templat
return album;
}

/// <summary>
/// Delete an album and its tracks given an album ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete]
[Route("{id}")]
public async Task<IActionResult> DeleteAlbum(int id)
Expand Down
29 changes: 26 additions & 3 deletions src/MusicCatalogue.Api/Controllers/ArtistsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ public ArtistsController(IMusicCatalogueFactory factory)
_factory = factory;
}

/// <summary>
/// Return a list of all the artists in the catalogue
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<ActionResult<List<Artist>>> GetArtistsAsync()
[Route("{wishlist:bool}")]
public async Task<ActionResult<List<Artist>>> GetArtistsAsync(bool wishlist)
{
// Get a list of all artists in the catalogue
List<Artist> 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())
{
Expand All @@ -37,8 +55,13 @@ public async Task<ActionResult<List<Artist>>> GetArtistsAsync()
return artists;
}

/// <summary>
/// Return artist details given an artist ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id}")]
[Route("{id:int}")]
public async Task<ActionResult<Artist>> GetArtistByIdAsync(int id)
{
var artist = await _factory.Artists.GetAsync(x => x.Id == id);
Expand Down

0 comments on commit 6c69b39

Please sign in to comment.