Skip to content

Commit

Permalink
fixbug: add product to basket
Browse files Browse the repository at this point in the history
  • Loading branch information
datsunbae committed Apr 20, 2024
1 parent 4ebf24b commit bceb476
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public AddBasketProductItemCommandHandler(

public async Task<Result<Guid>> Handle(AddBasketProductItemCommand request, CancellationToken cancellationToken)
{
Guid result = Guid.Empty;
Guid userId = _currentUser.GetUserId();
if (userId.Equals(Guid.Empty) is true)
throw new UnauthorizedException("Authentication Failed.");
Expand All @@ -40,19 +41,23 @@ public async Task<Result<Guid>> Handle(AddBasketProductItemCommand request, Canc
if (product is null)
return Result.Failure<Guid>(ProductErrors.NotFound);

Basket basket = await _basketRepository.FirstOrDefaultAsync(new BasketByUserIdWithBasketItemSpec(userId));
Basket basket = await _basketRepository.FirstOrDefaultAsync(new BasketByUserIdWithBasketItemSpec(userId), cancellationToken);
if (basket is null)
{
Basket newBasket = Basket.Create(userId);
newBasket.AddBasketProductItem(request.ProductId, request.Quantity);
await _basketRepository.AddAsync(basket);
await _basketRepository.AddAsync(newBasket);

result = newBasket.Id;
}
else
{
basket.AddBasketProductItem(request.ProductId, request.Quantity);
await _basketRepository.UpdateAsync(basket);

result = basket.Id;
}

return basket.Id;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<Result<BasketResponse>> Handle(GetBasketQuery request, Cancell

var result = await _basketRepository
.FirstOrDefaultAsync(
(ISpecification<Basket, BasketResponse>)new BasketByUserIdWithBasketItemResultSpec(userId), cancellationToken)
(ISpecification<Basket, BasketResponse>)new BasketByUserIdWithBasketItemAndProductResultSpec(userId), cancellationToken)
?? new BasketResponse(userId, 0, Enumerable.Empty<BasketProductItemResponse>());

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace CleanArchitecture.Application.Features.V1.Baskets.Specifications;

public class BasketByUserIdWithBasketItemResultSpec : Specification<Basket, BasketResponse>
public class BasketByUserIdWithBasketItemAndProductResultSpec : Specification<Basket, BasketResponse>
{
public BasketByUserIdWithBasketItemResultSpec(Guid userId) =>
public BasketByUserIdWithBasketItemAndProductResultSpec(Guid userId) =>
Query
.Where(b => b.UserId == userId)
.Include(b => b.BasketProductItems);
.Include(b => b.BasketProductItems)
.ThenInclude(p => p.Product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private Basket(

public Guid UserId { get; private set; }

public IReadOnlyCollection<BasketProductItem> BasketProductItems => _bastketProductItems.AsReadOnly();
public IReadOnlyCollection<BasketProductItem> BasketProductItems => _bastketProductItems;

#region NotMapped

Expand Down

0 comments on commit bceb476

Please sign in to comment.