Completed refactor
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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,40 @@
|
||||
namespace LiteCharms.Features.Shop.Orders.Models;
|
||||
|
||||
public record CreateOrder
|
||||
{
|
||||
public required Guid CustomerId { get; set; }
|
||||
|
||||
public required 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 record UpdateOrder
|
||||
{
|
||||
public required Guid OrderId { get; set; }
|
||||
|
||||
public required OrderStatus Status { get; set; }
|
||||
|
||||
public string? InvoiceUrl { get; set; }
|
||||
|
||||
public string[]? Notes { get; set; }
|
||||
|
||||
public string[]? Requirements { get; set; }
|
||||
}
|
||||
|
||||
public record RefundCustomer
|
||||
{
|
||||
public required Guid OrderId { get; set; }
|
||||
|
||||
public required Guid CustomerId { get; set; }
|
||||
|
||||
public required string Reason { get; set; }
|
||||
|
||||
public required decimal Amount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
using LiteCharms.Features.Extensions;
|
||||
using LiteCharms.Features.Models;
|
||||
using LiteCharms.Features.Shop.Orders.Models;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using static LiteCharms.Features.Extensions.Timezones;
|
||||
|
||||
namespace LiteCharms.Features.Shop.Orders;
|
||||
|
||||
public class OrderService(IDbContextFactory<ShopDbContext> contextFactory)
|
||||
{
|
||||
public async ValueTask<Result<Guid>> CreateOrderAsync(CreateOrder request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
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
|
||||
{
|
||||
Status = OrderStatus.Pending,
|
||||
CustomerId = request.CustomerId,
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<OrderRefund[]>> GetCustomerOrderRefundsAsync(Guid customerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken))
|
||||
return Result.Fail<OrderRefund[]>(new Error($"Customer with Id {customerId} does not exist."));
|
||||
|
||||
var refunds = await context.OrderRefunds.AsNoTracking().AsSplitQuery()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(r => r.Order!.CustomerId == 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 {customerId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<OrderRefund[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Order[]>> GetCustomerOrdersAsync(Guid customerId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Customers.AsNoTracking().AnyAsync(c => c.Id == customerId, cancellationToken))
|
||||
return Result.Fail<Order[]>(new Error($"Customer with Id {customerId} does not exist."));
|
||||
|
||||
var orders = await context.Orders.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(o => o.CustomerId == 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 {customerId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<OrderRefund>> GetOrderRefundAsync(Guid orderId, Guid orderRefundId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var refund = await context.OrderRefunds.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.OrderId == orderId && r.Id == orderRefundId, cancellationToken);
|
||||
|
||||
return refund is not null
|
||||
? Result.Ok(refund.ToModel())
|
||||
: Result.Fail<OrderRefund>(new Error($"Refund {orderRefundId} not found for the given OrderId: {orderId}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<OrderRefund>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<OrderRefund>> GetOrderRefundAsync(Guid orderRefundId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var refund = await context.OrderRefunds.AsNoTracking().FirstOrDefaultAsync(r => r.Id == orderRefundId, cancellationToken);
|
||||
|
||||
return refund is not null
|
||||
? Result.Ok(refund.ToModel())
|
||||
: Result.Fail<OrderRefund>($"Order refund could not be found with id {orderRefundId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<OrderRefund>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<OrderRefund[]>> GetOrderRefundsAsync(Guid orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var refunds = await context.OrderRefunds.AsNoTracking()
|
||||
.OrderByDescending(o => o.CreatedAt)
|
||||
.Where(r => r.OrderId == orderId)
|
||||
.ToArrayAsync(cancellationToken);
|
||||
|
||||
return refunds?.Length > 0
|
||||
? Result.Ok(refunds.Select(r => r.ToModel()).ToArray())
|
||||
: Result.Fail<OrderRefund[]>($"Order refunds could not be found with order id {orderId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<OrderRefund[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Order[]>> GetOrdersAsync(DateRange range, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = range.From.ToDateTime(TimeOnly.MinValue);
|
||||
var toDate = range.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(range.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 {range.From} - {range.To}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Order[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Guid>> RefundCustomerAsync(RefundCustomer request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateOrderRefundAsync(Guid orderRefundId, string reason, decimal amount, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var refund = await context.OrderRefunds.FirstOrDefaultAsync(r => r.Id == orderRefundId, cancellationToken);
|
||||
|
||||
if (refund is null)
|
||||
return Result.Fail($"Order refund not found with id {orderRefundId}");
|
||||
|
||||
refund.Reason = reason;
|
||||
refund.Amount = amount;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail($"Failed to update order refund {orderRefundId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result> UpdateOrderStatusAsync(UpdateOrder request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
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;
|
||||
order.UpdatedAt = SouthAfricanTimeZone.UtcNow();
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(request.InvoiceUrl)) order.InvoiceUrl = request.InvoiceUrl;
|
||||
|
||||
if(request.Requirements?.Length > 0) order.Requirements = request.Requirements;
|
||||
if(request.Notes?.Length > 0) order.Notes = request.Notes;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user