Browse Source
* replace counter by specific query to avoid load each items in memory * Create IBasketQueryServicemain
committed by
GitHub
7 changed files with 50 additions and 129 deletions
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces |
||||
|
{ |
||||
|
public interface IBasketQueryService |
||||
|
{ |
||||
|
Task<int> CountTotalBasketItems(string username); |
||||
|
} |
||||
|
} |
||||
@ -1,11 +0,0 @@ |
|||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; |
|
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces |
|
||||
{ |
|
||||
|
|
||||
//public interface IOrderRepository : IAsyncRepository<Order>
|
|
||||
//{
|
|
||||
// Task<Order> GetByIdWithItemsAsync(int id);
|
|
||||
//}
|
|
||||
} |
|
||||
@ -0,0 +1,27 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,22 +0,0 @@ |
|||||
using Microsoft.EntityFrameworkCore; |
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; |
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
|
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data |
|
||||
{ |
|
||||
//public class OrderRepository : EfRepository<Order>, IOrderRepository
|
|
||||
//{
|
|
||||
// public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
|
||||
// {
|
|
||||
// }
|
|
||||
|
|
||||
// public Task<Order> GetByIdWithItemsAsync(int id)
|
|
||||
// {
|
|
||||
// return _dbContext.Orders
|
|
||||
// .Include(o => o.OrderItems)
|
|
||||
// .ThenInclude(i => i.ItemOrdered)
|
|
||||
// .FirstOrDefaultAsync(x => x.Id == id);
|
|
||||
// }
|
|
||||
//}
|
|
||||
} |
|
||||
Loading…
Reference in new issue