Retructured solution
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetCustomerShoppingCartsQuery : IRequest<Result<ShoppingCart[]>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
private GetCustomerShoppingCartsQuery(Guid customerId) => CustomerId = customerId;
|
||||
|
||||
public static GetCustomerShoppingCartsQuery Create(Guid customerId)
|
||||
{
|
||||
if(customerId == Guid.Empty)
|
||||
throw new ArgumentException("Customer ID is required.", nameof(customerId));
|
||||
|
||||
return new(customerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetShoppingCartPackagesQuery : IRequest<Result<ShoppingCartPackage[]>>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private GetShoppingCartPackagesQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||
|
||||
public static GetShoppingCartPackagesQuery Create(Guid shoppingCartId)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId));
|
||||
|
||||
return new(shoppingCartId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetShoppingCartQuery : IRequest<Result<ShoppingCart>>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private GetShoppingCartQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||
|
||||
public static GetShoppingCartQuery 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.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
using LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetCustomerShoppingCartsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetCustomerShoppingCartsQuery, Result<ShoppingCart[]>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCart[]>> Handle(GetCustomerShoppingCartsQuery 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<ShoppingCart[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
|
||||
|
||||
var shoppingCarts = await context.ShoppingCarts.Where(sc => sc.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
|
||||
|
||||
return shoppingCarts?.Length > 0
|
||||
? Result.Ok(shoppingCarts.Select(c => c.ToModel()).ToArray())
|
||||
: Result.Fail<ShoppingCart[]>(new Error($"No shopping carts found for customer with Id {request.CustomerId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCart[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetShoppingCartItemsQueryHandler(IDbContextFactory<ShopDbContext> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetShoppingCartPackagesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartPackagesQuery, Result<ShoppingCartPackage[]>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCartPackage[]>> Handle(GetShoppingCartPackagesQuery 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 cart could not be found by ID {request.ShoppingCartId}");
|
||||
|
||||
var packages = await context.ShoppingCartPackages.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(cp => cp.ShoppingCartId == request.ShoppingCartId)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return packages?.Length > 0
|
||||
? Result.Ok(packages.Select(p => p.ToModel()).ToArray())
|
||||
: Result.Fail($"Could not find packaged in shopping cart by ID {request.ShoppingCartId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCartPackage[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetShoppingCartQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartQuery, Result<ShoppingCart>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCart>> Handle(GetShoppingCartQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var cart = await context.ShoppingCarts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
||||
|
||||
return cart is not null
|
||||
? Result.Ok(cart.ToModel())
|
||||
: Result.Fail<ShoppingCart>($"Failed to find shopping cart with id {request.ShoppingCartId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCart>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user