@ -2,12 +2,9 @@
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Authorization ;
using Volo.Abp.Users ;
using EShopOnAbp.CatalogService.Products ;
using Microsoft.Extensions.Logging ;
using Volo.Abp ;
using Volo.Abp.EventBus.Distributed ;
namespace EShopOnAbp.BasketService ;
@ -15,59 +12,37 @@ public class BasketAppService : BasketServiceAppService, IBasketAppService
{
private readonly IBasketRepository _ basketRepository ;
private readonly IBasketProductService _ basketProductService ;
private readonly IDistributedEventBus _d istributedEventBus ;
public BasketAppService (
IBasketRepository basketRepository ,
IBasketProductService basketProductService ,
IDistributedEventBus distributedEventBus )
IBasketProductService basketProductService )
{
_ basketRepository = basketRepository ;
_ basketProductService = basketProductService ;
_d istributedEventBus = distributedEventBus ;
}
public async Task < BasketDto > GetAsync ( )
public async Task < BasketDto > GetAsync ( Guid ? anonymousUserId )
{
var basket = await _ basketRepository . GetAsync ( CurrentUser . GetId ( ) ) ;
return await GetBasketDtoAsync ( basket ) ;
}
if ( anonymousUserId ! = null & & CurrentUser . IsAuthenticated )
{
var userBasket = await _ basketRepository . GetAsync ( CurrentUser . GetId ( ) ) ;
var anonymousUserBasket = await _ basketRepository . GetAsync ( anonymousUserId . Value ) ;
public async Task < BasketDto > GetByAnonymousUserIdAsync ( Guid id )
{
var basket = await _ basketRepository . GetAsync ( id ) ;
return await GetBasketDtoAsync ( basket ) ;
}
userBasket . Merge ( anonymousUserBasket ) ;
await _ basketRepository . UpdateAsync ( userBasket ) ;
public async Task < BasketDto > MergeBasketsAsync ( )
{
//TODO: move to custom shared project
var anonymousUserIdString = CurrentUser . FindClaimValue ( "anonymous_id" ) ;
if ( ! Guid . TryParse ( anonymousUserIdString , out Guid anonymousUserId ) )
{
Logger . LogError ( $"Couldn't parse anonymous Id from claim!{anonymousUserIdString}" ) ;
}
anonymousUserBasket . Clear ( ) ;
await _ basketRepository . UpdateAsync ( anonymousUserBasket ) ;
Basket anonymousUserBasket = await _ basketRepository . GetAsync ( anonymousUserId ) ;
if ( ! CurrentUser . IsAuthenticated )
{
Logger . LogWarning ( $"User is not authenticated! Merging baskets failed!" ) ;
return await GetBasketDtoAsync ( anonymousUserBasket ) ;
return await GetBasketDtoAsync ( userBasket ) ;
}
var userBasket = await _ basketRepository . GetAsync ( CurrentUser . GetId ( ) ) ;
foreach ( var item in anonymousUserBasket . Items )
if ( anonymousUserId ! = null & & ! CurrentUser . IsAuthenticated )
{
userBasket . AddProduct ( item . ProductId , item . Count ) ;
return await GetBasketDtoAsync ( await _ basketRepository . GetAsync ( anonymousUserId . Value ) ) ;
}
await _ basketRepository . UpdateAsync ( userBasket ) ;
anonymousUserBasket . Clear ( ) ;
await _ basketRepository . UpdateAsync ( anonymousUserBasket ) ;
return await GetBasketDtoAsync ( userBasket ) ;
return await GetBasketDtoAsync ( await _ basketRepository . GetAsync ( CurrentUser . GetId ( ) ) ) ;
}
public async Task < BasketDto > AddProductAsync ( AddProductDto input )