Retructured solution
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
namespace LiteCharms.Features.Quotes.Commands;
|
||||
|
||||
public class AssignQuoteToOrderCommand : IRequest<Result>
|
||||
{
|
||||
public Guid OrderId { get; set; }
|
||||
|
||||
public Guid QuoteId { get; set; }
|
||||
|
||||
private AssignQuoteToOrderCommand(Guid orderId, Guid quoteId)
|
||||
{
|
||||
OrderId = orderId;
|
||||
QuoteId = quoteId;
|
||||
}
|
||||
|
||||
public static AssignQuoteToOrderCommand Create(Guid orderId, Guid quoteId)
|
||||
{
|
||||
if(orderId == Guid.Empty)
|
||||
throw new ArgumentException("Order ID is required.", nameof(orderId));
|
||||
|
||||
if(quoteId == Guid.Empty)
|
||||
throw new ArgumentException("Quote ID is required.", nameof(quoteId));
|
||||
|
||||
return new AssignQuoteToOrderCommand(orderId, quoteId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace LiteCharms.Features.Quotes.Commands;
|
||||
|
||||
public class AssignQuoteToShoppingCartCommand : IRequest<Result>
|
||||
{
|
||||
public Guid QuoteId { get; set; }
|
||||
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private AssignQuoteToShoppingCartCommand(Guid quoteId, Guid shoppingCartId)
|
||||
{
|
||||
QuoteId = quoteId;
|
||||
ShoppingCartId = shoppingCartId;
|
||||
}
|
||||
|
||||
public static AssignQuoteToShoppingCartCommand Create(Guid quoteId, Guid shoppingCartId)
|
||||
{
|
||||
if(quoteId == Guid.Empty)
|
||||
throw new ArgumentException("QuoteId cannot be empty.", nameof(quoteId));
|
||||
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException("ShoppingCartId cannot be empty.", nameof(shoppingCartId));
|
||||
|
||||
return new(quoteId, shoppingCartId);
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using LiteCharms.Features.Shop;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Commands;
|
||||
|
||||
public class UpdateQuoteStatusCommand : IRequest<Result>
|
||||
{
|
||||
public Guid QuoteId { get; set; }
|
||||
|
||||
public QuoteStatus Status { get; set; }
|
||||
|
||||
private UpdateQuoteStatusCommand(Guid quoteId, QuoteStatus status)
|
||||
{
|
||||
QuoteId = quoteId;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public static UpdateQuoteStatusCommand Create(Guid quoteId, QuoteStatus status)
|
||||
{
|
||||
if(quoteId == Guid.Empty)
|
||||
throw new ArgumentException("Quote ID cannot be empty.", nameof(quoteId));
|
||||
|
||||
return new(quoteId, status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using LiteCharms.Features.Shop.Customers.Entities;
|
||||
using LiteCharms.Features.Shop.Orders.Entities;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.Entities;
|
||||
|
||||
namespace LiteCharms.Features.Shop.Quotes.Entities;
|
||||
|
||||
[EntityTypeConfiguration<QuoteConfiguration, Quote>]
|
||||
public class Quote : Models.Quote
|
||||
{
|
||||
public virtual Customer? Customer { get; set; }
|
||||
|
||||
public virtual Order? Order { get; set; }
|
||||
|
||||
public virtual ShoppingCart? ShoppingCart { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace LiteCharms.Features.Shop.Quotes.Entities;
|
||||
|
||||
public class QuoteConfiguration : IEntityTypeConfiguration<Quote>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Quote> builder)
|
||||
{
|
||||
builder.ToTable(nameof(Quote));
|
||||
|
||||
builder.HasKey(f => f.Id);
|
||||
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
|
||||
builder.Property(f => f.UpdatedAt).IsRequired(false);
|
||||
builder.Property(f => f.ExpiredAt).IsRequired(false);
|
||||
builder.Property(f => f.CustomerId).IsRequired();
|
||||
builder.Property(f => f.OrderId);
|
||||
builder.Property(f => f.ShoppingCartId);
|
||||
builder.Property(f => f.Status).IsRequired().HasConversion<int>();
|
||||
builder.Property(f => f.InvoiceUrl).IsRequired(false).HasMaxLength(2048);
|
||||
builder.Property(f => f.Reason).IsRequired(false);
|
||||
|
||||
builder.HasOne(q => q.Customer)
|
||||
.WithMany(c => c.Quotes)
|
||||
.HasForeignKey(q => q.CustomerId)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasOne(q => q.Order)
|
||||
.WithOne(o => o.Quote)
|
||||
.HasForeignKey<Quote>(q => q.OrderId);
|
||||
|
||||
builder.HasOne(q => q.ShoppingCart)
|
||||
.WithOne(o => o.Quote)
|
||||
.HasForeignKey<Quote>(q => q.ShoppingCartId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
public class Quote
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
|
||||
public DateTimeOffset? ExpiredAt { get; set; }
|
||||
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
public Guid? OrderId { get; set; }
|
||||
|
||||
public Guid? ShoppingCartId { get; set; }
|
||||
|
||||
public QuoteStatus Status { get; set; }
|
||||
|
||||
public string? InvoiceUrl { get; set; }
|
||||
|
||||
public string? Reason { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetCustomerQuotesQuery : IRequest<Result<Quote[]>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
private GetCustomerQuotesQuery(Guid customerId) => CustomerId = customerId;
|
||||
|
||||
public static GetCustomerQuotesQuery Create(Guid customerId)
|
||||
{
|
||||
if (customerId == Guid.Empty)
|
||||
throw new ArgumentException("CustomerId is required.");
|
||||
|
||||
return new(customerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetQuoteQuery : IRequest<Result<Quote>>
|
||||
{
|
||||
public Guid QuoteId { get; set; }
|
||||
|
||||
private GetQuoteQuery(Guid quoteId) => QuoteId = quoteId;
|
||||
|
||||
public static GetQuoteQuery Create(Guid quoteId)
|
||||
{
|
||||
if(quoteId == Guid.Empty)
|
||||
throw new ArgumentException("Quote ID is required.", nameof(quoteId));
|
||||
|
||||
return new(quoteId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries;
|
||||
|
||||
public class GetQuotesQuery : IRequest<Result<Quote[]>>
|
||||
{
|
||||
public DateOnly From { get; set; }
|
||||
|
||||
public DateOnly To { get; set; }
|
||||
|
||||
public int MaxRecords { get; set; }
|
||||
|
||||
private GetQuotesQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
MaxRecords = maxRecords;
|
||||
}
|
||||
|
||||
public static GetQuotesQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000)
|
||||
{
|
||||
if (from > to)
|
||||
throw new ArgumentException("From date cannot be greater than To date.");
|
||||
|
||||
if (maxRecords <= 0)
|
||||
throw new ArgumentException("MaxRecords must be a positive integer.");
|
||||
|
||||
return new(from, to, maxRecords);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Quotes.Queries;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetCustomerQuotesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetCustomerQuotesQuery, Result<Quote[]>>
|
||||
{
|
||||
public async ValueTask<Result<Quote[]>> Handle(GetCustomerQuotesQuery 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<Quote[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
|
||||
|
||||
var quotes = await context.Quotes.AsNoTracking()
|
||||
.Where(q => q.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
|
||||
|
||||
return quotes?.Length > 0
|
||||
? Result.Ok(quotes.Select(q => q.ToModel()).ToArray())
|
||||
: Result.Fail<Quote[]>(new Error($"No quotes found for customer with Id {request.CustomerId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Quote[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuoteQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetQuoteQuery, Result<Quote>>
|
||||
{
|
||||
public async ValueTask<Result<Quote>> Handle(GetQuoteQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var quote = await context.Quotes.AsNoTracking().FirstOrDefaultAsync(q => q.Id == request.QuoteId, cancellationToken);
|
||||
|
||||
return quote is not null
|
||||
? Result.Ok(quote.ToModel())
|
||||
: Result.Fail<Quote>(new Error($"Quote with ID {request.QuoteId} not found."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Quote>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.Quotes.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuotesHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetQuotesQuery, Result<Quote[]>>
|
||||
{
|
||||
public async ValueTask<Result<Quote[]>> Handle(GetQuotesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = request.From.ToDateTime(TimeOnly.MinValue);
|
||||
var toDate = request.To.ToDateTime(TimeOnly.MaxValue);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var quotes = await context.Quotes.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
|
||||
.Take(request.MaxRecords)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return quotes?.Length > 0
|
||||
? Result.Ok(quotes.Select(o => o.ToModel()).ToArray())
|
||||
: Result.Fail<Quote[]>(new Error($"No quotes found for the specified date range {request.From} - {request.To}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Quote[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user