From 70e009bda20734e726af9c4c78ee82b8aa86780e Mon Sep 17 00:00:00 2001 From: Ovi Date: Wed, 11 Sep 2019 09:16:00 -0400 Subject: [PATCH] Allow to remove items from the basket setting quantity to zero --- .../Entities/BasketAggregate/Basket.cs | 5 +++ src/ApplicationCore/Services/BasketService.cs | 3 +- src/Web/Pages/Basket/Index.cshtml | 2 +- .../BasketRepositoryTests/SetQuantities.cs | 42 +++++++++++++++++++ .../Entities/BasketTests/BasketAddItem.cs | 10 +++++ .../BasketServiceTests/SetQuantities.cs | 11 ++--- .../BasketServiceTests/TransferBasket.cs | 5 ++- 7 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 tests/IntegrationTests/Repositories/BasketRepositoryTests/SetQuantities.cs diff --git a/src/ApplicationCore/Entities/BasketAggregate/Basket.cs b/src/ApplicationCore/Entities/BasketAggregate/Basket.cs index 6713266b9..7b89bd543 100644 --- a/src/ApplicationCore/Entities/BasketAggregate/Basket.cs +++ b/src/ApplicationCore/Entities/BasketAggregate/Basket.cs @@ -25,5 +25,10 @@ public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); existingItem.Quantity += quantity; } + + public void RemoveEmptyItems() + { + _items.RemoveAll(i => i.Quantity == 0); + } } } diff --git a/src/ApplicationCore/Services/BasketService.cs b/src/ApplicationCore/Services/BasketService.cs index add38eaf1..118ec4379 100644 --- a/src/ApplicationCore/Services/BasketService.cs +++ b/src/ApplicationCore/Services/BasketService.cs @@ -59,10 +59,11 @@ public async Task SetQuantities(int basketId, Dictionary quantities { 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; } } + basket.RemoveEmptyItems(); await _basketRepository.UpdateAsync(basket); } diff --git a/src/Web/Pages/Basket/Index.cshtml b/src/Web/Pages/Basket/Index.cshtml index c28e46225..9a5126c46 100644 --- a/src/Web/Pages/Basket/Index.cshtml +++ b/src/Web/Pages/Basket/Index.cshtml @@ -35,7 +35,7 @@
$ @item.UnitPrice.ToString("N2")
- +
$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")
diff --git a/tests/IntegrationTests/Repositories/BasketRepositoryTests/SetQuantities.cs b/tests/IntegrationTests/Repositories/BasketRepositoryTests/SetQuantities.cs new file mode 100644 index 000000000..5a027e981 --- /dev/null +++ b/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 _basketRepository; + private readonly BasketBuilder BasketBuilder = new BasketBuilder(); + + public SetQuantities() + { + var dbOptions = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestCatalog") + .Options; + _catalogContext = new CatalogContext(dbOptions); + _basketRepository = new EfRepository(_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() { { BasketBuilder.BasketId.ToString(), 0 } }); + + Assert.Equal(0, basket.Items.Count); + } + } +} diff --git a/tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs b/tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs index 6df225402..173461c93 100644 --- a/tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs +++ b/tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketAddItem.cs @@ -53,5 +53,15 @@ public void DefaultsToQuantityOfOne() var firstItem = basket.Items.Single(); 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); + } } } diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs index 4c8943a4f..6f90db16a 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.cs @@ -5,13 +5,14 @@ using Moq; using System; using Xunit; - +using System.Threading.Tasks; + namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests { public class SetQuantities { - private int _invalidId = -1; - private Mock> _mockBasketRepo; + private readonly int _invalidId = -1; + private readonly Mock> _mockBasketRepo; public SetQuantities() { @@ -19,7 +20,7 @@ public SetQuantities() } [Fact] - public async void ThrowsGivenInvalidBasketId() + public async Task ThrowsGivenInvalidBasketId() { var basketService = new BasketService(_mockBasketRepo.Object, null); @@ -28,7 +29,7 @@ public async void ThrowsGivenInvalidBasketId() } [Fact] - public async void ThrowsGivenNullQuantities() + public async Task ThrowsGivenNullQuantities() { var basketService = new BasketService(null, null); diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs index 3b6fd9b81..69314743c 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs @@ -1,5 +1,6 @@ using Microsoft.eShopWeb.ApplicationCore.Services; using System; +using System.Threading.Tasks; using Xunit; namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests @@ -7,7 +8,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes public class TransferBasket { [Fact] - public async void ThrowsGivenNullAnonymousId() + public async Task ThrowsGivenNullAnonymousId() { var basketService = new BasketService(null, null); @@ -15,7 +16,7 @@ public async void ThrowsGivenNullAnonymousId() } [Fact] - public async void ThrowsGivenNullUserId() + public async Task ThrowsGivenNullUserId() { var basketService = new BasketService(null, null);