diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs index c7e06212..c404ccb6 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketAppService.cs @@ -15,6 +15,7 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService private readonly IBasketRepository _basketRepository; private readonly IBasketProductService _basketProductService; private readonly IDistributedEventBus _distributedEventBus; + private Guid _anonymousUserId { get; set; } public BasketAppService( IBasketRepository basketRepository, @@ -35,12 +36,15 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService public async Task GetByAnonymousUserIdAsync(Guid id) { var basket = await _basketRepository.GetAsync(id); + _anonymousUserId = id; return await GetBasketDtoAsync(basket); } public async Task AddProductAsync(AddProductDto input) { - var basket = await _basketRepository.GetAsync(CurrentUser.GetId()); + Guid userId = CurrentUser.IsAuthenticated ? CurrentUser.GetId() : _anonymousUserId; + + var basket = await _basketRepository.GetAsync(userId); var product = await _basketProductService.GetAsync(input.ProductId); if (basket.GetProductCount(product.Id) >= product.StockCount)