Implemented shopping carts functionality elements
This commit is contained in:
@@ -61,8 +61,4 @@
|
||||
<ProjectReference Include="..\LiteCharms.Infrastructure\LiteCharms.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ShoppingCarts\Commands\Handlers\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class AddItemToShoppingCartCommand : IRequest<Result>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
public Guid ProductPriceId { get; set; }
|
||||
|
||||
public int Quantity { get; set; }
|
||||
|
||||
private AddItemToShoppingCartCommand(Guid shoppingCartId, Guid productPriceId, int quantity = 1)
|
||||
{
|
||||
ShoppingCartId = shoppingCartId;
|
||||
ProductPriceId = productPriceId;
|
||||
Quantity = quantity;
|
||||
}
|
||||
|
||||
public static AddItemToShoppingCartCommand Create(Guid shoppingCartId, Guid productPriceId, int quantity = 1)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
|
||||
|
||||
if (productPriceId == Guid.Empty)
|
||||
throw new ArgumentException($"Product item required", nameof(productPriceId));
|
||||
|
||||
if(quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity));
|
||||
|
||||
return new(shoppingCartId, productPriceId, quantity);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,25 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class CreateShoppingCartCommand : IRequest<Result>
|
||||
public class CreateShoppingCartCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
public Guid? CustomerId { get; set; }
|
||||
|
||||
public Guid? OrderId { get; set; }
|
||||
|
||||
public Guid? QuoteId { get; set; }
|
||||
|
||||
private CreateShoppingCartCommand(Guid customerId, Guid? orderId = null, Guid? quoteId = null)
|
||||
{
|
||||
CustomerId = customerId;
|
||||
OrderId = orderId;
|
||||
QuoteId = quoteId;
|
||||
}
|
||||
|
||||
public static CreateShoppingCartCommand Create(Guid customerId, Guid? orderId = null, Guid? quoteId = null)
|
||||
{
|
||||
if (customerId == Guid.Empty)
|
||||
throw new ArgumentException($"Customer ID is required", nameof(customerId));
|
||||
|
||||
return new(customerId, orderId, quoteId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class EmptyShoppingCartCommand : IRequest<Result>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private EmptyShoppingCartCommand(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||
|
||||
public static EmptyShoppingCartCommand Create(Guid shoppingCartId)
|
||||
{
|
||||
if(shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart ID is required.", nameof(shoppingCartId));
|
||||
|
||||
return new(shoppingCartId);
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||
|
||||
public class AddItemToShoppingCartCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<AddItemToShoppingCartCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(AddItemToShoppingCartCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ProductPriceId, cancellationToken))
|
||||
return Result.Fail($"Product item could not be found with id {request.ProductPriceId}");
|
||||
|
||||
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
||||
|
||||
if (cart is null)
|
||||
return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}");
|
||||
|
||||
if (cart.ShoppingCartItems?.Any(i => i.ProductPriceId == request.ProductPriceId) == true)
|
||||
return Result.Fail($"Item already in shopping cart with id {request.ShoppingCartId}");
|
||||
|
||||
context.ShoppingCartItems.Add(new Entities.ShoppingCartItem
|
||||
{
|
||||
ShoppingCartId = request.ShoppingCartId,
|
||||
ProductPriceId = request.ProductPriceId,
|
||||
Quantity = request.Quantity
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to add cart item with id {request.ProductPriceId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||
|
||||
public class CreateShoppingCartCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateShoppingCartCommand, Result<Guid>>
|
||||
{
|
||||
public async ValueTask<Result<Guid>> Handle(CreateShoppingCartCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
|
||||
return Result.Fail<Guid>($"Customer could not be found with id {request.CustomerId}");
|
||||
|
||||
var cart = context.ShoppingCarts.Add(new Entities.ShoppingCart
|
||||
{
|
||||
CustomerId = request.CustomerId,
|
||||
OrderId = request.OrderId,
|
||||
QuoteId = request.QuoteId
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(cart.Entity.Id)
|
||||
: Result.Fail<Guid>($"Failed to create shopping cart for customer id {request.CustomerId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||
|
||||
public class EmptyShoppingCartCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<EmptyShoppingCartCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(EmptyShoppingCartCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
|
||||
return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}");
|
||||
|
||||
if (await context.ShoppingCartItems.CountAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken) == 0)
|
||||
return Result.Ok();
|
||||
|
||||
var cartItems = await context.ShoppingCartItems.Where(i => i.ShoppingCartId == request.ShoppingCartId).ToListAsync(cancellationToken);
|
||||
|
||||
context.RemoveRange(cartItems);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Could not empty cart with id {request.ShoppingCartId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||
|
||||
public class RemoveShoppingCartItemCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<RemoveShoppingCartItemCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(RemoveShoppingCartItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ShoppingCartItemId, cancellationToken))
|
||||
return Result.Fail($"Product item could not be found with id {request.ShoppingCartItemId}");
|
||||
|
||||
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
||||
|
||||
if (cart is null)
|
||||
return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartId}");
|
||||
|
||||
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.Id == request.ShoppingCartItemId, cancellationToken);
|
||||
|
||||
if (item is null) return Result.Ok();
|
||||
|
||||
context.ShoppingCartItems.Remove(item);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to remove shopping cart item with id {request.ShoppingCartItemId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
|
||||
|
||||
public class UpdateShoppingCartItemCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateShoppingCartItemCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(UpdateShoppingCartItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
|
||||
return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}");
|
||||
|
||||
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken);
|
||||
|
||||
if(item is null)
|
||||
return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartItemId}");
|
||||
|
||||
item.Quantity = request.Quantity;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to update cart item quntity");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class RemoveShoppingCartItemCommand : IRequest<Result>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
public Guid ShoppingCartItemId { get; set; }
|
||||
|
||||
private RemoveShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId)
|
||||
{
|
||||
ShoppingCartId = shoppingCartId;
|
||||
ShoppingCartItemId = shoppingCartItemId;
|
||||
}
|
||||
|
||||
public static RemoveShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
|
||||
|
||||
if (shoppingCartItemId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart item required", nameof(shoppingCartItemId));
|
||||
|
||||
return new(shoppingCartId, shoppingCartItemId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class UpdateShoppingCartItemCommand : IRequest<Result>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
public Guid ShoppingCartItemId { get; set; }
|
||||
|
||||
public int Quantity { get; set; }
|
||||
|
||||
private UpdateShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1)
|
||||
{
|
||||
ShoppingCartId = shoppingCartId;
|
||||
ShoppingCartItemId = shoppingCartItemId;
|
||||
Quantity = quantity;
|
||||
}
|
||||
|
||||
public static UpdateShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
|
||||
|
||||
if (shoppingCartItemId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart item is required", nameof(shoppingCartItemId));
|
||||
|
||||
if (quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity));
|
||||
|
||||
return new(shoppingCartId, shoppingCartItemId, quantity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetShoppingCartItemsQuery : IRequest<Result<ShoppingCartItem[]>>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private GetShoppingCartItemsQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||
|
||||
public static GetShoppingCartItemsQuery Create(Guid shoppingCartId)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId));
|
||||
|
||||
return new(shoppingCartId);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetShoppingCartItemsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetShoppingCartItemsQuery, Result<ShoppingCartItem[]>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCartItem[]>> Handle(GetShoppingCartItemsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.ShoppingCarts.AnyAsync(i => i.Id == request.ShoppingCartId, cancellationToken))
|
||||
return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}");
|
||||
|
||||
var items = await context.ShoppingCartItems.AsNoTracking()
|
||||
.Where(i => i.ShoppingCartId == request.ShoppingCartId).ToArrayAsync(cancellationToken);
|
||||
|
||||
return items?.Length > 0
|
||||
? Result.Ok(items.Select(i => i.ToModel()).ToArray())
|
||||
: Result.Fail<ShoppingCartItem[]>($"Failed to retrieve shopping cart items with id {request.ShoppingCartId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCartItem[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user