Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to remove items from the basket setting quantity to zero #300

Merged
merged 1 commit into from Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ApplicationCore/Entities/BasketAggregate/Basket.cs
Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/ApplicationCore/Services/BasketService.cs
Expand Up @@ -59,10 +59,11 @@ public async Task SetQuantities(int basketId, Dictionary<string, int> 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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Web/Pages/Basket/Index.cshtml
Expand Up @@ -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">
<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 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>
Expand Down
@@ -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);
}
}
}
Expand Up @@ -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);
}
}
}
Expand Up @@ -5,21 +5,22 @@
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<IAsyncRepository<Basket>> _mockBasketRepo;
private readonly int _invalidId = -1;
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;

public SetQuantities()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}

[Fact]
public async void ThrowsGivenInvalidBasketId()
public async Task ThrowsGivenInvalidBasketId()
{
var basketService = new BasketService(_mockBasketRepo.Object, null);

Expand All @@ -28,7 +29,7 @@ public async void ThrowsGivenInvalidBasketId()
}

[Fact]
public async void ThrowsGivenNullQuantities()
public async Task ThrowsGivenNullQuantities()
{
var basketService = new BasketService(null, null);

Expand Down
@@ -1,21 +1,22 @@
using Microsoft.eShopWeb.ApplicationCore.Services;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class TransferBasket
{
[Fact]
public async void ThrowsGivenNullAnonymousId()
public async Task ThrowsGivenNullAnonymousId()
{
var basketService = new BasketService(null, null);

await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
}

[Fact]
public async void ThrowsGivenNullUserId()
public async Task ThrowsGivenNullUserId()
{
var basketService = new BasketService(null, null);

Expand Down