Skip to content

Commit

Permalink
Removing the GetById from IRepository
Browse files Browse the repository at this point in the history
- removed it and updated everywhere, where it was called to use the async version
  • Loading branch information
efleming18 committed Mar 2, 2019
1 parent 239d217 commit 058d4e6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/ApplicationCore/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IRepository<T> where T : BaseEntity
{
T GetById(int id);
T GetSingleBySpec(ISpecification<T> spec);
IEnumerable<T> ListAll();
IEnumerable<T> List(ISpecification<T> spec);
Expand Down
5 changes: 0 additions & 5 deletions src/Infrastructure/Data/EfRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public EfRepository(CatalogContext dbContext)
_dbContext = dbContext;
}

public virtual T GetById(int id)
{
return _dbContext.Set<T>().Find(id);
}

public T GetSingleBySpec(ISpecification<T> spec)
{
return List(spec).FirstOrDefault();
Expand Down
46 changes: 26 additions & 20 deletions src/Web/Services/BasketViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class BasketViewModelService : IBasketViewModelService
{
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IUriComposer _uriComposer;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IAsyncRepository<CatalogItem> _itemRepository;

public BasketViewModelService(IAsyncRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IAsyncRepository<CatalogItem> itemRepository,
IUriComposer uriComposer)
{
_basketRepository = basketRepository;
Expand All @@ -34,30 +34,15 @@ public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
{
return await CreateBasketForUser(userName);
}
return CreateViewModelFromBasket(basket);
return await CreateViewModelFromBasket(basket);
}

private BasketViewModel CreateViewModelFromBasket(Basket basket)
private async Task<BasketViewModel> CreateViewModelFromBasket(Basket basket)
{
var viewModel = new BasketViewModel();
viewModel.Id = basket.Id;
viewModel.BuyerId = basket.BuyerId;
viewModel.Items = basket.Items.Select(i =>
{
var itemModel = new BasketItemViewModel()
{
Id = i.Id,
UnitPrice = i.UnitPrice,
Quantity = i.Quantity,
CatalogItemId = i.CatalogItemId

};
var item = _itemRepository.GetById(i.CatalogItemId);
itemModel.PictureUrl = _uriComposer.ComposePicUri(item.PictureUri);
itemModel.ProductName = item.Name;
return itemModel;
})
.ToList();
viewModel.Items = await GetBasketItems(basket.Items); ;
return viewModel;
}

Expand All @@ -73,5 +58,26 @@ private async Task<BasketViewModel> CreateBasketForUser(string userId)
Items = new List<BasketItemViewModel>()
};
}

private async Task<List<BasketItemViewModel>> GetBasketItems(IReadOnlyCollection<BasketItem> basketItems)
{
var items = new List<BasketItemViewModel>();
foreach (var item in basketItems)
{
var itemModel = new BasketItemViewModel
{
Id = item.Id,
UnitPrice = item.UnitPrice,
Quantity = item.Quantity,
CatalogItemId = item.CatalogItemId
};
var catalogItem = await _itemRepository.GetByIdAsync(item.CatalogItemId);
itemModel.PictureUrl = _uriComposer.ComposePicUri(catalogItem.PictureUri);
itemModel.ProductName = catalogItem.Name;
items.Add(itemModel);
}

return items;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task DeleteItemFromBasket()
await _basketItemRepository.DeleteAsync(existingBasket.Items.FirstOrDefault());
_catalogContext.SaveChanges();

var basketFromDB = _basketRepository.GetById(BasketBuilder.BasketId);
var basketFromDB = await _basketRepository.GetByIdAsync(BasketBuilder.BasketId);

Assert.Equal(0, basketFromDB.Items.Count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.eShopWeb.UnitTests.Builders;
using Xunit;
using Xunit.Abstractions;
using System.Threading.Tasks;

namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
{
Expand All @@ -24,15 +25,15 @@ public GetById(ITestOutputHelper output)
}

[Fact]
public void GetsExistingOrder()
public async Task GetsExistingOrder()
{
var existingOrder = OrderBuilder.WithDefaultValues();
_catalogContext.Orders.Add(existingOrder);
_catalogContext.SaveChanges();
int orderId = existingOrder.Id;
_output.WriteLine($"OrderId: {orderId}");

var orderFromRepo = _orderRepository.GetById(orderId);
var orderFromRepo = await _orderRepository.GetByIdAsync(orderId);
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId);

// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
Expand Down

0 comments on commit 058d4e6

Please sign in to comment.