Basket : Improve, reduce, remove duplication call to database (#610)
This commit is contained in:
@@ -7,17 +7,5 @@ namespace Microsoft.eShopWeb.ApplicationCore.Exceptions
|
||||
public BasketNotFoundException(int basketId) : base($"No basket found with id {basketId}")
|
||||
{
|
||||
}
|
||||
|
||||
protected BasketNotFoundException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
|
||||
public BasketNotFoundException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public BasketNotFoundException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||
@@ -6,8 +7,8 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||
public interface IBasketService
|
||||
{
|
||||
Task TransferBasketAsync(string anonymousId, string userName);
|
||||
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1);
|
||||
Task SetQuantities(int basketId, Dictionary<string, int> quantities);
|
||||
Task<Basket> AddItemToBasket(string username, int catalogItemId, decimal price, int quantity = 1);
|
||||
Task<Basket> SetQuantities(int basketId, Dictionary<string, int> quantities);
|
||||
Task DeleteBasketAsync(int basketId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,15 +19,21 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
|
||||
public async Task<Basket> AddItemToBasket(string username, int catalogItemId, decimal price, int quantity = 1)
|
||||
{
|
||||
var basketSpec = new BasketWithItemsSpecification(basketId);
|
||||
var basketSpec = new BasketWithItemsSpecification(username);
|
||||
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
|
||||
Guard.Against.NullBasket(basketId, basket);
|
||||
|
||||
if (basket == null)
|
||||
{
|
||||
basket = new Basket(username);
|
||||
await _basketRepository.AddAsync(basket);
|
||||
}
|
||||
|
||||
basket.AddItem(catalogItemId, price, quantity);
|
||||
|
||||
await _basketRepository.UpdateAsync(basket);
|
||||
return basket;
|
||||
}
|
||||
|
||||
public async Task DeleteBasketAsync(int basketId)
|
||||
@@ -36,7 +42,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
await _basketRepository.DeleteAsync(basket);
|
||||
}
|
||||
|
||||
public async Task SetQuantities(int basketId, Dictionary<string, int> quantities)
|
||||
public async Task<Basket> SetQuantities(int basketId, Dictionary<string, int> quantities)
|
||||
{
|
||||
Guard.Against.Null(quantities, nameof(quantities));
|
||||
var basketSpec = new BasketWithItemsSpecification(basketId);
|
||||
@@ -53,6 +59,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
}
|
||||
basket.RemoveEmptyItems();
|
||||
await _basketRepository.UpdateAsync(basket);
|
||||
return basket;
|
||||
}
|
||||
|
||||
public async Task TransferBasketAsync(string anonymousId, string userName)
|
||||
|
||||
Reference in New Issue
Block a user