11 changed files with 69 additions and 65 deletions
@ -1,8 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces; |
|||
|
|||
public interface IBasketQueryService |
|||
{ |
|||
Task<int> CountTotalBasketItems(string username); |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
|||
|
|||
namespace Microsoft.eShopWeb.Infrastructure.Data; |
|||
|
|||
public class BasketQueryService : IBasketQueryService |
|||
{ |
|||
private readonly CatalogContext _dbContext; |
|||
|
|||
public BasketQueryService(CatalogContext dbContext) |
|||
{ |
|||
_dbContext = dbContext; |
|||
} |
|||
|
|||
public async Task<int> CountTotalBasketItems(string username) |
|||
{ |
|||
var totalItems = await _dbContext.Baskets |
|||
.Where(basket => basket.BuyerId == username) |
|||
.SelectMany(item => item.Items) |
|||
.SumAsync(sum => sum.Quantity); |
|||
|
|||
return totalItems; |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; |
|||
using Xunit; |
|||
|
|||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests; |
|||
|
|||
public class BasketTotalItems |
|||
{ |
|||
private readonly int _testCatalogItemId = 123; |
|||
private readonly decimal _testUnitPrice = 1.23m; |
|||
private readonly int _testQuantity = 2; |
|||
private readonly string _buyerId = "Test buyerId"; |
|||
|
|||
[Fact] |
|||
public void ReturnsTotalQuantityWithOneItem() |
|||
{ |
|||
var basket = new Basket(_buyerId); |
|||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
|||
|
|||
var result = basket.TotalItems; |
|||
|
|||
Assert.Equal(_testQuantity, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ReturnsTotalQuantityWithMultipleItems() |
|||
{ |
|||
var basket = new Basket(_buyerId); |
|||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity); |
|||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity*2); |
|||
|
|||
var result = basket.TotalItems; |
|||
|
|||
Assert.Equal(_testQuantity*3, result); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue