Skip to content

Commit

Permalink
fix: adjust clear favorited products conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroNetto404 committed Aug 4, 2024
1 parent 8ec3abd commit 62359f1
Showing 1 changed file with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,10 @@ public sealed class FavoriteProductDomainService(
IRepository<Customer> customerRepository
)
{
public async Task<Result> ClearFavoritesAsync(Guid customerId, CancellationToken cancellationToken)
{
var customer = await customerRepository.GetByIdAsync(customerId, cancellationToken);
if (customer is null)
{
return DomainErrors.Customer.NotFound;
}

var favoriteProducts = await favoriteProductRepository.ListAsync(
new FavoriteProductsByCustomerSpecification(customerId),
cancellationToken);
if (favoriteProducts.Count != 0)
{
return Result.Ok();
}

await favoriteProductRepository.DeleteRangeAsync(favoriteProducts, cancellationToken);
return Result.Ok();
}

public async Task<Result<FavoriteProduct>> FavoriteAsync(
Guid customerId,
Guid productId,
CancellationToken cancellationToken = default)
Guid customerId,
Guid productId,
CancellationToken cancellationToken = default)
{
var existingFavoriteProduct = await favoriteProductRepository.SingleOrDefaultAsync(
new FavoriteProductByCustomerAndProductSpecification(
Expand Down Expand Up @@ -76,12 +56,32 @@ public async Task<Result<FavoriteProduct>> FavoriteAsync(
: DomainErrors.FavoriteProduct.NotSaved;
}

public async Task<Result> ClearFavoritesAsync(Guid customerId, CancellationToken cancellationToken)
{
var customer = await customerRepository.GetByIdAsync(customerId, cancellationToken);
if (customer is null)
{
return DomainErrors.Customer.NotFound;
}

var favoriteProducts = await favoriteProductRepository.ListAsync(
new FavoriteProductsByCustomerSpecification(customer.Id),
cancellationToken);
if (favoriteProducts.Count == 0)
{
return Result.Ok();
}

await favoriteProductRepository.DeleteRangeAsync(favoriteProducts, cancellationToken);
return Result.Ok();
}

public async Task<Result> UnfavoriteAsync(
Guid customerId,
Guid productId,
CancellationToken cancellationToken = default)
{
var favoriteProduct = await favoriteProductRepository.FirstOrDefaultAsync(
var favoriteProduct = await favoriteProductRepository.SingleOrDefaultAsync(
new FavoriteProductByCustomerAndProductSpecification(
customerId,
productId),
Expand Down

0 comments on commit 62359f1

Please sign in to comment.