6 changed files with 65 additions and 23 deletions
@ -0,0 +1,12 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces; |
|||
|
|||
/// <summary>
|
|||
/// Specific query used to fetch count without running in memory
|
|||
/// </summary>
|
|||
public interface IBasketQueryService |
|||
{ |
|||
Task<int> CountTotalBasketItems(string username); |
|||
} |
|||
|
|||
@ -0,0 +1,31 @@ |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
|||
|
|||
namespace Microsoft.eShopWeb.Infrastructure.Data.Queries; |
|||
|
|||
public class BasketQueryService : IBasketQueryService |
|||
{ |
|||
private readonly CatalogContext _dbContext; |
|||
|
|||
public BasketQueryService(CatalogContext dbContext) |
|||
{ |
|||
_dbContext = dbContext; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method performs the sum on the database rather than in memory
|
|||
/// </summary>
|
|||
/// <param name="username"></param>
|
|||
/// <returns></returns>
|
|||
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; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue