Updating the IItemRepository implementation

Since we added the new IsInactive field in our database, we can proceed by adapting the IItemRepository interface to filter our data based on the IsInactive field. Therefore, we will proceed by implementing the following changes in order to maintain consistency in our data source:

  • The IItemRepository.GetAsync() method will filter all the fields by IsInactive = false. Consequently, the resulting response will only contain active entities. This kind of approach guarantees that we get a lightweight response when we try to get multiple entities from the database. 
  • In the same way, GetItemByArtistIdAsync and GetItemByGenreIdAsync will filter the result by using the IsInactive = false flag. Also, in this case, we want to keep the response as light as possible.
  • The IItemRepository.GetAsync(Guid id) method will retrieve the details of the required entity regardless of the IsInactive flag. Moreover, this method is used by the validation checks of the application, therefore, we need to avoid duplicate IDs when we insert new objects, whether they are active or not.

Let's proceed by implementing these specifications mentioned in the IItemRepository interface:

namespace Catalog.Infrastructure.Repositories
{
public class ItemRepository
: IItemRepository
{
...

public async Task<IEnumerable<Item>> GetAsync()
{
return await _context.Items
.Where(x => !x.IsInactive)
.AsNoTracking()
.ToListAsync();
}

public async Task<IEnumerable<Item>> GetItemByArtistIdAsync(Guid id)
{
var items = await _context
.Items
.Where(x => !x.IsInactive)
.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(x => !x.IsInactive)
.Where(item => item.GenreId == id)
.Include(x => x.Genre)
.Include(x => x.Artist)
.ToListAsync();

return items;
}
...

This implementation changes the behavior of the GetAsync, GetItemByArtistIdAsync, and GetItemByGenreIdAsync methods by filtering for IsInactive == false using the Where LINQ  clause. On the other hand, GetAsync(Guid id) remains the same because, as mentioned in the specifications, we want the get detail operation to always retrieve the information, including cases where the record is not active. Therefore, we can test the resulting implementation by executing the following command in the root of the project:

dotnet test

All tests should pass because, by default, the IsInactive Boolean field is always false. To test the Where(x=>!x.IsInactive) change, we can add a new record in the Catalog.API/tests/Catalog.Fixtures/Data/item.json file by adding a new inactive item:

{
"Id": "f5da5ce4-091e-492e-a70a-22b073d75a52",
"Name": "Untitled",
"Description": "Untitled by Kendrick Lamar",
"PictureUri": "https://mycdn.com/pictures/32423423",
"ReleaseDate": "2016-01-01T00:00:00+00:00",
"Price": {
"Amount": 23.5,
"Currency": "EUR"
},
"Format": "Vinyl 33g",
"AvailableStock": 6,
"GenreId": "c04f05c0-f6ad-44d1-a400-3375bfb5dfd6",
"Genre": null,
"ArtistId": "3eb00b42-a9f0-4012-841d-70ebf3ab7474",
"Artist": null,
"IsInactive": true
},

Therefore, we can verify that the number of records retrieved by the get operation changes by creating a new test and counting the results. For example, if the item.json file contains three records and one is inactive, the get operation should retrieve three records. As an alternative, we can double-check by verifying that the Item.Id fields of the result do not include an inactive record:

namespace Catalog.Infrastructure.Tests
{
...
public class ItemRepositoryTests : IClassFixture<CatalogContextFactory>
{

[Theory]
[InlineData("f5da5ce4-091e-492e-a70a-22b073d75a52")]
public async Task getitems_should_not_return_inactive_records(string id)
{
var result =
await _sut.GetAsync();

result.Any(x => x.Id == new Guid(id)).ShouldBeFalse();
}

...
}

}

I chose to test the deletion behavior by adding a new  getitems_should_not_return_inactive_records method in the ItemRepositoryTests class. The test verifies that when I call the GetAsync method of the repository, the result excludes the Item entity with the Id specified as a parameter of the test. A similar approach can be also taken for the GetItemByArtistIdAsync and the GetItemByGenreIdAsync methods.

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

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