Retructured solution

This commit is contained in:
Khwezi Mngoma
2026-05-13 20:06:24 +02:00
parent 26075cd9a7
commit a42c51d7b2
231 changed files with 1618 additions and 1408 deletions
@@ -0,0 +1,35 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Quotes.Commands.Handlers;
public class AssignQuoteToOrderCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AssignQuoteToOrderCommand, Result>
{
public async ValueTask<Result> Handle(AssignQuoteToOrderCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var order = await context.Orders.FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
if (order is null)
return Result.Fail(new Error($"Order with id {request.OrderId} not found"));
if(!await context.Quotes.AnyAsync(q => q.Id == request.OrderId, cancellationToken))
return Result.Fail(new Error($"Quote with id {request.QuoteId} not found"));
if(order.QuoteId == request.QuoteId)
return Result.Fail(new Error($"Quote with id {request.QuoteId} is already assigned to order with id {request.OrderId}"));
order.QuoteId = request.QuoteId;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to assign quote with id {request.QuoteId} to order with id {request.OrderId}"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,32 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Quotes.Commands.Handlers;
public class AssignQuoteToShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AssignQuoteToShoppingCartCommand, Result>
{
public async ValueTask<Result> Handle(AssignQuoteToShoppingCartCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var shoppingCart = await context.Orders.FirstOrDefaultAsync(o => o.Id == request.ShoppingCartId, cancellationToken);
if (shoppingCart is null)
return Result.Fail(new Error($"ShoppingCart with id {request.ShoppingCartId} not found"));
if(!await context.Quotes.AnyAsync(q => q.Id == request.QuoteId, cancellationToken))
return Result.Fail(new Error($"Quote with id {request.QuoteId} not found"));
shoppingCart.QuoteId = request.QuoteId;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error("Failed to assign quote to shopping cart"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,29 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Quotes.Commands.Handlers;
public class UpdateQuoteStatusCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateQuoteStatusCommand, Result>
{
public async ValueTask<Result> Handle(UpdateQuoteStatusCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var quote = await context.Quotes.FirstOrDefaultAsync(q => q.Id == request.QuoteId, cancellationToken);
if (quote is null)
return Result.Fail(new Error("Quote not found."));
quote.Status = request.Status;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error("Failed to update quote status."));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}