From a0546fb45f4b5a97117acf19771ce5a6e3bc4e23 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Dec 2021 15:59:55 +0300 Subject: [PATCH] basket service update for anonymous users --- .../BasketAppService.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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)