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,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries;
|
||||
|
||||
public class GetCustomerQuery : IRequest<Result<Customer>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
private GetCustomerQuery(Guid customerId) => CustomerId = customerId;
|
||||
|
||||
public static GetCustomerQuery Create(Guid customerId)
|
||||
{
|
||||
if(customerId == Guid.Empty)
|
||||
throw new ArgumentException("Customer ID is required.", nameof(customerId));
|
||||
|
||||
return new(customerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries.Handlers;
|
||||
|
||||
public class GetCustomerQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerQuery, Result<Customer>>
|
||||
{
|
||||
public async ValueTask<Result<Customer>> Handle(GetCustomerQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken);
|
||||
|
||||
return customer is not null
|
||||
? Result.Ok(customer.ToModel())
|
||||
: Result.Fail<Customer>($"Customer not found with id {request.CustomerId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Customer>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,4 +61,8 @@
|
||||
<ProjectReference Include="..\LiteCharms.Infrastructure\LiteCharms.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ShoppingCarts\Commands\Handlers\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Commands;
|
||||
|
||||
public class CreateNotificationCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
public NotificationDirection Direction { get; set; }
|
||||
|
||||
public string? Author { get; set; }
|
||||
|
||||
public string? Title { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
|
||||
public string? Platform { get; set; }
|
||||
|
||||
public string? PlatformAddress { get; set; }
|
||||
|
||||
public string? CorrelationId { get; set; }
|
||||
|
||||
public string? CorrelationIdType { get; set; }
|
||||
|
||||
public bool IsInternal { get; set; }
|
||||
|
||||
private CreateNotificationCommand(NotificationDirection direction, string author, string title, string description, string platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
|
||||
{
|
||||
Direction = direction;
|
||||
Author = author;
|
||||
Title = title;
|
||||
Description = description;
|
||||
Platform = platform;
|
||||
PlatformAddress = platformAddress;
|
||||
CorrelationId = correlationId;
|
||||
CorrelationIdType = correlationIdType;
|
||||
IsInternal = isInternal;
|
||||
}
|
||||
|
||||
public static CreateNotificationCommand Create(NotificationDirection direction, string author, string title, string description, string platform, string platformAddress, string correlationId, string correlationIdType, bool isInternal)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(author))
|
||||
throw new ArgumentException("Author cannot be null or whitespace.", nameof(author));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(title))
|
||||
throw new ArgumentException("Title cannot be null or whitespace.", nameof(title));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(description))
|
||||
throw new ArgumentException("Description cannot be null or whitespace.", nameof(description));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(platform))
|
||||
throw new ArgumentException("Platform cannot be null or whitespace.", nameof(platform));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(platformAddress))
|
||||
throw new ArgumentException("PlatformAddress cannot be null or whitespace.", nameof(platformAddress));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(correlationId))
|
||||
throw new ArgumentException("CorrelationId cannot be null or whitespace.", nameof(correlationId));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(correlationIdType))
|
||||
throw new ArgumentException("CorrelationIdType cannot be null or whitespace.", nameof(correlationIdType));
|
||||
|
||||
return new(direction, author, title, description, platform, platformAddress, correlationId, correlationIdType, isInternal);
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Commands.Handlers;
|
||||
|
||||
public class CreateNotificationCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateNotificationCommand, Result<Guid>>
|
||||
{
|
||||
public async ValueTask<Result<Guid>> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var newNotification = context.Notifications.Add(new Entities.Notification
|
||||
{
|
||||
Direction = request.Direction,
|
||||
Author = request.Author,
|
||||
Title = request.Title,
|
||||
Description = request.Description,
|
||||
Platform = request.Platform,
|
||||
PlatformAddress = request.PlatformAddress,
|
||||
CorrelationId = request.CorrelationId,
|
||||
CorrelationIdType = request.CorrelationIdType,
|
||||
IsInternal = request.IsInternal,
|
||||
});
|
||||
|
||||
return newNotification is not null
|
||||
? Result.Ok(newNotification.Entity.Id)
|
||||
: Result.Fail(new Error("Failed to create notification"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Commands.Handlers;
|
||||
|
||||
public class UpdateNotificationCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateNotificationCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(UpdateNotificationCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken);
|
||||
|
||||
if(notification is null)
|
||||
return Result.Fail(new Error($"Notification with id {request.NotificationId} not found."));
|
||||
|
||||
notification.Processed = request.Processed;
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok()
|
||||
: Result.Fail(new Error($"Failed to update notification with id {request.NotificationId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace LiteCharms.Features.Notifications.Commands;
|
||||
|
||||
public class UpdateNotificationCommand : IRequest<Result>
|
||||
{
|
||||
public Guid NotificationId { get; set; }
|
||||
|
||||
public bool Processed { get; set; }
|
||||
|
||||
private UpdateNotificationCommand(Guid notificationId, bool processed)
|
||||
{
|
||||
NotificationId = notificationId;
|
||||
Processed = processed;
|
||||
}
|
||||
|
||||
public static UpdateNotificationCommand Create(Guid notificationId, bool processed)
|
||||
{
|
||||
if(notificationId == Guid.Empty)
|
||||
throw new ArgumentException("Notification ID cannot be empty.", nameof(notificationId));
|
||||
|
||||
return new(notificationId, processed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Queries;
|
||||
|
||||
public class GetNotificationQuery : IRequest<Result<Notification>>
|
||||
{
|
||||
public Guid NotificationId { get; set; }
|
||||
|
||||
private GetNotificationQuery(Guid notificationId) => NotificationId = notificationId;
|
||||
|
||||
public static GetNotificationQuery Create(Guid notificationId)
|
||||
{
|
||||
if (notificationId == Guid.Empty)
|
||||
throw new ArgumentException("Notification ID is required.", nameof(notificationId));
|
||||
|
||||
return new(notificationId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Notifications.Queries.Handlers;
|
||||
|
||||
public class GetNotificationQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetNotificationQuery, Result<Notification>>
|
||||
{
|
||||
public async ValueTask<Result<Notification>> Handle(GetNotificationQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var notification = await context.Notifications.FindAsync(new object[] { request.NotificationId }, cancellationToken);
|
||||
|
||||
return notification is not null
|
||||
? Result.Ok(notification.ToModel())
|
||||
: Result.Fail<Notification>(new Error($"Notification with id {request.NotificationId} not found"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<Notification>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class GetNotificationsQueryHandler(IDbContextFactory<LeadGeneratorDbConte
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var notifications = await context.Notifications
|
||||
var notifications = await context.Notifications.AsNoTracking()
|
||||
.Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate)
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Take(request.MaxRecords)
|
||||
|
||||
@@ -4,22 +4,25 @@ public class CreateOrderCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
public Guid ProductPriceId { get; set; }
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private CreateOrderCommand(Guid customerId, Guid productPriceId)
|
||||
public Guid? QuoteId { get; set; }
|
||||
|
||||
private CreateOrderCommand(Guid customerId, Guid shoppingCartId, Guid? quoteId = null)
|
||||
{
|
||||
CustomerId = customerId;
|
||||
ProductPriceId = productPriceId;
|
||||
ShoppingCartId = shoppingCartId;
|
||||
QuoteId = quoteId;
|
||||
}
|
||||
|
||||
public static CreateOrderCommand Create(Guid customerId, Guid productPriceId)
|
||||
public static CreateOrderCommand Create(Guid customerId, Guid shoppingCartId, Guid? quoteId = null)
|
||||
{
|
||||
if (customerId == Guid.Empty)
|
||||
throw new ArgumentException("CustomerId is required.", nameof(customerId));
|
||||
|
||||
if (productPriceId == Guid.Empty)
|
||||
throw new ArgumentException("ProductPriceId is required.", nameof(productPriceId));
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException("ShoppingCartId is required.", nameof(shoppingCartId));
|
||||
|
||||
return new(customerId, productPriceId);
|
||||
return new(customerId, shoppingCartId, quoteId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,16 +10,26 @@ public class CreateOrderCommandHandler(IDbContextFactory<LeadGeneratorDbContext>
|
||||
{
|
||||
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
|
||||
{
|
||||
CustomerId = request.CustomerId,
|
||||
ProductPriceId = request.ProductPriceId,
|
||||
ShoppingCartId = request.ShoppingCartId,
|
||||
QuoteId = request.QuoteId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Result.Ok(newOrder.Entity.Id)
|
||||
: Result.Fail<Guid>(new Error($"Failed to create customer {request.CustomerId} order using product price {request.ProductPriceId}."));
|
||||
: Result.Fail<Guid>(new Error($"Failed to create customer {request.CustomerId} order using shopping cart {request.ShoppingCartId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
+2
-2
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace LiteCharms.Features.Orders.Commands.Handlers;
|
||||
|
||||
public class UpdateOrderCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateOrderCommand, Result>
|
||||
public class UpdateOrderStatusCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<UpdateOrderStatusCommand, Result>
|
||||
{
|
||||
public async ValueTask<Result> Handle(UpdateOrderCommand request, CancellationToken cancellationToken)
|
||||
public async ValueTask<Result> Handle(UpdateOrderStatusCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
+3
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace LiteCharms.Features.Orders.Commands;
|
||||
|
||||
public class UpdateOrderCommand : IRequest<Result>
|
||||
public class UpdateOrderStatusCommand : IRequest<Result>
|
||||
{
|
||||
public Guid OrderId { get; set; }
|
||||
|
||||
@@ -10,14 +10,14 @@ public class UpdateOrderCommand : IRequest<Result>
|
||||
|
||||
public string? Note { get; set; }
|
||||
|
||||
private UpdateOrderCommand(Guid orderId, OrderStatus status, string? note)
|
||||
private UpdateOrderStatusCommand(Guid orderId, OrderStatus status, string? note)
|
||||
{
|
||||
OrderId = orderId;
|
||||
Status = status;
|
||||
Note = note;
|
||||
}
|
||||
|
||||
public static UpdateOrderCommand Create(Guid orderId, OrderStatus status, string? note)
|
||||
public static UpdateOrderStatusCommand Create(Guid orderId, OrderStatus status, string? note)
|
||||
{
|
||||
if (orderId == Guid.Empty)
|
||||
throw new ArgumentException("OrderId is required.", nameof(orderId));
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries;
|
||||
namespace LiteCharms.Features.Orders.Queries;
|
||||
|
||||
public class GetCustomerOrdersQuery : IRequest<Result<Order[]>>
|
||||
{
|
||||
+4
-1
@@ -2,7 +2,7 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries.Handlers;
|
||||
namespace LiteCharms.Features.Orders.Queries.Handlers;
|
||||
|
||||
public class GetCustomerOrdersQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerOrdersQuery, Result<Order[]>>
|
||||
{
|
||||
@@ -12,6 +12,9 @@ public class GetCustomerOrdersQueryHandler(IDbContextFactory<LeadGeneratorDbCont
|
||||
{
|
||||
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)
|
||||
@@ -12,6 +12,9 @@ public class GetProductPriceQueryHandler(IDbContextFactory<LeadGeneratorDbContex
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if(!await context.Products.AnyAsync(p => p.Id == request.ProductId, cancellationToken))
|
||||
return Result.Fail<ProductPrice>(new Error($"Product {request.ProductId} not found."));
|
||||
|
||||
var productPrice = await context.ProductPrices.AsNoTracking()
|
||||
.Where(pp => pp.ProductId == request.ProductId && pp.Active)
|
||||
.OrderByDescending(pp => pp.CreatedAt)
|
||||
|
||||
@@ -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.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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,18 @@
|
||||
using LiteCharms.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.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.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.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetCustomerQuotesQueryHandler(IDbContextFactory<LeadGeneratorDbContext> 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.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuoteQueryHandler(IDbContextFactory<LeadGeneratorDbContext> 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.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Quotes.Queries.Handlers;
|
||||
|
||||
public class GetQuotesHandler(IDbContextFactory<LeadGeneratorDbContext> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Commands.Handlers;
|
||||
namespace LiteCharms.Features.Refunds.Commands.Handlers;
|
||||
|
||||
public class RefundCustomerCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<RefundCustomerCommand, Result<Guid>>
|
||||
{
|
||||
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Features.Refunds.Commands.Handlers;
|
||||
|
||||
public class UpdateOrderRefundCommandHandler(IDbContextFactory<LeadGeneratorDbContext> 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
-1
@@ -1,4 +1,4 @@
|
||||
namespace LiteCharms.Features.Customers.Commands;
|
||||
namespace LiteCharms.Features.Refunds.Commands;
|
||||
|
||||
public class RefundCustomerCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace LiteCharms.Features.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
-1
@@ -1,6 +1,6 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries;
|
||||
namespace LiteCharms.Features.Refunds.Queries;
|
||||
|
||||
public class GetCustomerRefundsQuery : IRequest<Result<OrderRefund[]>>
|
||||
{
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.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);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Refunds.Queries;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Customers.Queries.Handlers;
|
||||
namespace LiteCharms.Features.Refunds.Queries.Handlers;
|
||||
|
||||
public class GetCustomerRefundsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerRefundsQuery, Result<OrderRefund[]>>
|
||||
{
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.Refunds.Queries.Handlers;
|
||||
|
||||
public class GetRefundQueryHandler(IDbContextFactory<LeadGeneratorDbContext> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace LiteCharms.Features.ShoppingCarts.Commands;
|
||||
|
||||
public class CreateShoppingCartCommand : IRequest<Result>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetCustomerShoppingCartsQuery : IRequest<Result<ShoppingCart[]>>
|
||||
{
|
||||
public Guid CustomerId { get; set; }
|
||||
|
||||
private GetCustomerShoppingCartsQuery(Guid customerId) => CustomerId = customerId;
|
||||
|
||||
public static GetCustomerShoppingCartsQuery Create(Guid customerId)
|
||||
{
|
||||
if(customerId == Guid.Empty)
|
||||
throw new ArgumentException("Customer ID is required.", nameof(customerId));
|
||||
|
||||
return new(customerId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries;
|
||||
|
||||
public class GetShoppingCartQuery : IRequest<Result<ShoppingCart>>
|
||||
{
|
||||
public Guid ShoppingCartId { get; set; }
|
||||
|
||||
private GetShoppingCartQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
|
||||
|
||||
public static GetShoppingCartQuery Create(Guid shoppingCartId)
|
||||
{
|
||||
if (shoppingCartId == Guid.Empty)
|
||||
throw new ArgumentException($"Shopping cart id is required", nameof(shoppingCartId));
|
||||
|
||||
return new(shoppingCartId);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.ShoppingCarts.Queries;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetCustomerShoppingCartsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerShoppingCartsQuery, Result<ShoppingCart[]>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCart[]>> Handle(GetCustomerShoppingCartsQuery 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<ShoppingCart[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
|
||||
|
||||
var shoppingCarts = await context.ShoppingCarts.Where(sc => sc.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
|
||||
|
||||
return shoppingCarts?.Length > 0
|
||||
? Result.Ok(shoppingCarts.Select(c => c.ToModel()).ToArray())
|
||||
: Result.Fail<ShoppingCart[]>(new Error($"No shopping carts found for customer with Id {request.CustomerId}."));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCart[]>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
||||
|
||||
public class GetShoppingCartQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetShoppingCartQuery, Result<ShoppingCart>>
|
||||
{
|
||||
public async ValueTask<Result<ShoppingCart>> Handle(GetShoppingCartQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var cart = await context.ShoppingCarts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
||||
|
||||
return cart is not null
|
||||
? Result.Ok(cart.ToModel())
|
||||
: Result.Fail<ShoppingCart>($"Failed to find shopping cart with id {request.ShoppingCartId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail<ShoppingCart>(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user