Added notifications
Added shopping cart and items Added quotes Refactored relatinoships Migrated changes Refactored cqrs commands and queries Refactored mappings
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Commands.Handlers;
|
||||
|
||||
public class AssignQuoteToOrderCommandHandler(IDbContextFactory<LeadGeneratorDbContext> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Commands.Handlers;
|
||||
|
||||
public class AssignQuoteToShoppingCartCommandHandler(IDbContextFactory<LeadGeneratorDbContext> 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.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Commands.Handlers;
|
||||
|
||||
public class UpdateQuoteStatusCommandHandler(IDbContextFactory<LeadGeneratorDbContext> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user