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,40 @@
namespace LiteCharms.Features.Orders.Commands;
public class CreateOrderCommand : IRequest<Result<Guid>>
{
public Guid CustomerId { get; set; }
public Guid ShoppingCartId { get; set; }
public Guid? QuoteId { get; set; }
public string[]? Requirements { get; set; }
public string[]? Notes { get; set; }
public string[]? Terms { get; set; }
public bool DepositRequired { get; set; }
private CreateOrderCommand(Guid customerId, Guid shoppingCartId, bool depositRequired, Guid? quoteId = null, string[]? requirements = null, string[]? notes = null, string[]? terms = null)
{
CustomerId = customerId;
ShoppingCartId = shoppingCartId;
DepositRequired = depositRequired;
QuoteId = quoteId;
Requirements = requirements;
Notes = notes;
Terms = terms;
}
public static CreateOrderCommand Create(Guid customerId, Guid shoppingCartId, bool depositRequired, Guid? quoteId = null, string[]? requirements = null, string[]? notes = null, string[]? terms = null)
{
if (customerId == Guid.Empty)
throw new ArgumentException("CustomerId is required.", nameof(customerId));
if (shoppingCartId == Guid.Empty)
throw new ArgumentException("ShoppingCartId is required.", nameof(shoppingCartId));
return new(customerId, shoppingCartId, depositRequired, quoteId, requirements, notes, terms);
}
}
@@ -0,0 +1,46 @@
using LiteCharms.Features.Shop;
using LiteCharms.Features.Shop.Postgres;
using LiteCharms.Models;
namespace LiteCharms.Features.Orders.Commands.Handlers;
public class CreateOrderCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<CreateOrderCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(CreateOrderCommand 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>(new Error($"Customer {request.CustomerId} does not exist."));
if(!await context.ShoppingCarts.AnyAsync(sc => sc.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail<Guid>(new Error($"Shopping cart {request.ShoppingCartId} does not exist."));
if(request.QuoteId.HasValue && !await context.Quotes.AnyAsync(q => q.Id == request.QuoteId.Value, cancellationToken))
return Result.Fail<Guid>(new Error($"Quote {request.QuoteId.Value} does not exist."));
var newOrder = context.Orders.Add(new Entities.Order
{
CreatedAt = DateTime.UtcNow,
Status = OrderStatus.Pending,
CustomerId = request.CustomerId,
QuoteId = request.QuoteId,
ShoppingCartId = request.ShoppingCartId,
DepositRequired = request.DepositRequired,
Requirements = request.Requirements,
Notes = request.Notes,
Terms = request.Terms
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(newOrder.Entity.Id)
: Result.Fail<Guid>(new Error($"Failed to create customer {request.CustomerId} order using shopping cart {request.ShoppingCartId}."));
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,29 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Orders.Commands.Handlers;
public class UpdateOrderStatusCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateOrderStatusCommand, Result>
{
public async ValueTask<Result> Handle(UpdateOrderStatusCommand 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 {request.OrderId} not found"));
order.Status = request.Status;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail(new Error($"Failed to update order {request.OrderId}"));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,31 @@
using LiteCharms.Features.Shop;
using LiteCharms.Models;
namespace LiteCharms.Features.Orders.Commands;
public class UpdateOrderStatusCommand : IRequest<Result>
{
public Guid OrderId { get; set; }
public OrderStatus Status { get; set; }
public string? Note { get; set; }
private UpdateOrderStatusCommand(Guid orderId, OrderStatus status, string? note)
{
OrderId = orderId;
Status = status;
Note = note;
}
public static UpdateOrderStatusCommand Create(Guid orderId, OrderStatus status, string? note)
{
if (orderId == Guid.Empty)
throw new ArgumentException("OrderId is required.", nameof(orderId));
if (!Enum.IsDefined(typeof(OrderStatus), status))
throw new ArgumentException("Invalid order status.", nameof(status));
return new(orderId, status, note);
}
}
@@ -0,0 +1,17 @@
using LiteCharms.Features.Shop.Customers.Entities;
using LiteCharms.Features.Shop.Quotes.Entities;
using LiteCharms.Features.Shop.ShoppingCarts.Entities;
namespace LiteCharms.Features.Shop.Orders.Entities;
[EntityTypeConfiguration<OrderConfiguration, Order>]
public class Order : Models.Order
{
public virtual ICollection<OrderRefund>? Refunds { get; set; }
public virtual Customer? Customer { get; set; }
public virtual Quote? Quote { get; set; }
public virtual ShoppingCart? ShoppingCart { get; set; }
}
@@ -0,0 +1,25 @@
namespace LiteCharms.Features.Shop.Orders.Entities;
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.ToTable(nameof(Order));
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).ValueGeneratedOnAdd().HasDefaultValueSql("now()");
builder.Property(f => f.UpdatedAt).IsRequired(false);
builder.Property(f => f.CustomerId).IsRequired();
builder.Property(f => f.Status).HasConversion<int>().IsRequired();
builder.Property(f => f.Requirements).HasColumnType("jsonb").IsRequired(false);
builder.Property(f => f.Notes).HasColumnType("jsonb").IsRequired(false);
builder.Property(f => f.Terms).HasColumnType("jsonb").IsRequired(false);
builder.Property(f => f.InvoiceUrl).IsRequired(false).HasMaxLength(2048);
builder.HasOne(o => o.Customer)
.WithMany(c => c.Orders)
.HasForeignKey(o => o.CustomerId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
}
}
@@ -0,0 +1,7 @@
namespace LiteCharms.Features.Shop.Orders.Entities;
[EntityTypeConfiguration<OrderRefundConfiguration, OrderRefund>]
public class OrderRefund : Models.OrderRefund
{
public virtual Order? Order { get; set; }
}
@@ -0,0 +1,21 @@
namespace LiteCharms.Features.Shop.Orders.Entities;
public class OrderRefundConfiguration : IEntityTypeConfiguration<OrderRefund>
{
public void Configure(EntityTypeBuilder<OrderRefund> builder)
{
builder.ToTable(nameof(OrderRefund));
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).ValueGeneratedOnAdd().HasDefaultValueSql("now()");
builder.Property(f => f.OrderId).IsRequired();
builder.Property(f => f.Reason).IsRequired();
builder.Property(f => f.Amount).IsRequired().HasPrecision(18, 2);
builder.HasOne(r => r.Order)
.WithMany(o => o.Refunds)
.HasForeignKey(r => r.OrderId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,22 @@
namespace LiteCharms.Features.Shop.Orders.Models;
public class Order
{
public Guid Id { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public Guid CustomerId { get; set; }
public OrderStatus Status { get; set; }
public string[]? Requirements { get; set; }
public string[]? Notes { get; set; }
public string[]? Terms { get; set; }
public string? InvoiceUrl { get; set; }
}
@@ -0,0 +1,14 @@
namespace LiteCharms.Features.Shop.Orders.Models;
public class OrderRefund
{
public Guid Id { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public Guid OrderId { get; set; }
public string? Reason { get; set; }
public decimal Amount { get; set; }
}
@@ -0,0 +1,18 @@
using LiteCharms.Features.Shop.Orders.Models;
namespace LiteCharms.Features.Orders.Queries;
public class GetCustomerOrdersQuery : IRequest<Result<Order[]>>
{
public Guid CustomerId { get; }
private GetCustomerOrdersQuery(Guid customerId) => CustomerId = customerId;
public static GetCustomerOrdersQuery Create(Guid customerId)
{
if (customerId == Guid.Empty)
throw new ArgumentException("CustomerId is required.", nameof(customerId));
return new(customerId);
}
}
@@ -0,0 +1,27 @@
using LiteCharms.Features.Shop.Orders.Models;
namespace LiteCharms.Features.Orders.Queries;
public class GetOrderRefundQuery : IRequest<Result<OrderRefund>>
{
public Guid OrderId { get; set; }
public Guid OrderRefundId { get; set; }
private GetOrderRefundQuery(Guid orderId, Guid orderRefundId)
{
OrderId = orderId;
OrderRefundId = orderRefundId;
}
public static GetOrderRefundQuery Create(Guid orderId, Guid orderRefundId)
{
if (orderId == Guid.Empty)
throw new ArgumentException("OrderId is required.", nameof(orderId));
if (orderRefundId == Guid.Empty)
throw new ArgumentException("OrderRefundId is required.", nameof(orderRefundId));
return new(orderId, orderRefundId);
}
}
@@ -0,0 +1,30 @@
using LiteCharms.Features.Shop.Orders.Models;
namespace LiteCharms.Features.Orders.Queries;
public class GetOrdersQuery : IRequest<Result<Order[]>>
{
public DateOnly From { get; set; }
public DateOnly To { get; set; }
public int MaxRecords { get; set; }
private GetOrdersQuery(DateOnly from, DateOnly to, int maxRecords = 1000)
{
From = from;
To = to;
MaxRecords = maxRecords;
}
public static GetOrdersQuery 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,32 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Orders.Queries.Handlers;
public class GetCustomerOrdersQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetCustomerOrdersQuery, Result<Order[]>>
{
public async ValueTask<Result<Order[]>> Handle(GetCustomerOrdersQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if(!await context.Customers.AsNoTracking().AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
return Result.Fail<Order[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
var orders = await context.Orders.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(o => o.CustomerId == request.CustomerId)
.ToArrayAsync(cancellationToken);
return orders?.Length > 0
? Result.Ok(orders.Select(o => o.ToModel()).ToArray())
: Result.Fail<Order[]>(new Error($"No orders found for customer with Id {request.CustomerId}."));
}
catch (Exception ex)
{
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,27 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Orders.Queries.Handlers;
public class GetOrderRefundQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetOrderRefundQuery, Result<OrderRefund>>
{
public async ValueTask<Result<OrderRefund>> Handle(GetOrderRefundQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var refund = await context.OrderRefunds.AsNoTracking()
.FirstOrDefaultAsync(r => r.OrderId == request.OrderId && r.Id == request.OrderRefundId, cancellationToken);
return refund is not null
? Result.Ok(refund.ToModel())
: Result.Fail<OrderRefund>(new Error($"Refund {request.OrderRefundId} not found for the given OrderId: {request.OrderId}"));
}
catch (Exception ex)
{
return Result.Fail<OrderRefund>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,34 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Orders.Queries.Handlers;
public class GetOrdersQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetOrdersQuery, Result<Order[]>>
{
public async ValueTask<Result<Order[]>> Handle(GetOrdersQuery 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 orders = await context.Orders
.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return orders?.Length > 0
? Result.Ok(orders.Select(o => o.ToModel()).ToArray())
: Result.Fail<Order[]>(new Error($"No orders found for the specified date range {request.From} - {request.To}."));
}
catch (Exception ex)
{
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,39 @@
using LiteCharms.Features.Shop.Orders.Refunds.Commands;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Shop.Orders.Refunds.Commands.Handlers;
public class RefundCustomerCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<RefundCustomerCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(RefundCustomerCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if(!await context.Orders.AnyAsync(o => o.Id == request.OrderId, cancellationToken))
return Result.Fail<Guid>(new Error($"Order with Id: {request.OrderId} does not exist"));
if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
return Result.Fail<Guid>(new Error($"Customer with Id: {request.CustomerId} does not exist"));
if(!await context.Orders.AnyAsync(o => o.Id == request.OrderId && o.CustomerId == request.CustomerId, cancellationToken))
return Result.Fail<Guid>(new Error($"Order with Id: {request.OrderId} does not belong to Customer with Id: {request.CustomerId}"));
var refund = context.OrderRefunds.Add(new Entities.OrderRefund
{
OrderId = request.OrderId,
Reason = request.Reason,
Amount = request.Amount
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(refund.Entity.Id)
: Result.Fail<Guid>(new Error($"Failed to create refund for OrderId: {request.OrderId}"));
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,31 @@
using LiteCharms.Features.Shop.Orders.Refunds.Commands;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Shop.Orders.Refunds.Commands.Handlers;
public class UpdateOrderRefundCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateOrderRefundCommand, Result>
{
public async ValueTask<Result> Handle(UpdateOrderRefundCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var refund = await context.OrderRefunds.FirstOrDefaultAsync(r => r.Id == request.OrderRefundId, cancellationToken);
if (refund is null)
return Result.Fail($"Order refund not found with id {request.OrderRefundId}");
refund.Reason = request.Reason;
refund.Amount = request.Amount;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to update order refund {request.OrderRefundId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,38 @@
namespace LiteCharms.Features.Shop.Orders.Refunds.Commands;
public class RefundCustomerCommand : IRequest<Result<Guid>>
{
public Guid OrderId { get; set; }
public Guid CustomerId { get; set; }
public string? Reason { get; set; }
public decimal Amount { get; set; }
private RefundCustomerCommand(Guid orderId, Guid customerId, string? reason, decimal amount)
{
OrderId = orderId;
CustomerId = customerId;
Reason = reason;
Amount = amount;
CustomerId = customerId;
}
public static RefundCustomerCommand Create(Guid orderId, Guid customerId, string? reason, decimal amount)
{
if (orderId == Guid.Empty)
throw new ArgumentException("OrderId is required", nameof(orderId));
if (customerId == Guid.Empty)
throw new ArgumentException("CustomerId is required", nameof(customerId));
if (amount <= 0)
throw new ArgumentException("Amount must be greater than zero", nameof(amount));
if (string.IsNullOrWhiteSpace(reason))
throw new ArgumentException("Reason is required", nameof(reason));
return new(orderId, customerId, reason, amount);
}
}
@@ -0,0 +1,28 @@
namespace LiteCharms.Features.Shop.Orders.Refunds.Commands;
public class UpdateOrderRefundCommand : IRequest<Result>
{
public Guid OrderRefundId { get; set; }
public string? Reason { get; set; }
public decimal Amount { get; set; }
private UpdateOrderRefundCommand(Guid orderRefundId, string? reason, decimal amount)
{
OrderRefundId = orderRefundId;
Reason = reason;
Amount = amount;
}
public static UpdateOrderRefundCommand Create(Guid orderRefundId, string? reason, decimal amount)
{
if (orderRefundId == Guid.Empty)
throw new ArgumentException("Order refund id is required.", nameof(orderRefundId));
if (string.IsNullOrWhiteSpace(reason))
throw new ArgumentException("Refund update reason is required");
return new(orderRefundId, reason, amount);
}
}
@@ -0,0 +1,18 @@
using LiteCharms.Features.Shop.Orders.Models;
namespace LiteCharms.Features.Shop.Orders.Refunds.Queries;
public class GetCustomerRefundsQuery : IRequest<Result<OrderRefund[]>>
{
public Guid CustomerId { get; set; }
private GetCustomerRefundsQuery(Guid customerId) => CustomerId = customerId;
public static GetCustomerRefundsQuery Create(Guid customerId)
{
if (customerId == Guid.Empty)
throw new ArgumentException("CustomerId is required.", nameof(customerId));
return new(customerId);
}
}
@@ -0,0 +1,18 @@
using LiteCharms.Features.Shop.Orders.Models;
namespace LiteCharms.Features.Shop.Orders.Refunds.Queries;
public class GetRefundQuery : IRequest<Result<OrderRefund>>
{
public Guid OrderRefundId { get; set; }
private GetRefundQuery(Guid orderRefundId) => OrderRefundId = orderRefundId;
public static GetRefundQuery Create(Guid orderRefundId)
{
if(orderRefundId == Guid.Empty)
throw new ArgumentException("Customer ID is required.", nameof(orderRefundId));
return new(orderRefundId);
}
}
@@ -0,0 +1,32 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Orders.Refunds.Queries;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Shop.Orders.Refunds.Queries.Handlers;
public class GetCustomerRefundsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetCustomerRefundsQuery, Result<OrderRefund[]>>
{
public async ValueTask<Result<OrderRefund[]>> Handle(GetCustomerRefundsQuery 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<OrderRefund[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
var refunds = await context.OrderRefunds.AsNoTracking().AsSplitQuery()
.OrderByDescending(o => o.CreatedAt)
.Where(r => r.Order!.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
return refunds?.Length > 0
? Result.Ok(refunds.Select(r => r.ToModel()).ToArray())
: Result.Fail<OrderRefund[]>(new Error($"No refunds found for customer with Id {request.CustomerId}."));
}
catch (Exception ex)
{
return Result.Fail<OrderRefund[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,27 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Orders.Refunds.Queries;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Shop.Orders.Refunds.Queries.Handlers;
public class GetRefundQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetRefundQuery, Result<OrderRefund>>
{
public async ValueTask<Result<OrderRefund>> Handle(GetRefundQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var refund = await context.OrderRefunds.AsNoTracking().FirstOrDefaultAsync(r => r.Id == request.OrderRefundId, cancellationToken);
return refund is not null
? Result.Ok(refund.ToModel())
: Result.Fail<OrderRefund>($"Order refund could not be found with id {request.OrderRefundId}");
}
catch (Exception ex)
{
return Result.Fail<OrderRefund>(new Error(ex.Message).CausedBy(ex));
}
}
}