Browse Source

Merge pull request #300 from ovicrisan/master

Allow to remove items from the basket setting quantity to zero
main
Eric Fleming 6 years ago
committed by GitHub
parent
commit
9ce4bcf76f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/ApplicationCore/Entities/BasketAggregate/Basket.cs
  2. 3
      src/ApplicationCore/Services/BasketService.cs
  3. 2
      src/Web/Pages/Basket/Index.cshtml
  4. 42
      tests/IntegrationTests/Repositories/BasketRepositoryTests/SetQuantities.cs
  5. 10
      tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs
  6. 9
      tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs
  7. 5
      tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

5
src/ApplicationCore/Entities/BasketAggregate/Basket.cs

@ -25,5 +25,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity; existingItem.Quantity += quantity;
} }
public void RemoveEmptyItems()
{
_items.RemoveAll(i => i.Quantity == 0);
}
} }
} }

3
src/ApplicationCore/Services/BasketService.cs

@ -59,10 +59,11 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{ {
if (quantities.TryGetValue(item.Id.ToString(), out var quantity)) if (quantities.TryGetValue(item.Id.ToString(), out var quantity))
{ {
_logger.LogInformation($"Updating quantity of item ID:{item.Id} to {quantity}."); if(_logger != null) _logger.LogInformation($"Updating quantity of item ID:{item.Id} to {quantity}.");
item.Quantity = quantity; item.Quantity = quantity;
} }
} }
basket.RemoveEmptyItems();
await _basketRepository.UpdateAsync(basket); await _basketRepository.UpdateAsync(basket);
} }

2
src/Web/Pages/Basket/Index.cshtml

@ -35,7 +35,7 @@
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section> <section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2"> <section class="esh-basket-item esh-basket-item--middle col-xs-2">
<input type="hidden" name="@("Items[" + i + "].Key")" value="@item.Id" /> <input type="hidden" name="@("Items[" + i + "].Key")" value="@item.Id" />
<input type="number" class="esh-basket-input" min="1" name="@("Items[" + i + "].Value")" value="@item.Quantity" /> <input type="number" class="esh-basket-input" min="0" name="@("Items[" + i + "].Value")" value="@item.Quantity" />
</section> </section>
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section> <section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
</div> </div>

42
tests/IntegrationTests/Repositories/BasketRepositoryTests/SetQuantities.cs

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using Xunit;
namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketRepositoryTests
{
public class SetQuantities
{
private readonly CatalogContext _catalogContext;
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly BasketBuilder BasketBuilder = new BasketBuilder();
public SetQuantities()
{
var dbOptions = new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_basketRepository = new EfRepository<Basket>(_catalogContext);
}
[Fact]
public async Task RemoveEmptyQuantities()
{
var basket = BasketBuilder.WithOneBasketItem();
var basketService = new BasketService(_basketRepository, null);
await _basketRepository.AddAsync(basket);
_catalogContext.SaveChanges();
await basketService.SetQuantities(BasketBuilder.BasketId, new Dictionary<string, int>() { { BasketBuilder.BasketId.ToString(), 0 } });
Assert.Equal(0, basket.Items.Count);
}
}
}

10
tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs

@ -53,5 +53,15 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
var firstItem = basket.Items.Single(); var firstItem = basket.Items.Single();
Assert.Equal(1, firstItem.Quantity); Assert.Equal(1, firstItem.Quantity);
} }
[Fact]
public void RemoveEmptyItems()
{
var basket = new Basket();
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
basket.RemoveEmptyItems();
Assert.Equal(0, basket.Items.Count);
}
} }
} }

9
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs

@ -5,13 +5,14 @@ using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Moq; using Moq;
using System; using System;
using Xunit; using Xunit;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{ {
public class SetQuantities public class SetQuantities
{ {
private int _invalidId = -1; private readonly int _invalidId = -1;
private Mock<IAsyncRepository<Basket>> _mockBasketRepo; private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public SetQuantities() public SetQuantities()
{ {
@ -19,7 +20,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
} }
[Fact] [Fact]
public async void ThrowsGivenInvalidBasketId() public async Task ThrowsGivenInvalidBasketId()
{ {
var basketService = new BasketService(_mockBasketRepo.Object, null); var basketService = new BasketService(_mockBasketRepo.Object, null);
@ -28,7 +29,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
} }
[Fact] [Fact]
public async void ThrowsGivenNullQuantities() public async Task ThrowsGivenNullQuantities()
{ {
var basketService = new BasketService(null, null); var basketService = new BasketService(null, null);

5
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

@ -1,5 +1,6 @@
using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.ApplicationCore.Services;
using System; using System;
using System.Threading.Tasks;
using Xunit; using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
@ -7,7 +8,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class TransferBasket public class TransferBasket
{ {
[Fact] [Fact]
public async void ThrowsGivenNullAnonymousId() public async Task ThrowsGivenNullAnonymousId()
{ {
var basketService = new BasketService(null, null); var basketService = new BasketService(null, null);
@ -15,7 +16,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
} }
[Fact] [Fact]
public async void ThrowsGivenNullUserId() public async Task ThrowsGivenNullUserId()
{ {
var basketService = new BasketService(null, null); var basketService = new BasketService(null, null);

Loading…
Cancel
Save