Extending the IItemRepository interface

Another step we can take to extend our web service project with the related entities is to implement two new methods to retrieve the items related to an artist or a genre in the IItemRepository interface: the GetItemsByArtistIdAsync and GetItemsByGenreIdAsync methods. Both of these methods can be used by the GET /api/artists/{id}/items and GET /api/genre/{id}/items routes to retrieve the items.

Let's proceed by adding the following methods to the IItemsRepository interface and implementing them in the corresponding implementation:

    //Repositories/IItemRepository.cs
public interface IItemRepository : IRepository
{
...
Task<IEnumerable<Item>> GetItemByArtistIdAsync(Guid id);
Task<IEnumerable<Item>> GetItemByGenreIdAsync(Guid id);
...
}

//Repositories/ItemRepository.cs
public class ItemRepository
: IItemRepository
{
...
public async Task<IEnumerable<Item>> GetItemByArtistIdAsync(Guid id)
{
var items = await _context
.Items
.Where(item => item.ArtistId == id)
.Include(x => x.Genre)
.Include(x => x.Artist)
.ToListAsync();

return items;
}

public async Task<IEnumerable<Item>> GetItemByGenreIdAsync(Guid id)
{
var items = await _context.Items
.Where(item => item.GenreId == id)
.Include(x => x.Genre)
.Include(x => x.Artist)
.ToListAsync();

return items;
}
...
}
}

This code extends the IItemRepository interface and its implementation in order to include the functionalities to query the items using ArtistId and GenreId. Both methods retrieve data using the Where clause and by calling the Include statement to include the related entities in the result of the query. Once we have fully extended the repository layer, we can continue by also extending the service classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.131.238