From 134d8429c00e8a3f91bab7676b5ce408ce84c31a Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Thu, 14 May 2026 01:33:21 +0200 Subject: [PATCH] Completed refactor --- LiteCharms.Features.Tests/CommonFixture.cs | 1 + .../NotificationsFeatureTests.cs | 32 +- LiteCharms.Features/Email/EmailService.cs | 8 +- .../SendShopEmailEnquiryEventHandler.cs | 35 +- LiteCharms.Features/Email/IEmailService.cs | 15 - .../Setup.cs => Extensions/Email.cs} | 9 +- .../Extensions/EntityModeMappers.cs | 30 +- LiteCharms.Features/Extensions/Shop.cs | 27 ++ LiteCharms.Features/Models/DateRange.cs | 10 + .../Commands/AddPackageItemsCommand.cs | 25 -- .../Commands/CreatePackageCommand.cs | 23 -- .../Commands/DeletePackageCommand.cs | 25 -- .../Commands/DeletePackageItemsCommand.cs | 16 - .../Handlers/AddPackageItemCommandHandler.cs | 38 --- .../Handlers/CreatePackageCommandHandler.cs | 32 -- .../DeletePackageItemCommandHandler.cs | 32 -- .../DeletePackageItemsCommandHandler.cs | 29 -- .../Handlers/UpdatePackageCommandHandler.cs | 33 -- .../UpdatePackageStatusCommandHandler.cs | 29 -- .../Commands/UpdatePackageCommand.cs | 28 -- .../Commands/UpdatePackageStatusCommand.cs | 22 -- .../Shop/CartPackages/PackageService.cs | 243 ++++++++++++++ .../Queries/GetPackageItemsQuery.cs | 18 -- .../CartPackages/Queries/GetPackageQuery.cs | 18 -- .../CartPackages/Queries/GetPackagesQuery.cs | 33 -- .../Handlers/GetPackageItemsQueryHandler.cs | 32 -- .../Handlers/GetPackageQueryHandler.cs | 27 -- .../Handlers/GetPackagesQueryHandler.cs | 35 -- .../Commands/CreateCustomerCommand.cs | 64 ---- .../Handlers/CreateCustomerCommandHandler.cs | 48 --- .../Handlers/UpdateCustomerCommandHandler.cs | 44 --- .../Commands/UpdateCustomerCommand.cs | 70 ---- .../Shop/Customers/CustomerService.cs | 132 ++++++++ .../Shop/Customers/Models/Records.cs | 73 +++++ .../Customers/Queries/GetCustomerQuery.cs | 18 -- .../Customers/Queries/GetCustomersQuery.cs | 30 -- .../Handlers/GetCustomerQueryHandler.cs | 26 -- .../Handlers/GetCustomersQueryHandler.cs | 33 -- .../Shop/Leads/Commands/CreateLeadCommand.cs | 55 ---- .../Handlers/CreateLeadCommandHandler.cs | 47 --- .../Handlers/UpdateLeadCommandHandler.cs | 29 -- .../Shop/Leads/Commands/UpdateLeadCommand.cs | 28 -- LiteCharms.Features/Shop/Leads/LeadService.cs | 116 +++++++ .../Shop/Leads/Models/Records.cs | 28 ++ .../Leads/Queries/GetCustomerLeadsQuery.cs | 30 -- .../Shop/Leads/Queries/GetLeadsQuery.cs | 30 -- .../Handlers/GetCustomerLeadsQueryHandler.cs | 33 -- .../Queries/Handlers/GetLeadsQueryHandler.cs | 33 -- .../Commands/CreateNotificationCommand.cs | 73 ----- .../CreateNotificationCommandHandler.cs | 40 --- .../UpdateNotificationCommandHandler.cs | 35 -- .../Commands/UpdateNotificationCommand.cs | 28 -- .../Entities/NotificationConfiguration.cs | 4 +- .../ProcessEmailNotificationsEventHandler.cs | 64 +++- .../Events/ProcessEmailNotificationsEvent.cs | 2 +- .../Notifications/INotificationService.cs | 9 - .../Shop/Notifications/Models/Notification.cs | 4 +- .../Shop/Notifications/Models/Records.cs | 20 +- .../Shop/Notifications/NotificationService.cs | 114 ++++++- .../Queries/GetNotificationQuery.cs | 18 -- .../Queries/GetNotificationsQuery.cs | 30 -- .../Handlers/GetNotificationQueryHandler.cs | 26 -- .../Handlers/GetNotificationsQueryHandler.cs | 33 -- .../Orders/Commands/CreateOrderCommand.cs | 40 --- .../Handlers/CreateOrderCommandHandler.cs | 46 --- .../UpdateOrderStatusCommandHandler.cs | 29 -- .../Commands/UpdateOrderStatusCommand.cs | 31 -- .../Shop/Orders/Models/Records.cs | 40 +++ .../Shop/Orders/OrderService.cs | 260 +++++++++++++++ .../Orders/Queries/GetCustomerOrdersQuery.cs | 18 -- .../Orders/Queries/GetOrderRefundQuery.cs | 27 -- .../Shop/Orders/Queries/GetOrdersQuery.cs | 30 -- .../Handlers/GetCustomerOrdersQueryHandler.cs | 32 -- .../Handlers/GetOrderRefundQueryHandler.cs | 27 -- .../Queries/Handlers/GetOrdersQueryHandler.cs | 34 -- .../Handlers/RefundCustomerCommandHandler.cs | 39 --- .../UpdateOrderRefundCommandHandler.cs | 31 -- .../Refunds/Commands/RefundCustomerCommand.cs | 38 --- .../Commands/UpdateOrderRefundCommand.cs | 28 -- .../Queries/GetCustomerRefundsQuery.cs | 18 -- .../Orders/Refunds/Queries/GetRefundQuery.cs | 18 -- .../GetCustomerRefundsQueryHandler.cs | 32 -- .../Queries/Handlers/GetRefundQueryHandler.cs | 27 -- .../Shop/Products/Models/Records.cs | 14 + .../Shop/Products/ProductService.cs | 229 ++++++++++++++ .../Products/Queries/GetProductPriceQuery.cs | 18 -- .../Products/Queries/GetProductPricesQuery.cs | 18 -- .../Shop/Products/Queries/GetProductQuery.cs | 18 -- .../Shop/Products/Queries/GetProductsQuery.cs | 18 -- .../Handlers/GetProductPriceQueryHandler.cs | 32 -- .../Handlers/GetProductPricesQueryHandler.cs | 27 -- .../Handlers/GetProductQueryHandler.cs | 26 -- .../Handlers/GetProductsQueryHandler.cs | 27 -- .../Commands/AssignQuoteToOrderCommand.cs | 25 -- .../AssignQuoteToShoppingCartCommand.cs | 25 -- .../AssignQuoteToOrderCommandHandler.cs | 35 -- ...AssignQuoteToShoppingCartCommandHandler.cs | 32 -- .../UpdateQuoteStatusCommandHandler.cs | 29 -- .../Commands/UpdateQuoteStatusCommand.cs | 25 -- .../Quotes/Queries/GetCustomerQuotesQuery.cs | 18 -- .../Shop/Quotes/Queries/GetQuoteQuery.cs | 18 -- .../Shop/Quotes/Queries/GetQuotesQuery.cs | 30 -- .../Handlers/GetCustomerQuotesQueryHandler.cs | 31 -- .../Queries/Handlers/GetQuoteQueryHandler.cs | 26 -- .../Queries/Handlers/GetQuotesHandler.cs | 33 -- .../Shop/Quotes/QuoteService.cs | 154 +++++++++ .../Commands/AddItemToShoppingCartCommand.cs | 30 -- .../AddPackageToShoppingCartCommand.cs | 25 -- .../Commands/CreateShoppingCartCommand.cs | 25 -- .../Commands/EmptyShoppingCartCommand.cs | 16 - .../AddItemToShoppingCartCommandHandler.cs | 40 --- .../AddPackageToShoppingCartCommandHandler.cs | 39 --- .../CreateShoppingCartCommandHandler.cs | 32 -- .../EmptyShoppingCartCommandHandler.cs | 32 -- ...vePackageFromShoppingCartCommandHandler.cs | 35 -- .../RemoveShoppingCartItemCommandHandler.cs | 36 --- .../UpdateShoppingCartItemCommandHandler.cs | 32 -- .../RemovePackageFromShoppingCartCommand.cs | 25 -- .../Commands/RemoveShoppingCartItemCommand.cs | 25 -- .../Commands/UpdateShoppingCartItemCommand.cs | 30 -- .../Queries/GetCustomerShoppingCartsQuery.cs | 18 -- .../Queries/GetShoppingCartItemsQuery.cs | 18 -- .../Queries/GetShoppingCartPackagesQuery.cs | 18 -- .../Queries/GetShoppingCartQuery.cs | 18 -- .../GetCustomerShoppingCartsQueryHandler.cs | 30 -- .../GetShoppingCartItemsQueryHandler.cs | 30 -- .../GetShoppingCartPackagesQueryHandler.cs | 32 -- .../Handlers/GetShoppingCartQueryHandler.cs | 26 -- .../Shop/ShoppingCarts/ShoppingCartService.cs | 298 ++++++++++++++++++ 129 files changed, 1870 insertions(+), 3165 deletions(-) delete mode 100644 LiteCharms.Features/Email/IEmailService.cs rename LiteCharms.Features/{Email/Extensions/Setup.cs => Extensions/Email.cs} (69%) create mode 100644 LiteCharms.Features/Extensions/Shop.cs create mode 100644 LiteCharms.Features/Models/DateRange.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/AddPackageItemsCommand.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/CreatePackageCommand.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageCommand.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageItemsCommand.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/AddPackageItemCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/CreatePackageCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemsCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageStatusCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageCommand.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageStatusCommand.cs create mode 100644 LiteCharms.Features/Shop/CartPackages/PackageService.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/GetPackageItemsQuery.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/GetPackageQuery.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/GetPackagesQuery.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageItemsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackagesQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Commands/CreateCustomerCommand.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Commands/Handlers/CreateCustomerCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Commands/Handlers/UpdateCustomerCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Commands/UpdateCustomerCommand.cs create mode 100644 LiteCharms.Features/Shop/Customers/CustomerService.cs create mode 100644 LiteCharms.Features/Shop/Customers/Models/Records.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Queries/GetCustomerQuery.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Queries/GetCustomersQuery.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomerQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomersQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Commands/CreateLeadCommand.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Commands/Handlers/CreateLeadCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Commands/Handlers/UpdateLeadCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Commands/UpdateLeadCommand.cs create mode 100644 LiteCharms.Features/Shop/Leads/LeadService.cs create mode 100644 LiteCharms.Features/Shop/Leads/Models/Records.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Queries/GetCustomerLeadsQuery.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Queries/GetLeadsQuery.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Queries/Handlers/GetCustomerLeadsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Leads/Queries/Handlers/GetLeadsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Commands/CreateNotificationCommand.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Commands/Handlers/CreateNotificationCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Commands/Handlers/UpdateNotificationCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Commands/UpdateNotificationCommand.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/INotificationService.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Queries/GetNotificationQuery.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Queries/GetNotificationsQuery.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Commands/CreateOrderCommand.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Commands/Handlers/CreateOrderCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Commands/Handlers/UpdateOrderStatusCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Commands/UpdateOrderStatusCommand.cs create mode 100644 LiteCharms.Features/Shop/Orders/Models/Records.cs create mode 100644 LiteCharms.Features/Shop/Orders/OrderService.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/GetCustomerOrdersQuery.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/GetOrderRefundQuery.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/GetOrdersQuery.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/Handlers/GetCustomerOrdersQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrderRefundQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrdersQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/RefundCustomerCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/UpdateOrderRefundCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Commands/RefundCustomerCommand.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Commands/UpdateOrderRefundCommand.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Queries/GetCustomerRefundsQuery.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Queries/GetRefundQuery.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetCustomerRefundsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetRefundQueryHandler.cs create mode 100644 LiteCharms.Features/Shop/Products/Models/Records.cs create mode 100644 LiteCharms.Features/Shop/Products/ProductService.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/GetProductPriceQuery.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/GetProductPricesQuery.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/GetProductQuery.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/GetProductsQuery.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPriceQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPricesQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToOrderCommand.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToOrderCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/Handlers/UpdateQuoteStatusCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Commands/UpdateQuoteStatusCommand.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/GetCustomerQuotesQuery.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/GetQuoteQuery.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/GetQuotesQuery.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetCustomerQuotesQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuoteQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuotesHandler.cs create mode 100644 LiteCharms.Features/Shop/Quotes/QuoteService.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/AddItemToShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/AddPackageToShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/CreateShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/EmptyShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddItemToShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddPackageToShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/CreateShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/EmptyShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemovePackageFromShoppingCartCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemoveShoppingCartItemCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/UpdateShoppingCartItemCommandHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/RemovePackageFromShoppingCartCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/RemoveShoppingCartItemCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Commands/UpdateShoppingCartItemCommand.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/GetCustomerShoppingCartsQuery.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartItemsQuery.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartPackagesQuery.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartQuery.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetCustomerShoppingCartsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartItemsQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartPackagesQueryHandler.cs delete mode 100644 LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartQueryHandler.cs create mode 100644 LiteCharms.Features/Shop/ShoppingCarts/ShoppingCartService.cs diff --git a/LiteCharms.Features.Tests/CommonFixture.cs b/LiteCharms.Features.Tests/CommonFixture.cs index 358aaf2..a73bed2 100644 --- a/LiteCharms.Features.Tests/CommonFixture.cs +++ b/LiteCharms.Features.Tests/CommonFixture.cs @@ -22,6 +22,7 @@ public class CommonFixture : IDisposable Services = new ServiceCollection() .AddMediator() .AddLogging() + .AddShopServices() .AddEmailServiceBus() .AddShopDatabase(Configuration) .AddEmailServices(Configuration) diff --git a/LiteCharms.Features.Tests/NotificationsFeatureTests.cs b/LiteCharms.Features.Tests/NotificationsFeatureTests.cs index 53b8154..ede7fd8 100644 --- a/LiteCharms.Features.Tests/NotificationsFeatureTests.cs +++ b/LiteCharms.Features.Tests/NotificationsFeatureTests.cs @@ -1,19 +1,35 @@ -using LiteCharms.Features.Notifications.Commands; +using LiteCharms.Features.Shop.Notifications; namespace LiteCharms.Features.Tests; -public class NotificationsFeatureTests(CommonFixture fixture) : IClassFixture +public class NotificationsFeatureTests(CommonFixture fixture, ITestOutputHelper output) : IClassFixture { + private readonly NotificationService notificationService = fixture.Services.GetRequiredService(); + [Fact] public async Task CreateNotificationCommand_ShouldSucceed() { - var command = CreateNotification.Create(Models.NotificationDirection.Outgoing, "UnitTest", "khwezi@mngoma.co.za", - "CreateNotificationCommand_ShouldSucceed Test", "Test Message", Models.NotificationPlatforms.Email, Models.Priorities.Medium, - "Khngisa Shop - Test", "shop@litecharms.co.za", Guid.NewGuid().ToString(), Models.CorrelationIdTypes.None, - true, false); + Shop.Notifications.Models.CreateNotification request = new() + { + CorrelationId = Guid.CreateVersion7().ToString(), + CorrelationIdType = Shop.CorrelationIdTypes.None, + Direction = Shop.NotificationDirection.Outgoing, + Platform = Shop.NotificationPlatforms.Email, + Priority = Shop.Priorities.Medium, + Sender = "xUnit Test", + SenderAddress = "khwezi@mngoma.africa", + Recipient = $"{Email.Extensions.Constants.ShopEmailFromName} [Test]", + RecipientAddress = Email.Extensions.Constants.ShopEmailFromAddress, + Subject = "Test Message", + Message = "This is an automation test", + IsHtml = false, + IsInternal = true, + }; - var result = await fixture.Mediator.Send(command); + var createResult = await notificationService.CreateNotificationAsync(request); - Assert.True(result.IsSuccess); + Assert.True(createResult.IsSuccess); + + foreach (var error in createResult.Errors) output.WriteLine(error.Message); } } diff --git a/LiteCharms.Features/Email/EmailService.cs b/LiteCharms.Features/Email/EmailService.cs index 8cb3a82..3e6c103 100644 --- a/LiteCharms.Features/Email/EmailService.cs +++ b/LiteCharms.Features/Email/EmailService.cs @@ -5,7 +5,7 @@ using LiteCharms.Features.Shop; namespace LiteCharms.Features.Email; -public class EmailService(IOptions options) : IEmailService +public class EmailService(IOptions options) : IDisposable { private readonly SmtpSettings settings = options.Value; private readonly SmtpClient client = new(); @@ -14,7 +14,7 @@ public class EmailService(IOptions options) : IEmailService public EmailStatuses Status { get; private set; } = EmailStatuses.Disconnected; - public async Task> SendEmailAsync(Message message, CancellationToken cancellationToken = default) + public async ValueTask> SendEmailAsync(Message message, CancellationToken cancellationToken = default) { using var activity = EmailTelemetry.Source.StartActivity("Email Send"); activity?.SetTag("email.recipient", message.Recipient?.Address); @@ -100,7 +100,7 @@ public class EmailService(IOptions options) : IEmailService } } - public async Task> ConnectAsync(CancellationToken cancellationToken = default) + public async ValueTask> ConnectAsync(CancellationToken cancellationToken = default) { using var activity = EmailTelemetry.Source.StartActivity("Email Connect"); activity?.SetTag("email.smtp.connect", settings.Host); @@ -153,7 +153,7 @@ public class EmailService(IOptions options) : IEmailService } } - public async Task DisconnectAsync(CancellationToken cancellationToken = default) + public async ValueTask DisconnectAsync(CancellationToken cancellationToken = default) { using var activity = EmailTelemetry.Source.StartActivity("Email Disconnect"); activity?.SetTag("email.smtp.disconnect", settings.Host); diff --git a/LiteCharms.Features/Email/Events/Handlers/SendShopEmailEnquiryEventHandler.cs b/LiteCharms.Features/Email/Events/Handlers/SendShopEmailEnquiryEventHandler.cs index f3e703f..f4173a6 100644 --- a/LiteCharms.Features/Email/Events/Handlers/SendShopEmailEnquiryEventHandler.cs +++ b/LiteCharms.Features/Email/Events/Handlers/SendShopEmailEnquiryEventHandler.cs @@ -1,22 +1,27 @@ -using LiteCharms.Features.Notifications.Commands; -using LiteCharms.Features.Shop; +using LiteCharms.Features.Shop; +using LiteCharms.Features.Shop.Notifications; using static LiteCharms.Features.Email.Extensions.Constants; namespace LiteCharms.Features.Email.Events.Handlers; -// TODO: Inject the INotificationService -public class SendShopEmailEnquiryEventHandler(ISender mediator) : +public class SendShopEmailEnquiryEventHandler(NotificationService notificationService) : INotificationHandler { - public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken) - { - // TODO: Refactor this to use the NotificationService - var command = CreateNotification.Create(NotificationDirection.Outgoing, notification.SenderName!, - notification.SenderAddress!, notification.Subject!, notification.Message!, NotificationPlatforms.Email, - notification.Priority, ShopEmailFromName, ShopEmailFromAddress, Guid.CreateVersion7().ToString(), - CorrelationIdTypes.None, isInternal: true, isHtml: false); - - // TODO: Remove, deprecated - await mediator.Send(command, cancellationToken); - } + public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken) => + await notificationService.CreateNotificationAsync(new Shop.Notifications.Models.CreateNotification + { + CorrelationId = notification.CorrelationId, + CorrelationIdType = CorrelationIdTypes.None, + Direction = NotificationDirection.Outgoing, + IsHtml = false, + IsInternal = true, + Message = notification.Message, + Platform = NotificationPlatforms.Email, + Priority = notification.Priority, + Subject = notification.Subject!, + Sender = notification.SenderName!, + SenderAddress = notification.SenderAddress!, + Recipient = ShopEmailFromName, + RecipientAddress = ShopEmailFromAddress + }, cancellationToken); } diff --git a/LiteCharms.Features/Email/IEmailService.cs b/LiteCharms.Features/Email/IEmailService.cs deleted file mode 100644 index bda6a6f..0000000 --- a/LiteCharms.Features/Email/IEmailService.cs +++ /dev/null @@ -1,15 +0,0 @@ -using LiteCharms.Features.Email.Models; -using LiteCharms.Features.Shop; - -namespace LiteCharms.Features.Email; - -public interface IEmailService : IDisposable -{ - EmailStatuses Status { get; } - - Task> SendEmailAsync(Message message, CancellationToken cancellationToken = default); - - Task> ConnectAsync(CancellationToken cancellationToken = default); - - Task DisconnectAsync(CancellationToken cancellationToken = default); -} diff --git a/LiteCharms.Features/Email/Extensions/Setup.cs b/LiteCharms.Features/Extensions/Email.cs similarity index 69% rename from LiteCharms.Features/Email/Extensions/Setup.cs rename to LiteCharms.Features/Extensions/Email.cs index f937dda..ae3756d 100644 --- a/LiteCharms.Features/Email/Extensions/Setup.cs +++ b/LiteCharms.Features/Extensions/Email.cs @@ -1,14 +1,15 @@ -using LiteCharms.Features.Email.Configuration; +using LiteCharms.Features.Email; +using LiteCharms.Features.Email.Configuration; -namespace LiteCharms.Features.Email.Extensions; +namespace LiteCharms.Features.Extensions; -public static class Setup +public static class Email { public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration) { services.Configure(configuration.GetSection("Email")); - services.AddSingleton(); + services.AddSingleton(); services.AddOpenTelemetry() .WithTracing(tracing => tracing.AddSource("LiteCharms.EmailService")) diff --git a/LiteCharms.Features/Extensions/EntityModeMappers.cs b/LiteCharms.Features/Extensions/EntityModeMappers.cs index baeb69c..d73a028 100644 --- a/LiteCharms.Features/Extensions/EntityModeMappers.cs +++ b/LiteCharms.Features/Extensions/EntityModeMappers.cs @@ -11,7 +11,7 @@ namespace LiteCharms.Features.Extensions; public static class EntityModeMappers { - public static ShoppingCartPackage ToModel(this Shop.ShoppingCarts.Entities.ShoppingCartPackage entity) => + public static ShoppingCartPackage ToModel(this Features.Shop.ShoppingCarts.Entities.ShoppingCartPackage entity) => new() { Id = entity.Id, @@ -20,7 +20,7 @@ public static class EntityModeMappers ShoppingCartId = entity.ShoppingCartId }; - public static PackageItem ToModel(this Shop.CartPackages.Entities.PackageItem entity) => + public static PackageItem ToModel(this Features.Shop.CartPackages.Entities.PackageItem entity) => new() { Id = entity.Id, @@ -30,7 +30,7 @@ public static class EntityModeMappers ProductPriceId = entity.ProductPriceId }; - public static Package ToModel(this Shop.CartPackages.Entities.Package entity) => + public static Package ToModel(this Features.Shop.CartPackages.Entities.Package entity) => new() { Id = entity.Id, @@ -43,7 +43,7 @@ public static class EntityModeMappers Summary = entity.Summary }; - public static ShoppingCartItem ToModel(this Shop.ShoppingCarts.Entities.ShoppingCartItem entity) => + public static ShoppingCartItem ToModel(this Features.Shop.ShoppingCarts.Entities.ShoppingCartItem entity) => new() { Id = entity.Id, @@ -54,7 +54,7 @@ public static class EntityModeMappers ShoppingCartId = entity.ShoppingCartId }; - public static ShoppingCart ToModel(this Shop.ShoppingCarts.Entities.ShoppingCart entity) => + public static ShoppingCart ToModel(this Features.Shop.ShoppingCarts.Entities.ShoppingCart entity) => new() { Id = entity.Id, @@ -64,7 +64,7 @@ public static class EntityModeMappers OrderId = entity.OrderId }; - public static Quote ToModel(this Shop.Quotes.Entities.Quote entity) => + public static Quote ToModel(this Features.Shop.Quotes.Entities.Quote entity) => new() { Id = entity.Id, @@ -79,7 +79,7 @@ public static class EntityModeMappers OrderId = entity.OrderId }; - public static Notification ToModel(this Shop.Notifications.Entities.Notification entity) => + public static Notification ToModel(this Features.Shop.Notifications.Entities.Notification entity) => new() { Id = entity.Id, @@ -89,9 +89,9 @@ public static class EntityModeMappers CorrelationId = entity.CorrelationId, CorrelationIdType = entity.CorrelationIdType, IsInternal = entity.IsInternal, - Sender = entity.Sender, + SenderAddress = entity.SenderAddress, Platform = entity.Platform, - Recipient = entity.Recipient, + RecipientName = entity.RecipientName, Subject = entity.Subject, Processed = entity.Processed, SenderName = entity.SenderName, @@ -103,7 +103,7 @@ public static class EntityModeMappers Errors = entity.Errors }; - public static Customer ToModel(this Shop.Customers.Entities.Customer entity) => + public static Customer ToModel(this Features.Shop.Customers.Entities.Customer entity) => new() { Id = entity.Id, @@ -128,7 +128,7 @@ public static class EntityModeMappers Whatsapp = entity.Whatsapp }; - public static Lead ToModel(this Shop.Leads.Entities.Lead entity) => + public static Lead ToModel(this Features.Shop.Leads.Entities.Lead entity) => new() { Id = entity.Id, @@ -149,7 +149,7 @@ public static class EntityModeMappers Status = entity.Status }; - public static Order ToModel(this Shop.Orders.Entities.Order entity) => + public static Order ToModel(this Features.Shop.Orders.Entities.Order entity) => new() { Id = entity.Id, @@ -163,7 +163,7 @@ public static class EntityModeMappers InvoiceUrl = entity.InvoiceUrl }; - public static OrderRefund ToModel(this Shop.Orders.Entities.OrderRefund entity) => + public static OrderRefund ToModel(this Features.Shop.Orders.Entities.OrderRefund entity) => new() { Id = entity.Id, @@ -173,7 +173,7 @@ public static class EntityModeMappers Amount = entity.Amount }; - public static Product ToModel(this Shop.Products.Entities.Product entity) => + public static Product ToModel(this Features.Shop.Products.Entities.Product entity) => new() { Id = entity.Id, @@ -185,7 +185,7 @@ public static class EntityModeMappers Thumbnails = entity.Thumbnails }; - public static ProductPrice ToModel(this Shop.Products.Entities.ProductPrice entity) => + public static ProductPrice ToModel(this Features.Shop.Products.Entities.ProductPrice entity) => new() { Id = entity.Id, diff --git a/LiteCharms.Features/Extensions/Shop.cs b/LiteCharms.Features/Extensions/Shop.cs new file mode 100644 index 0000000..78502de --- /dev/null +++ b/LiteCharms.Features/Extensions/Shop.cs @@ -0,0 +1,27 @@ +using LiteCharms.Features.Shop.CartPackages; +using LiteCharms.Features.Shop.Customers; +using LiteCharms.Features.Shop.Leads; +using LiteCharms.Features.Shop.Notifications; +using LiteCharms.Features.Shop.Orders; +using LiteCharms.Features.Shop.Products; +using LiteCharms.Features.Shop.Quotes; +using LiteCharms.Features.Shop.ShoppingCarts; + +namespace LiteCharms.Features.Extensions; + +public static class Shop +{ + public static IServiceCollection AddShopServices(this IServiceCollection services) + { + services.AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + + return services; + } +} diff --git a/LiteCharms.Features/Models/DateRange.cs b/LiteCharms.Features/Models/DateRange.cs new file mode 100644 index 0000000..c82cba3 --- /dev/null +++ b/LiteCharms.Features/Models/DateRange.cs @@ -0,0 +1,10 @@ +namespace LiteCharms.Features.Models; + +public class DateRange +{ + public DateOnly From { get; set; } + + public DateOnly To { get; set; } + + public int MaxRecords { get; set; } +} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/AddPackageItemsCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/AddPackageItemsCommand.cs deleted file mode 100644 index be87a47..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/AddPackageItemsCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class AddPackageItemCommand : IRequest> -{ - public Guid PackageId { get; set; } - - public Guid ProductPriceId { get; set; } - - private AddPackageItemCommand(Guid packageId, Guid productPriceId) - { - PackageId = packageId; - ProductPriceId = productPriceId; - } - - public static AddPackageItemCommand Create(Guid packageId, Guid productPriceId) - { - if (packageId == Guid.Empty) - throw new ArgumentException("Package id is required", nameof(packageId)); - - if (productPriceId == Guid.Empty) - throw new ArgumentException("Product price id is required", nameof(productPriceId)); - - return new(packageId, productPriceId); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/CreatePackageCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/CreatePackageCommand.cs deleted file mode 100644 index 4a86846..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/CreatePackageCommand.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class CreatePackageCommand : IRequest> -{ - public string? Name { get; set; } - - public string? Description { get; set; } - - private CreatePackageCommand(string? name, string? description) - { - Name = name; - Description = description; - } - - public static CreatePackageCommand Create(string? name, string? description) - { - ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); - - ArgumentException.ThrowIfNullOrWhiteSpace(description, nameof(description)); - - return new(name, description); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageCommand.cs deleted file mode 100644 index 5957dce..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class DeletePackageItemCommand : IRequest -{ - public Guid PackageId { get; set; } - - public Guid PackageItemId { get; set; } - - private DeletePackageItemCommand(Guid packageId, Guid packageItemId) - { - PackageId = packageId; - PackageItemId = packageItemId; - } - - public static DeletePackageItemCommand Create(Guid packageId, Guid packageItemId) - { - if (packageId == Guid.Empty) - throw new ArgumentException("Package id is required", nameof(packageId)); - - if (packageItemId == Guid.Empty) - throw new ArgumentException("Product price id is required", nameof(packageItemId)); - - return new(packageId, packageItemId); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageItemsCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageItemsCommand.cs deleted file mode 100644 index c9aa3e0..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/DeletePackageItemsCommand.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class DeletePackageItemsCommand : IRequest -{ - public Guid PackageId { get; set; } - - private DeletePackageItemsCommand(Guid packageId) => PackageId = packageId; - - public static DeletePackageItemsCommand Create(Guid packageId) - { - if (packageId == Guid.Empty) - throw new ArgumentException("Package ID is required", nameof(packageId)); - - return new(packageId); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/AddPackageItemCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/AddPackageItemCommandHandler.cs deleted file mode 100644 index 71c54f7..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/AddPackageItemCommandHandler.cs +++ /dev/null @@ -1,38 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class AddPackageItemCommandHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(AddPackageItemCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken)) - return Result.Fail($"Could not find package by ID {request.PackageId}"); - - if (!await context.ProductPrices.AnyAsync(p => p.Id == request.ProductPriceId && p.Active == true, cancellationToken)) - return Result.Fail($"Could not find an active product price by ID {request.ProductPriceId}"); - - if (await context.PackageItems.AnyAsync(p => p.ProductPriceId == request.ProductPriceId && p.PackageId == request.PackageId, cancellationToken)) - return Result.Fail($"Product price {request.ProductPriceId} is already added to this package {request.PackageId}"); - - var newPackageItem = context.PackageItems.Add(new Entities.PackageItem - { - PackageId = request.PackageId, - ProductPriceId = request.ProductPriceId, - Active = true - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok(newPackageItem.Entity.Id) - : Result.Fail($"Failed to add new package item by ID {request.ProductPriceId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/CreatePackageCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/CreatePackageCommandHandler.cs deleted file mode 100644 index ff7847e..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/CreatePackageCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class CreatePackageCommandHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(CreatePackageCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (await context.Packages.AnyAsync(p => p.Name == request.Name, cancellationToken)) - return Result.Fail($"A package by the same name already exists: {request.Name}"); - - var newPackage = context.Packages.Add(new Entities.Package - { - Name = request.Name, - Description = request.Description, - Active = true - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok(newPackage.Entity.Id) - : Result.Fail($"Failed to create a new package by the name: {request.Name}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemCommandHandler.cs deleted file mode 100644 index 7d7284e..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class DeletePackageItemCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(DeletePackageItemCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken)) - return Result.Fail($"Could not find package by ID {request.PackageId}"); - - var item = await context.PackageItems.FirstOrDefaultAsync(p => p.Id == request.PackageItemId && p.PackageId == request.PackageId, cancellationToken); - - if(item is null) - return Result.Fail($"Product item {request.PackageItemId} is already added to this package {request.PackageId}"); - - context.PackageItems.Remove(item); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to delete package item by id {request.PackageItemId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemsCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemsCommandHandler.cs deleted file mode 100644 index bad0e89..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/DeletePackageItemsCommandHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class DeletePackageItemsCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(DeletePackageItemsCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken)) - return Result.Fail($"Could not find package by ID {request.PackageId}"); - - var items = await context.PackageItems.Where(i => i.PackageId == request.PackageId).ToArrayAsync(cancellationToken); - - context.PackageItems.RemoveRange(items); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to delete package {request.PackageId} items"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageCommandHandler.cs deleted file mode 100644 index 15945ff..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageCommandHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class UpdatePackageCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(UpdatePackageCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (await context.Packages.AnyAsync(p => p.Name == request.Name, cancellationToken)) - return Result.Fail($"A package by the same name already exists: {request.Name}"); - - var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken); - - if (package is null) - return Result.Fail($"Could not find package by id {request.PackageId}"); - - package.Name = request.Name; - package.Description = request.Description; - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to update package with id {request.PackageId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageStatusCommandHandler.cs b/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageStatusCommandHandler.cs deleted file mode 100644 index 16c6482..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/Handlers/UpdatePackageStatusCommandHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Commands.Handlers; - -public class UpdatePackageStatusCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(UpdatePackageStatusCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken); - - if (package is null) - return Result.Fail($"Could not find package by id {request.PackageId}"); - - package.Active = request.Active; - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to update package with id {request.PackageId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageCommand.cs deleted file mode 100644 index 938ca44..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageCommand.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class UpdatePackageCommand : IRequest -{ - public Guid PackageId { get; set; } - - public string? Name { get; set; } - - public string? Description { get; set; } - - private UpdatePackageCommand(Guid packageId, string? name, string? description) - { - PackageId = packageId; - Name = name; - Description = description; - } - - public static UpdatePackageCommand Create(Guid packageId, string? name, string? description) - { - if (packageId == Guid.Empty) - throw new ArgumentException($"Package ID is required", nameof(packageId)); - - ArgumentNullException.ThrowIfNullOrWhiteSpace(name, nameof(name)); - ArgumentNullException.ThrowIfNullOrWhiteSpace(description, nameof(description)); - - return new(packageId, name, description); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageStatusCommand.cs b/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageStatusCommand.cs deleted file mode 100644 index 7be4651..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Commands/UpdatePackageStatusCommand.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace LiteCharms.Features.CartPackages.Commands; - -public class UpdatePackageStatusCommand : IRequest -{ - public Guid PackageId { get; set; } - - public bool Active { get; set; } - - private UpdatePackageStatusCommand(Guid packageId, bool active) - { - PackageId = packageId; - Active = active; - } - - public static UpdatePackageStatusCommand Create(Guid packageId, bool active) - { - if(packageId == Guid.Empty) - throw new ArgumentException($"Package id is required", nameof(packageId)); - - return new(packageId, active); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/PackageService.cs b/LiteCharms.Features/Shop/CartPackages/PackageService.cs new file mode 100644 index 0000000..1df6c00 --- /dev/null +++ b/LiteCharms.Features/Shop/CartPackages/PackageService.cs @@ -0,0 +1,243 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Models; +using LiteCharms.Features.Shop.CartPackages.Models; +using LiteCharms.Features.Shop.Postgres; +using static LiteCharms.Features.Extensions.Timezones; + +namespace LiteCharms.Features.Shop.CartPackages; + +public class PackageService(IDbContextFactory contextFactory) +{ + public async ValueTask> AddPackageItemAsync(Guid packageId, Guid productPriceId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken)) + return Result.Fail($"Could not find package by ID {packageId}"); + + if (!await context.ProductPrices.AnyAsync(p => p.Id == productPriceId && p.Active == true, cancellationToken)) + return Result.Fail($"Could not find an active product price by ID {productPriceId}"); + + if (await context.PackageItems.AnyAsync(p => p.ProductPriceId == productPriceId && p.PackageId == packageId, cancellationToken)) + return Result.Fail($"Product price {productPriceId} is already added to this package {packageId}"); + + var newPackageItem = context.PackageItems.Add(new Entities.PackageItem + { + PackageId = packageId, + ProductPriceId = productPriceId, + Active = true + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newPackageItem.Entity.Id) + : Result.Fail($"Failed to add new package item by ID {productPriceId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> CreatePackageAsync(string? name, string? summary, string? description, string? ImageUrl, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (await context.Packages.AnyAsync(p => p.Name == name, cancellationToken)) + return Result.Fail($"A package by the same name already exists: {name}"); + + var newPackage = context.Packages.Add(new Entities.Package + { + UpdatedAt = null, + Name = name, + Summary = summary, + Description = description, + ImageUrl = ImageUrl, + Active = true + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newPackage.Entity.Id) + : Result.Fail($"Failed to create a new package by the name: {name}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask DeletePackageItemAsync(Guid packageId, Guid packageItemId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken)) + return Result.Fail($"Could not find package by ID {packageId}"); + + var item = await context.PackageItems.FirstOrDefaultAsync(p => p.Id == packageItemId && p.PackageId == packageId, cancellationToken); + + if (item is null) + return Result.Fail($"Product item {packageItemId} is already added to this package {packageId}"); + + context.PackageItems.Remove(item); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to delete package item by id {packageItemId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask DeletePackageItemsAsync(Guid packageId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken)) + return Result.Fail($"Could not find package by ID {packageId}"); + + var items = await context.PackageItems.Where(i => i.PackageId == packageId).ToArrayAsync(cancellationToken); + + context.PackageItems.RemoveRange(items); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to delete package {packageId} items"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetPackageAsync(Guid packageId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken); + + return package is not null + ? Result.Ok(package.ToModel()) + : Result.Fail($"Failed to find package by ID {packageId}"); + + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetPackageItemsAsync(Guid packageId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken)) + return Result.Fail($"Package could not be found with ID {packageId}"); + + var items = await context.PackageItems.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(p => p.PackageId == packageId) + .ToArrayAsync(cancellationToken); + + return items?.Length > 0 + ? Result.Ok(items.Select(i => i.ToModel()).ToArray()) + : Result.Fail($"Could not find package items by package ID {packageId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetPackagesAsync(Guid packageId, DateRange range, bool active, 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 packages = await context.Packages + .AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(p => p.CreatedAt >= fromDate && p.CreatedAt <= toDate) + .Where(p => p.Active == active) + .Take(range.MaxRecords) + .ToArrayAsync(cancellationToken); + + return packages?.Length > 0 + ? Result.Ok(packages.Select(o => o.ToModel()).ToArray()) + : Result.Fail(new Error($"No packages found for the specified date range {range.From} - {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> UpdatePackageAsync(Guid packageId, string? name, string? summary, string? description, string? ImageUrl, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (await context.Packages.AnyAsync(p => p.Name == name, cancellationToken)) + return Result.Fail($"A package by the same name already exists: {name}"); + + var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken); + + if (package is null) + return Result.Fail($"Could not find package by id {packageId}"); + + package.Name = name; + package.Summary = summary; + package.Description = description; + package.ImageUrl = ImageUrl; + package.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to update package with id {packageId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> UpdatePackageStatusAsync(Guid packageId, bool active, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == packageId, cancellationToken); + + if (package is null) + return Result.Fail($"Could not find package by id {packageId}"); + + package.Active = active; + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to update package with id {packageId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } +} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageItemsQuery.cs b/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageItemsQuery.cs deleted file mode 100644 index 3315726..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageItemsQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.CartPackages.Models; - -namespace LiteCharms.Features.CartPackages.Queries; - -public class GetPackageItemsQuery : IRequest> -{ - public Guid PackageId { get; set; } - - private GetPackageItemsQuery(Guid packageId) => PackageId = packageId; - - public static GetPackageItemsQuery Create(Guid packageId) - { - if (packageId == Guid.Empty) - throw new ArgumentException("Package ID is required", nameof(packageId)); - - return new(packageId); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageQuery.cs b/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageQuery.cs deleted file mode 100644 index a05264e..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackageQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.CartPackages.Models; - -namespace LiteCharms.Features.CartPackages.Queries; - -public class GetPackageQuery : IRequest> -{ - public Guid PackageId { get; set; } - - private GetPackageQuery(Guid packageId) => PackageId = packageId; - - public static GetPackageQuery Create(Guid packageId) - { - if(packageId == Guid.Empty) - throw new ArgumentException("Package ID is required", nameof(packageId)); - - return new(packageId); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackagesQuery.cs b/LiteCharms.Features/Shop/CartPackages/Queries/GetPackagesQuery.cs deleted file mode 100644 index 036ff09..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/GetPackagesQuery.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Features.Shop.CartPackages.Models; - -namespace LiteCharms.Features.CartPackages.Queries; - -public class GetPackagesQuery : IRequest> -{ - public DateOnly From { get; set; } - - public DateOnly To { get; set; } - - public int MaxRecords { get; set; } - - public bool Active { get; set; } - - private GetPackagesQuery(DateOnly from, DateOnly to, int maxRecords = 1000, bool active = true) - { - From = from; - To = to; - MaxRecords = maxRecords; - Active = active; - } - - public static GetPackagesQuery Create(DateOnly from, DateOnly to, int maxRecords = 1000, bool active = true) - { - 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, active); - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageItemsQueryHandler.cs b/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageItemsQueryHandler.cs deleted file mode 100644 index 3b19cdc..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageItemsQueryHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.CartPackages.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Queries.Handlers; - -public class GetPackageItemsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetPackageItemsQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken)) - return Result.Fail($"Package could not be found with ID {request.PackageId}"); - - var items = await context.PackageItems.AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(p => p.PackageId == request.PackageId) - .ToArrayAsync(cancellationToken); - - return items?.Length > 0 - ? Result.Ok(items.Select(i => i.ToModel()).ToArray()) - : Result.Fail($"Could not find package items by package ID {request.PackageId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageQueryHandler.cs b/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageQueryHandler.cs deleted file mode 100644 index d822584..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackageQueryHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.CartPackages.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Queries.Handlers; - -public class GetPackageQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetPackageQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var package = await context.Packages.FirstOrDefaultAsync(p => p.Id == request.PackageId, cancellationToken); - - return package is not null - ? Result.Ok(package.ToModel()) - : Result.Fail($"Failed to find package by ID {request.PackageId}"); - - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackagesQueryHandler.cs b/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackagesQueryHandler.cs deleted file mode 100644 index 1ef691f..0000000 --- a/LiteCharms.Features/Shop/CartPackages/Queries/Handlers/GetPackagesQueryHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.CartPackages.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.CartPackages.Queries.Handlers; - -public class GetPackagesQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetPackagesQuery 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 packages = await context.Packages - .AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(p => p.CreatedAt >= fromDate && p.CreatedAt <= toDate) - .Where(p => p.Active == request.Active) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return packages?.Length > 0 - ? Result.Ok(packages.Select(o => o.ToModel()).ToArray()) - : Result.Fail(new Error($"No packages found for the specified date range {request.From} - {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Customers/Commands/CreateCustomerCommand.cs b/LiteCharms.Features/Shop/Customers/Commands/CreateCustomerCommand.cs deleted file mode 100644 index f0b3560..0000000 --- a/LiteCharms.Features/Shop/Customers/Commands/CreateCustomerCommand.cs +++ /dev/null @@ -1,64 +0,0 @@ -namespace LiteCharms.Features.Customers.Commands; - -public class CreateCustomerCommand : IRequest> -{ - public string? Company { get; set; } - - public string Name { get; set; } - - public string LastName { get; set; } - - public string? Tax { get; set; } - - public string Email { get; set; } - - public string? Discord { get; set; } - - public string? Slack { get; set; } - - public string? LinkedIn { get; set; } - - public string? Whatsapp { get; set; } - - public string? Website { get; set; } - - public string? Phone { get; set; } - - public string? Address { get; set; } - - public string? City { get; set; } - - public string? Region { get; set; } - - public string? Country { get; set; } - - public string? PostalCode { get; set; } - - private CreateCustomerCommand(string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode) - { - Name = name; - LastName = lastName; - Company = company; - Tax = tax; - Email = email; - Discord = discord; - Slack = slack; - LinkedIn = linkedIn; - Whatsapp = whatsapp; - Website = website; - Phone = phone; - Address = address; - City = city; - Region = region; - Country = country; - PostalCode = postalCode; - } - - public static CreateCustomerCommand Create(string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode) - { - if (string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(lastName) && string.IsNullOrWhiteSpace(email)) - throw new ArgumentException("At the following fields must be provided: Name, LastName, Email"); - - return new(name, lastName, company, tax, email, discord, slack, linkedIn, whatsapp, website, phone, address, city, region, country, postalCode); - } -} diff --git a/LiteCharms.Features/Shop/Customers/Commands/Handlers/CreateCustomerCommandHandler.cs b/LiteCharms.Features/Shop/Customers/Commands/Handlers/CreateCustomerCommandHandler.cs deleted file mode 100644 index bf1a5d6..0000000 --- a/LiteCharms.Features/Shop/Customers/Commands/Handlers/CreateCustomerCommandHandler.cs +++ /dev/null @@ -1,48 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Customers.Commands.Handlers; - -public class CreateCustomerCommandHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var customerEmail = request.Email.ToLower().Trim(); - - if (await context.Customers.AnyAsync(c => c.Email == customerEmail, cancellationToken)) - return Result.Fail(new Error($"A customer with the email {customerEmail} already exists")); - - var newCustomer = context.Customers.Add(new Entities.Customer - { - Company = request.Company, - Name = request.Name, - LastName = request.LastName, - Tax = request.Tax, - Email = customerEmail, - Discord = request.Discord, - Slack = request.Slack, - LinkedIn = request.LinkedIn, - Whatsapp = request.Whatsapp, - Website = request.Website, - Phone = request.Phone, - Address = request.Address, - City = request.City, - Region = request.Region, - Country = request.Country, - PostalCode = request.PostalCode, - Active = true, - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok(newCustomer.Entity.Id) - : Result.Fail(new Error($"Failed to create customer {customerEmail}")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Customers/Commands/Handlers/UpdateCustomerCommandHandler.cs b/LiteCharms.Features/Shop/Customers/Commands/Handlers/UpdateCustomerCommandHandler.cs deleted file mode 100644 index 031f591..0000000 --- a/LiteCharms.Features/Shop/Customers/Commands/Handlers/UpdateCustomerCommandHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Customers.Commands.Handlers; - -public class UpdateCustomerCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(UpdateCustomerCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken); - - if (customer is null) - return Result.Fail(new Error($"Customer with ID {request.CustomerId} not found.")); - - customer.Name = request.Name; - customer.LastName = request.LastName; - customer.Email = request.Email; - customer.Company = request.Company; - customer.Address = request.Address; - customer.City = request.City; - customer.Region = request.Region; - customer.Country = request.Country; - customer.PostalCode = request.PostalCode; - customer.Phone = request.Phone; - customer.Tax = request.Tax; - customer.City = request.City; - customer.Discord = request.Discord; - customer.Slack = request.Slack; - customer.LinkedIn = request.LinkedIn; - customer.Whatsapp = request.Whatsapp; - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail(new Error($"Failed to update the customer {request.CustomerId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} \ No newline at end of file diff --git a/LiteCharms.Features/Shop/Customers/Commands/UpdateCustomerCommand.cs b/LiteCharms.Features/Shop/Customers/Commands/UpdateCustomerCommand.cs deleted file mode 100644 index ac4f0cd..0000000 --- a/LiteCharms.Features/Shop/Customers/Commands/UpdateCustomerCommand.cs +++ /dev/null @@ -1,70 +0,0 @@ -namespace LiteCharms.Features.Customers.Commands; - -public class UpdateCustomerCommand : IRequest -{ - public Guid CustomerId { get; set; } - - public string? Company { get; set; } - - public string? Name { get; set; } - - public string? LastName { get; set; } - - public string? Tax { get; set; } - - public string? Email { get; set; } - - public string? Discord { get; set; } - - public string? Slack { get; set; } - - public string? LinkedIn { get; set; } - - public string? Whatsapp { get; set; } - - public string? Website { get; set; } - - public string? Phone { get; set; } - - public string? Address { get; set; } - - public string? City { get; set; } - - public string? Region { get; set; } - - public string? Country { get; set; } - - public string? PostalCode { get; set; } - - private UpdateCustomerCommand(Guid customerId, string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode) - { - CustomerId = customerId; - Name = name; - LastName = lastName; - Company = company; - Tax = tax; - Email = email; - Discord = discord; - Slack = slack; - LinkedIn = linkedIn; - Whatsapp = whatsapp; - Website = website; - Phone = phone; - Address = address; - City = city; - Region = region; - Country = country; - PostalCode = postalCode; - } - - public static UpdateCustomerCommand Create(Guid customerId, string name, string lastName, string? company, string? tax, string email, string? discord, string? slack, string? linkedIn, string? whatsapp, string? website, string? phone, string? address, string? city, string? region, string? country, string? postalCode) - { - if (customerId == Guid.Empty) - throw new ArgumentException("Customer ID is required.", nameof(customerId)); - - if (string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(lastName) && string.IsNullOrWhiteSpace(email)) - throw new ArgumentException("At the following fields must be provided: Name, LastName, Email"); - - return new(customerId, name, lastName, company, tax, email, discord, slack, linkedIn, whatsapp, website, phone, address, city, region, country, postalCode); - } -} diff --git a/LiteCharms.Features/Shop/Customers/CustomerService.cs b/LiteCharms.Features/Shop/Customers/CustomerService.cs new file mode 100644 index 0000000..531ecfe --- /dev/null +++ b/LiteCharms.Features/Shop/Customers/CustomerService.cs @@ -0,0 +1,132 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Models; +using LiteCharms.Features.Shop.Customers.Models; +using LiteCharms.Features.Shop.Postgres; + +namespace LiteCharms.Features.Shop.Customers; + +public class CustomerService(IDbContextFactory contextFactory) +{ + public async ValueTask> CreateCustomerAsync(CreateCustomer request, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var customerEmail = request.Email.ToLower().Trim(); + + if (await context.Customers.AnyAsync(c => c.Email == customerEmail, cancellationToken)) + return Result.Fail(new Error($"A customer with the email {customerEmail} already exists")); + + var newCustomer = context.Customers.Add(new Entities.Customer + { + Company = request.Company, + Name = request.Name, + LastName = request.LastName, + Tax = request.Tax, + Email = customerEmail, + Discord = request.Discord, + Slack = request.Slack, + LinkedIn = request.LinkedIn, + Whatsapp = request.Whatsapp, + Website = request.Website, + Phone = request.Phone, + Address = request.Address, + City = request.City, + Region = request.Region, + Country = request.Country, + PostalCode = request.PostalCode, + Active = true, + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newCustomer.Entity.Id) + : Result.Fail(new Error($"Failed to create customer {customerEmail}")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetCustomerAsync(Guid customerId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == customerId, cancellationToken); + + return customer is not null + ? Result.Ok(customer.ToModel()) + : Result.Fail($"Customer not found with id {customerId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetCustomersAsync(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 customers = await context.Customers.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(c => c.CreatedAt >= fromDate && c.CreatedAt <= toDate) + .Take(range.MaxRecords) + .ToArrayAsync(cancellationToken); + + return customers?.Length > 0 + ? Result.Ok(customers.Select(c => c.ToModel()).ToArray()) + : Result.Fail(new Error("No customers found in the specified date range.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask UpdateCustomerAsync(UpdateCustomer request, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var customer = await context.Customers.FirstOrDefaultAsync(c => c.Id == request.CustomerId, cancellationToken); + + if (customer is null) + return Result.Fail(new Error($"Customer with ID {request.CustomerId} not found.")); + + customer.Name = request.Name; + customer.LastName = request.LastName; + customer.Email = request.Email; + customer.Company = request.Company; + customer.Address = request.Address; + customer.City = request.City; + customer.Region = request.Region; + customer.Country = request.Country; + customer.PostalCode = request.PostalCode; + customer.Phone = request.Phone; + customer.Tax = request.Tax; + customer.City = request.City; + customer.Discord = request.Discord; + customer.Slack = request.Slack; + customer.LinkedIn = request.LinkedIn; + customer.Whatsapp = request.Whatsapp; + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail(new Error($"Failed to update the customer {request.CustomerId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } +} diff --git a/LiteCharms.Features/Shop/Customers/Models/Records.cs b/LiteCharms.Features/Shop/Customers/Models/Records.cs new file mode 100644 index 0000000..0b4c788 --- /dev/null +++ b/LiteCharms.Features/Shop/Customers/Models/Records.cs @@ -0,0 +1,73 @@ +namespace LiteCharms.Features.Shop.Customers.Models; + +public record CreateCustomer +{ + public string? Company { get; set; } + + public required string Name { get; set; } + + public required string LastName { get; set; } + + public string? Tax { get; set; } + + public required string Email { get; set; } + + public string? Discord { get; set; } + + public string? Slack { get; set; } + + public string? LinkedIn { get; set; } + + public string? Whatsapp { get; set; } + + public string? Website { get; set; } + + public string? Phone { get; set; } + + public string? Address { get; set; } + + public string? City { get; set; } + + public string? Region { get; set; } + + public string? Country { get; set; } + + public string? PostalCode { get; set; } +} + +public record UpdateCustomer +{ + public required Guid CustomerId { get; set; } + + public string? Company { get; set; } + + public string? Name { get; set; } + + public string? LastName { get; set; } + + public string? Tax { get; set; } + + public string? Email { get; set; } + + public string? Discord { get; set; } + + public string? Slack { get; set; } + + public string? LinkedIn { get; set; } + + public string? Whatsapp { get; set; } + + public string? Website { get; set; } + + public string? Phone { get; set; } + + public string? Address { get; set; } + + public string? City { get; set; } + + public string? Region { get; set; } + + public string? Country { get; set; } + + public string? PostalCode { get; set; } +} \ No newline at end of file diff --git a/LiteCharms.Features/Shop/Customers/Queries/GetCustomerQuery.cs b/LiteCharms.Features/Shop/Customers/Queries/GetCustomerQuery.cs deleted file mode 100644 index c6ee4b0..0000000 --- a/LiteCharms.Features/Shop/Customers/Queries/GetCustomerQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Customers.Models; - -namespace LiteCharms.Features.Customers.Queries; - -public class GetCustomerQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Customers/Queries/GetCustomersQuery.cs b/LiteCharms.Features/Shop/Customers/Queries/GetCustomersQuery.cs deleted file mode 100644 index 9c830b4..0000000 --- a/LiteCharms.Features/Shop/Customers/Queries/GetCustomersQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Customers.Models; - -namespace LiteCharms.Features.Customers.Queries; - -public class GetCustomersQuery : IRequest> -{ - public DateOnly From { get; set; } - - public DateOnly To { get; set; } - - public int MaxRecords { get; set; } - - private GetCustomersQuery(DateOnly from, DateOnly to, int maxRecords = 1000) - { - From = from; - To = to; - MaxRecords = maxRecords; - } - - public static GetCustomersQuery 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); - } -} diff --git a/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomerQueryHandler.cs b/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomerQueryHandler.cs deleted file mode 100644 index cbe478d..0000000 --- a/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomerQueryHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Customers.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Customers.Queries.Handlers; - -public class GetCustomerQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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 not found with id {request.CustomerId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomersQueryHandler.cs b/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomersQueryHandler.cs deleted file mode 100644 index b9d1e99..0000000 --- a/LiteCharms.Features/Shop/Customers/Queries/Handlers/GetCustomersQueryHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Customers.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Customers.Queries.Handlers; - -public class GetCustomersQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetCustomersQuery 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 customers = await context.Customers.AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(c => c.CreatedAt >= fromDate && c.CreatedAt <= toDate) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return customers?.Length > 0 - ? Result.Ok(customers.Select(c => c.ToModel()).ToArray()) - : Result.Fail(new Error("No customers found in the specified date range.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Leads/Commands/CreateLeadCommand.cs b/LiteCharms.Features/Shop/Leads/Commands/CreateLeadCommand.cs deleted file mode 100644 index 5b120df..0000000 --- a/LiteCharms.Features/Shop/Leads/Commands/CreateLeadCommand.cs +++ /dev/null @@ -1,55 +0,0 @@ -namespace LiteCharms.Features.Leads.Commands; - -public class CreateLeadCommand : IRequest> -{ - public Guid? CustomerId { get; set; } - - public string? Source { get; set; } - - public string? ClickId { get; set; } - - public string? WebClickId { get; set; } - - public string? AppClickId { get; set; } - - public long? CampaignId { get; set; } - - public long? AdGroupId { get; set; } - - public long? AdName { get; set; } - - public long? TargetId { get; set; } - - public long? FeedItemId { get; set; } - - public string? ClickLocation { get; set; } - - public string? AttribusionHash { get; set; } - - private CreateLeadCommand(Guid? customerId, string source, string clickId, string webClickId, string appClickId, long? campaignId, long? adGroupId, long? adName, long? targetId, long? feedItemId, string? clickLocation, string? attribusionHash) - { - CustomerId = customerId; - Source = source; - ClickId = clickId; - WebClickId = webClickId; - AppClickId = appClickId; - CampaignId = campaignId; - AdGroupId = adGroupId; - AdName = adName; - TargetId = targetId; - FeedItemId = feedItemId; - ClickLocation = clickLocation; - AttribusionHash = attribusionHash; - } - - public static CreateLeadCommand Create(Guid? customerId, string source, string clickId, string webClickId, string appClickId, long? campaignId, long? adGroupId, long? adName, long? targetId, long? feedItemId, string? clickLocation, string? attribusionHash) - { - if(string.IsNullOrWhiteSpace(source)) - throw new ArgumentNullException("Lead source is required to create a lead.", nameof(source)); - - if (string.IsNullOrWhiteSpace(clickId) || string.IsNullOrWhiteSpace(appClickId) || string.IsNullOrWhiteSpace(webClickId)) - throw new ArgumentException("ClickId, App ClickId and Web ClickId are required to create a lead."); - - return new(customerId, source, clickId, webClickId, appClickId, campaignId, adGroupId, adName, targetId, feedItemId, clickLocation, attribusionHash); - } -} diff --git a/LiteCharms.Features/Shop/Leads/Commands/Handlers/CreateLeadCommandHandler.cs b/LiteCharms.Features/Shop/Leads/Commands/Handlers/CreateLeadCommandHandler.cs deleted file mode 100644 index a51d31a..0000000 --- a/LiteCharms.Features/Shop/Leads/Commands/Handlers/CreateLeadCommandHandler.cs +++ /dev/null @@ -1,47 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Utilities.Hash.Commands; - -namespace LiteCharms.Features.Leads.Commands.Handlers; - -public class CreateLeadCommandHandler(IDbContextFactory contextFactory, ISender mediator) : IRequestHandler> -{ - public async ValueTask> Handle(CreateLeadCommand request, CancellationToken cancellationToken) - { - try - { - var hashCommand = ComputeHashCommand.Create($"{request.ClickId}{request.AppClickId}{request.WebClickId}"); - var hashResult = await mediator.Send(hashCommand, cancellationToken); - - if(hashResult.IsFailed) - return Result.Fail(new Error($"Failed to compute hash for lead -> Google ClickId: {request.ClickId}, App ClickId: {request.AppClickId}, Web ClickId: {request.WebClickId}") - .CausedBy(hashResult.Errors)); - - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var newLead = context.Leads.Add(new Entities.Lead - { - WebClickId = request.WebClickId, - AppClickId = request.AppClickId, - Source = request.Source, - ClickId = request.ClickId, - AdGroupId = request.AdGroupId, - AdName = request.AdName, - CampaignId = request.CampaignId, - ClickLocation = request.ClickLocation, - CustomerId = request.CustomerId, - FeedItemId = request.FeedItemId, - Status = Models.LeadStatus.New, - TargetId = request.TargetId, - AttributionHash = hashResult.Value - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok(newLead.Entity.Id) - : Result.Fail(new Error($"Failed to create lead -> Google ClickId: {request.ClickId}, App ClickId: {request.AppClickId}, Web ClickId: {request.WebClickId}")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Leads/Commands/Handlers/UpdateLeadCommandHandler.cs b/LiteCharms.Features/Shop/Leads/Commands/Handlers/UpdateLeadCommandHandler.cs deleted file mode 100644 index 67f14fc..0000000 --- a/LiteCharms.Features/Shop/Leads/Commands/Handlers/UpdateLeadCommandHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Leads.Commands.Handlers; - -public class UpdateLeadCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(UpdateLeadCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var lead = await context.Leads.FirstOrDefaultAsync(l => l.Id == request.LeadId, cancellationToken); - - if (lead is null) - return Result.Fail(new Error($"Lead with ID {request.LeadId} not found.")); - - lead.Status = request.Status; - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail(new Error($"Failed to update the lead {request.LeadId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Leads/Commands/UpdateLeadCommand.cs b/LiteCharms.Features/Shop/Leads/Commands/UpdateLeadCommand.cs deleted file mode 100644 index ef31b01..0000000 --- a/LiteCharms.Features/Shop/Leads/Commands/UpdateLeadCommand.cs +++ /dev/null @@ -1,28 +0,0 @@ -using LiteCharms.Features.Shop; -using LiteCharms.Models; - -namespace LiteCharms.Features.Leads.Commands; - -public class UpdateLeadCommand : IRequest -{ - public Guid LeadId { get; set; } - - public LeadStatus Status { get; set; } - - private UpdateLeadCommand(Guid leadId, LeadStatus status) - { - LeadId = leadId; - Status = status; - } - - public static UpdateLeadCommand Create(Guid leadId, LeadStatus status) - { - if (leadId == Guid.Empty) - throw new ArgumentException("Lead ID cannot be empty.", nameof(leadId)); - - if (!Enum.IsDefined(typeof(LeadStatus), status)) - throw new ArgumentException("Invalid lead status.", nameof(status)); - - return new(leadId, status); - } -} diff --git a/LiteCharms.Features/Shop/Leads/LeadService.cs b/LiteCharms.Features/Shop/Leads/LeadService.cs new file mode 100644 index 0000000..f8b4637 --- /dev/null +++ b/LiteCharms.Features/Shop/Leads/LeadService.cs @@ -0,0 +1,116 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Models; +using LiteCharms.Features.Shop.Leads.Models; +using LiteCharms.Features.Shop.Postgres; +using static LiteCharms.Features.Extensions.Hash; + +namespace LiteCharms.Features.Shop.Leads; + +public class LeadService(IDbContextFactory contextFactory) +{ + public async ValueTask> CreateLeadAsync(CreateLead request, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var newLead = context.Leads.Add(new Entities.Lead + { + WebClickId = request.WebClickId, + AppClickId = request.AppClickId, + Source = request.Source, + ClickId = request.ClickId, + AdGroupId = request.AdGroupId, + AdName = request.AdName, + CampaignId = request.CampaignId, + ClickLocation = request.ClickLocation, + CustomerId = request.CustomerId, + FeedItemId = request.FeedItemId, + Status = LeadStatus.New, + TargetId = request.TargetId, + AttributionHash = GenerateSha256HashString.Invoke($"{request.ClickId}{request.AppClickId}{request.WebClickId}") + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newLead.Entity.Id) + : Result.Fail(new Error($"Failed to create lead -> Google ClickId: {request.ClickId}, App ClickId: {request.AppClickId}, Web ClickId: {request.WebClickId}")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetCustomerLeadsAsync(Guid customerId, 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 leads = await context.Leads.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(lead => lead.CustomerId == customerId) + .Where(lead => lead.CreatedAt.Date >= fromDate && lead.CreatedAt.Date <= toDate) + .ToArrayAsync(cancellationToken); + + return leads?.Length > 0 + ? Result.Ok(leads.Select(l => l.ToModel()).ToArray()) + : Result.Fail(new Error($"No customer {customerId} leads found for the specified date range {range.From} to {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetLeadsAsync(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 leads = await context.Leads.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(l => l.CreatedAt.Date >= fromDate && l.CreatedAt.Date <= toDate) + .Take(range.MaxRecords) + .ToArrayAsync(cancellationToken); + + return leads?.Length > 0 + ? Result.Ok(leads.Select(l => l.ToModel()).ToArray()) + : Result.Fail(new Error($"No leads found for the specified date range {range.From} to {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask UpdateLeadAsync(Guid leadId, LeadStatus status, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var lead = await context.Leads.FirstOrDefaultAsync(l => l.Id == leadId, cancellationToken); + + if (lead is null) + return Result.Fail(new Error($"Lead with ID {leadId} not found.")); + + lead.Status = status; + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail(new Error($"Failed to update the lead {leadId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } +} diff --git a/LiteCharms.Features/Shop/Leads/Models/Records.cs b/LiteCharms.Features/Shop/Leads/Models/Records.cs new file mode 100644 index 0000000..9037942 --- /dev/null +++ b/LiteCharms.Features/Shop/Leads/Models/Records.cs @@ -0,0 +1,28 @@ +namespace LiteCharms.Features.Shop.Leads.Models; + +public record CreateLead +{ + public Guid? CustomerId { get; set; } + + public required string Source { get; set; } + + public required string ClickId { get; set; } + + public required string WebClickId { get; set; } + + public required string AppClickId { get; set; } + + public long? CampaignId { get; set; } + + public long? AdGroupId { get; set; } + + public long? AdName { get; set; } + + public long? TargetId { get; set; } + + public long? FeedItemId { get; set; } + + public string? ClickLocation { get; set; } + + public string? AttribusionHash { get; set; } +} diff --git a/LiteCharms.Features/Shop/Leads/Queries/GetCustomerLeadsQuery.cs b/LiteCharms.Features/Shop/Leads/Queries/GetCustomerLeadsQuery.cs deleted file mode 100644 index de53634..0000000 --- a/LiteCharms.Features/Shop/Leads/Queries/GetCustomerLeadsQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Leads.Models; - -namespace LiteCharms.Features.Leads.Queries; - -public class GetCustomerLeadsQuery : IRequest> -{ - public Guid CustomerId { get; } - - public DateOnly From { get; set; } - - public DateOnly To { get; set; } - - private GetCustomerLeadsQuery(Guid customerId, DateOnly from, DateOnly to) - { - CustomerId = customerId; - From = from; - To = to; - } - - public static GetCustomerLeadsQuery Create(Guid customerId, DateOnly from, DateOnly to) - { - if(customerId == Guid.Empty) - throw new ArgumentException("Customer ID cannot be empty.", nameof(customerId)); - - if(from > to) - throw new ArgumentException("The 'From' date cannot be later than the 'To' date."); - - return new(customerId, from, to); - } -} diff --git a/LiteCharms.Features/Shop/Leads/Queries/GetLeadsQuery.cs b/LiteCharms.Features/Shop/Leads/Queries/GetLeadsQuery.cs deleted file mode 100644 index d280e65..0000000 --- a/LiteCharms.Features/Shop/Leads/Queries/GetLeadsQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Leads.Models; - -namespace LiteCharms.Features.Leads.Queries; - -public class GetLeadsQuery : IRequest> -{ - public DateOnly From { get; set; } - - public DateOnly To { get; set; } - - public int MaxRecords { get; set; } - - private GetLeadsQuery(DateOnly from, DateOnly to, int maxRecords = 1000) - { - From = from; - To = to; - MaxRecords = maxRecords; - } - - public static GetLeadsQuery 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); - } -} diff --git a/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetCustomerLeadsQueryHandler.cs b/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetCustomerLeadsQueryHandler.cs deleted file mode 100644 index dd9ba15..0000000 --- a/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetCustomerLeadsQueryHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Leads.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Leads.Queries.Handlers; - -public class GetCustomerLeadsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetCustomerLeadsQuery 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 leads = await context.Leads.AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(lead => lead.CustomerId == request.CustomerId) - .Where(lead => lead.CreatedAt.Date >= fromDate && lead.CreatedAt.Date <= toDate) - .ToArrayAsync(cancellationToken); - - return leads?.Length > 0 - ? Result.Ok(leads.Select(l => l.ToModel()).ToArray()) - : Result.Fail(new Error($"No customer {request.CustomerId} leads found for the specified date range {request.From} to {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetLeadsQueryHandler.cs b/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetLeadsQueryHandler.cs deleted file mode 100644 index 417ff5c..0000000 --- a/LiteCharms.Features/Shop/Leads/Queries/Handlers/GetLeadsQueryHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Leads.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Leads.Queries.Handlers; - -public class GetLeadsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetLeadsQuery 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 leads = await context.Leads.AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(l => l.CreatedAt.Date >= fromDate && l.CreatedAt.Date <= toDate) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return leads?.Length > 0 - ? Result.Ok(leads.Select(l => l.ToModel()).ToArray()) - : Result.Fail(new Error($"No leads found for the specified date range {request.From} to {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Commands/CreateNotificationCommand.cs b/LiteCharms.Features/Shop/Notifications/Commands/CreateNotificationCommand.cs deleted file mode 100644 index 59e69d6..0000000 --- a/LiteCharms.Features/Shop/Notifications/Commands/CreateNotificationCommand.cs +++ /dev/null @@ -1,73 +0,0 @@ -using LiteCharms.Features.Shop; -using LiteCharms.Models; - -namespace LiteCharms.Features.Notifications.Commands; - -public class CreateNotification : IRequest> -{ - public NotificationDirection Direction { get; set; } - - public string? Sender { get; set; } - - public string? SenderAddress { get; set; } - - public string? Subject { get; set; } - - public string? Message { get; set; } - - public NotificationPlatforms Platform { get; set; } - - public Priorities Priority { get; set; } - - public string? Recipient { get; set; } - - public string? RecipientAddress { get; set; } - - public string? CorrelationId { get; set; } - - public CorrelationIdTypes CorrelationIdType { get; set; } - - public bool IsInternal { get; set; } - - public bool IsHtml { get; set; } - - private CreateNotification(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, CorrelationIdTypes correlationIdType, bool isInternal, bool isHtml = false) - { - Direction = direction; - Sender = sender; - SenderAddress = senderAddress; - Subject = subject; - Message = message; - Platform = platform; - Priority = priority; - Recipient = recipient; - RecipientAddress = recipientAddress; - CorrelationId = correlationId; - CorrelationIdType = correlationIdType; - IsInternal = isInternal; - IsHtml = isHtml; - } - - public static CreateNotification Create(NotificationDirection direction, string sender, string senderAddress, string subject, string message, NotificationPlatforms platform, Priorities priority, string recipient, string recipientAddress, string correlationId, CorrelationIdTypes correlationIdType, bool isInternal, bool isHtml = false) - { - if (string.IsNullOrWhiteSpace(sender)) - throw new ArgumentException("Sender name is required.", nameof(sender)); - - if (string.IsNullOrWhiteSpace(subject)) - throw new ArgumentException("Subject is required.", nameof(subject)); - - if (string.IsNullOrWhiteSpace(message)) - throw new ArgumentException("Message is required.", nameof(message)); - - if (string.IsNullOrWhiteSpace(recipient)) - throw new ArgumentException("Recipient name is required.", nameof(recipient)); - - if (string.IsNullOrWhiteSpace(recipientAddress)) - throw new ArgumentException("Recipient address is required.", nameof(recipientAddress)); - - if (string.IsNullOrWhiteSpace(correlationId)) - throw new ArgumentException("CorrelationId is required.", nameof(correlationId)); - - return new(direction, sender, senderAddress, subject, message, platform, priority, recipient, recipientAddress, correlationId, correlationIdType, isInternal, isHtml); - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Commands/Handlers/CreateNotificationCommandHandler.cs b/LiteCharms.Features/Shop/Notifications/Commands/Handlers/CreateNotificationCommandHandler.cs deleted file mode 100644 index facd9a3..0000000 --- a/LiteCharms.Features/Shop/Notifications/Commands/Handlers/CreateNotificationCommandHandler.cs +++ /dev/null @@ -1,40 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Notifications.Commands.Handlers; - -public class CreateNotificationCommandHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(CreateNotification request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var newNotification = context.Notifications.Add(new Entities.Notification - { - Direction = request.Direction, - SenderName = request.Sender, - Sender = request.SenderAddress, - Recipient = request.Recipient, - RecipientAddress = request.RecipientAddress, - Subject = request.Subject, - Message = request.Message, - Platform = request.Platform, - Priority = request.Priority, - CorrelationId = request.CorrelationId, - CorrelationIdType = request.CorrelationIdType, - IsInternal = request.IsInternal, - IsHtml = request.IsHtml, - Processed = false - }); - - 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Commands/Handlers/UpdateNotificationCommandHandler.cs b/LiteCharms.Features/Shop/Notifications/Commands/Handlers/UpdateNotificationCommandHandler.cs deleted file mode 100644 index 6d32acc..0000000 --- a/LiteCharms.Features/Shop/Notifications/Commands/Handlers/UpdateNotificationCommandHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Notifications.Commands.Handlers; - -public class UpdateNotificationCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask 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; - - if (request.HasError) - { - notification.HasError = request.HasError; - notification.Errors = request.Errors; - } - - 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Commands/UpdateNotificationCommand.cs b/LiteCharms.Features/Shop/Notifications/Commands/UpdateNotificationCommand.cs deleted file mode 100644 index 950442d..0000000 --- a/LiteCharms.Features/Shop/Notifications/Commands/UpdateNotificationCommand.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace LiteCharms.Features.Notifications.Commands; - -public class UpdateNotificationCommand : IRequest -{ - public Guid NotificationId { get; set; } - - public bool Processed { get; set; } - - public bool HasError { get; set; } - - public string[]? Errors { get; set; } - - private UpdateNotificationCommand(Guid notificationId, bool processed, bool hasError = false, string[]? errors = null) - { - NotificationId = notificationId; - Processed = processed; - HasError = hasError; - Errors = errors; - } - - public static UpdateNotificationCommand Create(Guid notificationId, bool processed, bool hasError = false, string[]? errors = null) - { - if(notificationId == Guid.Empty) - throw new ArgumentException("Notification ID cannot be empty.", nameof(notificationId)); - - return new(notificationId, processed); - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Entities/NotificationConfiguration.cs b/LiteCharms.Features/Shop/Notifications/Entities/NotificationConfiguration.cs index 37b7659..8a00b68 100644 --- a/LiteCharms.Features/Shop/Notifications/Entities/NotificationConfiguration.cs +++ b/LiteCharms.Features/Shop/Notifications/Entities/NotificationConfiguration.cs @@ -13,10 +13,10 @@ public class NotificationConfiguration : IEntityTypeConfiguration builder.Property(f => f.Platform).IsRequired().HasConversion(); builder.Property(f => f.Priority).IsRequired().HasConversion(); builder.Property(f => f.CorrelationIdType).IsRequired().HasConversion(); - builder.Property(f => f.Sender).IsRequired(); + builder.Property(f => f.SenderAddress).IsRequired(); builder.Property(f => f.Subject).IsRequired(); builder.Property(f => f.Message).IsRequired(); - builder.Property(f => f.Recipient).IsRequired(); + builder.Property(f => f.RecipientName).IsRequired(); builder.Property(f => f.RecipientAddress).IsRequired(); builder.Property(f => f.CorrelationId).IsRequired(); builder.Property(f => f.IsHtml).HasDefaultValue(false); diff --git a/LiteCharms.Features/Shop/Notifications/Events/Handlers/ProcessEmailNotificationsEventHandler.cs b/LiteCharms.Features/Shop/Notifications/Events/Handlers/ProcessEmailNotificationsEventHandler.cs index 332ec86..6a11fda 100644 --- a/LiteCharms.Features/Shop/Notifications/Events/Handlers/ProcessEmailNotificationsEventHandler.cs +++ b/LiteCharms.Features/Shop/Notifications/Events/Handlers/ProcessEmailNotificationsEventHandler.cs @@ -1,13 +1,15 @@ -using LiteCharms.Features.Email.Commands; +using LiteCharms.Features.Email; using LiteCharms.Features.Shop.Notifications.Entities; using LiteCharms.Features.Shop.Postgres; -using static LiteCharms.Features.ServiceBus.Constants; +using static LiteCharms.Features.Extensions.Timezones; -namespace LiteCharms.Features.Notifications.Events.Handlers; +namespace LiteCharms.Features.Shop.Notifications.Events.Handlers; -public class ProcessEmailNotificationsEventHandler(IDbContextFactory contextFactory, ILogger logger, ISender mediator) : - INotificationHandler +public class ProcessEmailNotificationsEventHandler(IDbContextFactory contextFactory, ILogger logger, + EmailService emailService) : INotificationHandler { + private bool dropBatch = false; + public async ValueTask Handle(ProcessEmailNotificationsEvent message, CancellationToken cancellationToken) { try @@ -17,14 +19,16 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory o.Priority) .ThenBy(o => o.CreatedAt) - .Where(n => n.CorrelationIdType == Models.CorrelationIdTypes.Email) - .Where(n => n.Direction == Models.NotificationDirection.Outgoing) + .Where(n => n.CorrelationIdType == CorrelationIdTypes.Email) + .Where(n => n.Direction == NotificationDirection.Outgoing) .Take(message.MaxRecords) .ToListAsync(cancellationToken); foreach (var notification in notifications) { - var sendResult = await SendEmailAsync(notification, cancellationToken); + if (dropBatch || cancellationToken.IsCancellationRequested) break; + + var sendResult = await SendEmailAsync(notification,emailService, cancellationToken); if(sendResult.IsFailed) { @@ -40,6 +44,7 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory SendEmailAsync(Notification notification, CancellationToken cancellationToken = default) + private async Task SendEmailAsync(Notification notification, EmailService service, CancellationToken cancellationToken = default) { try { - var request = SendEmailCommand.Create(notification.Sender!, notification.SenderName!, ShopEmailFromAddress, - ShopEmailFromName, notification.Subject!, notification.Message!); + using Email.Models.Message message = CreateMessage(notification); - var result = await mediator.Send(request, cancellationToken); + var sendResult = await service.SendEmailAsync(message, cancellationToken); - return result.IsFailed - ? Result.Fail(result.Errors) + if (sendResult.IsFailed) + { + if (emailService.Status != EmailStatuses.Success && emailService.Status != EmailStatuses.Connected) dropBatch = true; + + return Result.Fail(sendResult.Errors); + } + + return sendResult.IsFailed + ? Result.Fail(sendResult.Errors) : Result.Ok(); } catch (Exception ex) @@ -68,4 +79,29 @@ public class ProcessEmailNotificationsEventHandler(IDbContextFactory + new() + { + Sender = new Email.Models.Party + { + Name = notification.SenderName, + Address = notification.SenderAddress + }, + Recipient = new Email.Models.Party + { + Name = notification.RecipientName, + Address = notification.RecipientAddress + }, + Subject = notification.Subject, + Body = new Email.Models.Body + { + Properties = new Email.Models.BodyProperties + { + HasAttachments = false, + IsHtml = notification.IsHtml + }, + Message = notification.Message + } + }; } diff --git a/LiteCharms.Features/Shop/Notifications/Events/ProcessEmailNotificationsEvent.cs b/LiteCharms.Features/Shop/Notifications/Events/ProcessEmailNotificationsEvent.cs index bbc4da4..d496ab9 100644 --- a/LiteCharms.Features/Shop/Notifications/Events/ProcessEmailNotificationsEvent.cs +++ b/LiteCharms.Features/Shop/Notifications/Events/ProcessEmailNotificationsEvent.cs @@ -1,6 +1,6 @@ using LiteCharms.Features.Abstractions; -namespace LiteCharms.Features.Notifications.Events; +namespace LiteCharms.Features.Shop.Notifications.Events; public class ProcessEmailNotificationsEvent : EventBase, IEvent { diff --git a/LiteCharms.Features/Shop/Notifications/INotificationService.cs b/LiteCharms.Features/Shop/Notifications/INotificationService.cs deleted file mode 100644 index d036de3..0000000 --- a/LiteCharms.Features/Shop/Notifications/INotificationService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using LiteCharms.Features.Shop.Notifications.Models; - -namespace LiteCharms.Features.Shop.Notifications; - -public interface INotificationService -{ - Task> CreateNotificationAsync(CreateNotification request, CancellationToken cancellationToken = default); - Task UpdateNotificationAsync(UpdateNotification request, CancellationToken cancellationToken = default); -} diff --git a/LiteCharms.Features/Shop/Notifications/Models/Notification.cs b/LiteCharms.Features/Shop/Notifications/Models/Notification.cs index cba815c..bb9b77c 100644 --- a/LiteCharms.Features/Shop/Notifications/Models/Notification.cs +++ b/LiteCharms.Features/Shop/Notifications/Models/Notification.cs @@ -16,7 +16,7 @@ public class Notification public CorrelationIdTypes CorrelationIdType { get; set; } - public string? Sender { get; set; } + public string? SenderAddress { get; set; } public string? SenderName { get; set; } @@ -24,7 +24,7 @@ public class Notification public string? Message { get; set; } - public string? Recipient { get; set; } + public string? RecipientName { get; set; } public string? RecipientAddress { get; set; } diff --git a/LiteCharms.Features/Shop/Notifications/Models/Records.cs b/LiteCharms.Features/Shop/Notifications/Models/Records.cs index 2f0f949..6d41f26 100644 --- a/LiteCharms.Features/Shop/Notifications/Models/Records.cs +++ b/LiteCharms.Features/Shop/Notifications/Models/Records.cs @@ -2,23 +2,23 @@ public record CreateNotification { - public NotificationDirection Direction { get; set; } + public required NotificationDirection Direction { get; set; } - public string? Sender { get; set; } + public required string Sender { get; set; } - public string? SenderAddress { get; set; } + public required string SenderAddress { get; set; } - public string? Subject { get; set; } + public required string Subject { get; set; } public string? Message { get; set; } - public NotificationPlatforms Platform { get; set; } + public required NotificationPlatforms Platform { get; set; } - public Priorities Priority { get; set; } + public required Priorities Priority { get; set; } - public string? Recipient { get; set; } + public required string Recipient { get; set; } - public string? RecipientAddress { get; set; } + public required string RecipientAddress { get; set; } public string? CorrelationId { get; set; } @@ -31,9 +31,9 @@ public record CreateNotification public class UpdateNotification { - public Guid NotificationId { get; set; } + public required Guid NotificationId { get; set; } - public bool Processed { get; set; } + public required bool Processed { get; set; } public bool HasError { get; set; } diff --git a/LiteCharms.Features/Shop/Notifications/NotificationService.cs b/LiteCharms.Features/Shop/Notifications/NotificationService.cs index 02ee707..84e7565 100644 --- a/LiteCharms.Features/Shop/Notifications/NotificationService.cs +++ b/LiteCharms.Features/Shop/Notifications/NotificationService.cs @@ -1,5 +1,115 @@ -namespace LiteCharms.Features.Shop.Notifications; +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Models; +using LiteCharms.Features.Shop.Notifications.Models; +using LiteCharms.Features.Shop.Postgres; -public class NotificationService : INotificationService +namespace LiteCharms.Features.Shop.Notifications; + +public class NotificationService(IDbContextFactory contextFactory) { + public async ValueTask> CreateNotificationAsync(CreateNotification request, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var newNotification = context.Notifications.Add(new Entities.Notification + { + Direction = request.Direction, + SenderName = request.Sender, + SenderAddress = request.SenderAddress, + RecipientName = request.Recipient, + RecipientAddress = request.RecipientAddress, + Subject = request.Subject, + Message = request.Message, + Platform = request.Platform, + Priority = request.Priority, + CorrelationId = request.CorrelationId, + CorrelationIdType = request.CorrelationIdType, + IsInternal = request.IsInternal, + IsHtml = request.IsHtml, + Processed = false + }); + + 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)); + } + } + + public async ValueTask> GetNotificationAsync(Guid notificationId, CancellationToken cancellationToken = default) + { + try + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == notificationId, cancellationToken); + + return notification is not null + ? Result.Ok(notification.ToModel()) + : Result.Fail(new Error($"Notification with id {notificationId} not found")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetNotificationsAsync(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 notifications = await context.Notifications.AsNoTracking() + .Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate) + .OrderByDescending(n => n.CreatedAt) + .Take(range.MaxRecords) + .ToArrayAsync(cancellationToken); + + return notifications?.Length > 0 + ? Result.Ok(notifications.Select(n => n.ToModel()).ToArray()) + : Result.Fail(new Error($"No notifications found for the specified date range {range.From} to {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask UpdateNotificationAsync(UpdateNotification request, CancellationToken cancellationToken = default) + { + 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; + + if (request.HasError) + { + notification.HasError = request.HasError; + notification.Errors = request.Errors; + } + + 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)); + } + } } diff --git a/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationQuery.cs b/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationQuery.cs deleted file mode 100644 index 6c599fb..0000000 --- a/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Notifications.Models; - -namespace LiteCharms.Features.Notifications.Queries; - -public class GetNotificationQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationsQuery.cs b/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationsQuery.cs deleted file mode 100644 index 6a10a4e..0000000 --- a/LiteCharms.Features/Shop/Notifications/Queries/GetNotificationsQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Notifications.Models; - -namespace LiteCharms.Features.Notifications.Queries; - -public class GetNotificationsQuery : IRequest> -{ - public DateOnly From { get; set; } - - public DateOnly To { get; set; } - - public int MaxRecords { get; set; } - - private GetNotificationsQuery(DateOnly from, DateOnly to, int maxRecords = 1000) - { - From = from; - To = to; - MaxRecords = maxRecords; - } - - public static GetNotificationsQuery 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.", nameof(maxRecords)); - - return new(from, to, maxRecords); - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationQueryHandler.cs b/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationQueryHandler.cs deleted file mode 100644 index 11d5553..0000000 --- a/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationQueryHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Notifications.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Notifications.Queries.Handlers; - -public class GetNotificationQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetNotificationQuery request, CancellationToken cancellationToken) - { - try - { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken); - - return notification is not null - ? Result.Ok(notification.ToModel()) - : Result.Fail(new Error($"Notification with id {request.NotificationId} not found")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationsQueryHandler.cs b/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationsQueryHandler.cs deleted file mode 100644 index 17e6094..0000000 --- a/LiteCharms.Features/Shop/Notifications/Queries/Handlers/GetNotificationsQueryHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Notifications.Models; -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Notifications.Queries.Handlers; - -public class GetNotificationsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetNotificationsQuery 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 notifications = await context.Notifications.AsNoTracking() - .Where(n => n.CreatedAt >= fromDate && n.CreatedAt <= toDate) - .OrderByDescending(n => n.CreatedAt) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return notifications?.Length > 0 - ? Result.Ok(notifications.Select(n => n.ToModel()).ToArray()) - : Result.Fail(new Error($"No notifications found for the specified date range {request.From} to {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Commands/CreateOrderCommand.cs b/LiteCharms.Features/Shop/Orders/Commands/CreateOrderCommand.cs deleted file mode 100644 index 8baf7e1..0000000 --- a/LiteCharms.Features/Shop/Orders/Commands/CreateOrderCommand.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace LiteCharms.Features.Orders.Commands; - -public class CreateOrderCommand : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Commands/Handlers/CreateOrderCommandHandler.cs b/LiteCharms.Features/Shop/Orders/Commands/Handlers/CreateOrderCommandHandler.cs deleted file mode 100644 index 95a599a..0000000 --- a/LiteCharms.Features/Shop/Orders/Commands/Handlers/CreateOrderCommandHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"Customer {request.CustomerId} does not exist.")); - - if(!await context.ShoppingCarts.AnyAsync(sc => sc.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail(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(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(new Error($"Failed to create customer {request.CustomerId} order using shopping cart {request.ShoppingCartId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Commands/Handlers/UpdateOrderStatusCommandHandler.cs b/LiteCharms.Features/Shop/Orders/Commands/Handlers/UpdateOrderStatusCommandHandler.cs deleted file mode 100644 index 2c0052f..0000000 --- a/LiteCharms.Features/Shop/Orders/Commands/Handlers/UpdateOrderStatusCommandHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Orders.Commands.Handlers; - -public class UpdateOrderStatusCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Commands/UpdateOrderStatusCommand.cs b/LiteCharms.Features/Shop/Orders/Commands/UpdateOrderStatusCommand.cs deleted file mode 100644 index eefc734..0000000 --- a/LiteCharms.Features/Shop/Orders/Commands/UpdateOrderStatusCommand.cs +++ /dev/null @@ -1,31 +0,0 @@ -using LiteCharms.Features.Shop; -using LiteCharms.Models; - -namespace LiteCharms.Features.Orders.Commands; - -public class UpdateOrderStatusCommand : IRequest -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Models/Records.cs b/LiteCharms.Features/Shop/Orders/Models/Records.cs new file mode 100644 index 0000000..21dde34 --- /dev/null +++ b/LiteCharms.Features/Shop/Orders/Models/Records.cs @@ -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; } +} \ No newline at end of file diff --git a/LiteCharms.Features/Shop/Orders/OrderService.cs b/LiteCharms.Features/Shop/Orders/OrderService.cs new file mode 100644 index 0000000..705a099 --- /dev/null +++ b/LiteCharms.Features/Shop/Orders/OrderService.cs @@ -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 contextFactory) +{ + public async ValueTask> 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(new Error($"Customer {request.CustomerId} does not exist.")); + + if (!await context.ShoppingCarts.AnyAsync(sc => sc.Id == request.ShoppingCartId, cancellationToken)) + return Result.Fail(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(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(new Error($"Failed to create customer {request.CustomerId} order using shopping cart {request.ShoppingCartId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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(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(new Error($"No refunds found for customer with Id {customerId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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(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(new Error($"No orders found for customer with Id {customerId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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(new Error($"Refund {orderRefundId} not found for the given OrderId: {orderId}")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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($"Order refund could not be found with id {orderRefundId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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($"Order refunds could not be found with order id {orderId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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(new Error($"No orders found for the specified date range {range.From} - {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> 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(new Error($"Order with Id: {request.OrderId} does not exist")); + + if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken)) + return Result.Fail(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(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(new Error($"Failed to create refund for OrderId: {request.OrderId}")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask 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 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)); + } + } +} diff --git a/LiteCharms.Features/Shop/Orders/Queries/GetCustomerOrdersQuery.cs b/LiteCharms.Features/Shop/Orders/Queries/GetCustomerOrdersQuery.cs deleted file mode 100644 index f74f5c4..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/GetCustomerOrdersQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Orders.Models; - -namespace LiteCharms.Features.Orders.Queries; - -public class GetCustomerOrdersQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Queries/GetOrderRefundQuery.cs b/LiteCharms.Features/Shop/Orders/Queries/GetOrderRefundQuery.cs deleted file mode 100644 index 01295cf..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/GetOrderRefundQuery.cs +++ /dev/null @@ -1,27 +0,0 @@ -using LiteCharms.Features.Shop.Orders.Models; - -namespace LiteCharms.Features.Orders.Queries; - -public class GetOrderRefundQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Queries/GetOrdersQuery.cs b/LiteCharms.Features/Shop/Orders/Queries/GetOrdersQuery.cs deleted file mode 100644 index 0fc45d8..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/GetOrdersQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Orders.Models; - -namespace LiteCharms.Features.Orders.Queries; - -public class GetOrdersQuery : IRequest> -{ - 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); - } -} \ No newline at end of file diff --git a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetCustomerOrdersQueryHandler.cs b/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetCustomerOrdersQueryHandler.cs deleted file mode 100644 index a2f65d9..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetCustomerOrdersQueryHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(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(new Error($"No orders found for customer with Id {request.CustomerId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrderRefundQueryHandler.cs b/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrderRefundQueryHandler.cs deleted file mode 100644 index 3493b11..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrderRefundQueryHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"Refund {request.OrderRefundId} not found for the given OrderId: {request.OrderId}")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrdersQueryHandler.cs b/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrdersQueryHandler.cs deleted file mode 100644 index 898f3fc..0000000 --- a/LiteCharms.Features/Shop/Orders/Queries/Handlers/GetOrdersQueryHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"No orders found for the specified date range {request.From} - {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/RefundCustomerCommandHandler.cs b/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/RefundCustomerCommandHandler.cs deleted file mode 100644 index 9013f10..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/RefundCustomerCommandHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"Order with Id: {request.OrderId} does not exist")); - - if (!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken)) - return Result.Fail(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(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(new Error($"Failed to create refund for OrderId: {request.OrderId}")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/UpdateOrderRefundCommandHandler.cs b/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/UpdateOrderRefundCommandHandler.cs deleted file mode 100644 index ac88a49..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Commands/Handlers/UpdateOrderRefundCommandHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler -{ - public async ValueTask 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Commands/RefundCustomerCommand.cs b/LiteCharms.Features/Shop/Orders/Refunds/Commands/RefundCustomerCommand.cs deleted file mode 100644 index ac5c75b..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Commands/RefundCustomerCommand.cs +++ /dev/null @@ -1,38 +0,0 @@ -namespace LiteCharms.Features.Shop.Orders.Refunds.Commands; - -public class RefundCustomerCommand : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Commands/UpdateOrderRefundCommand.cs b/LiteCharms.Features/Shop/Orders/Refunds/Commands/UpdateOrderRefundCommand.cs deleted file mode 100644 index 9f69b2f..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Commands/UpdateOrderRefundCommand.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace LiteCharms.Features.Shop.Orders.Refunds.Commands; - -public class UpdateOrderRefundCommand : IRequest -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetCustomerRefundsQuery.cs b/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetCustomerRefundsQuery.cs deleted file mode 100644 index 56d19f6..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetCustomerRefundsQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Orders.Models; - -namespace LiteCharms.Features.Shop.Orders.Refunds.Queries; - -public class GetCustomerRefundsQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetRefundQuery.cs b/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetRefundQuery.cs deleted file mode 100644 index ddeaa7b..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Queries/GetRefundQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Orders.Models; - -namespace LiteCharms.Features.Shop.Orders.Refunds.Queries; - -public class GetRefundQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetCustomerRefundsQueryHandler.cs b/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetCustomerRefundsQueryHandler.cs deleted file mode 100644 index 8439f88..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetCustomerRefundsQueryHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(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(new Error($"No refunds found for customer with Id {request.CustomerId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetRefundQueryHandler.cs b/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetRefundQueryHandler.cs deleted file mode 100644 index a363945..0000000 --- a/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetRefundQueryHandler.cs +++ /dev/null @@ -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 contextFactory) : IRequestHandler> -{ - public async ValueTask> 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($"Order refund could not be found with id {request.OrderRefundId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Products/Models/Records.cs b/LiteCharms.Features/Shop/Products/Models/Records.cs new file mode 100644 index 0000000..b5579b8 --- /dev/null +++ b/LiteCharms.Features/Shop/Products/Models/Records.cs @@ -0,0 +1,14 @@ +namespace LiteCharms.Features.Shop.Products.Models; + +public record CreateProduct +{ + public required string Name { get; set; } + + public required string Summary { get; set; } + + public required string Description { get; set; } + + public required string ImageUrl { get; set; } + + public string[]? Thumbnails { get; set; } +} diff --git a/LiteCharms.Features/Shop/Products/ProductService.cs b/LiteCharms.Features/Shop/Products/ProductService.cs new file mode 100644 index 0000000..4b9b294 --- /dev/null +++ b/LiteCharms.Features/Shop/Products/ProductService.cs @@ -0,0 +1,229 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Shop.Postgres; +using LiteCharms.Features.Shop.Products.Models; +using static LiteCharms.Features.Extensions.Timezones; + +namespace LiteCharms.Features.Shop.Products; + +public class ProductService(IDbContextFactory contextFactory) +{ + public async ValueTask ChangeProductPriceStatusAsync(Guid productPriceId, bool active, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var price = await context.ProductPrices.FirstOrDefaultAsync(p => p.Id == productPriceId, cancellationToken); + + if (price is null) + return Result.Fail($"Could not find product price with ID {productPriceId}"); + + price.Active = active; + price.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to change product price by ID {productPriceId}"); + + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask ChangeProductStatusAsync(Guid productId, bool active, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var product = await context.Products.FirstOrDefaultAsync(p => p.Id == productId, cancellationToken); + + if (product is null) + return Result.Fail($"Could not find product with ID {productId}"); + + product.Active = active; + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to change product status by ID {productId}"); + + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> CreateProductAsync(CreateProduct request, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Products.AnyAsync(p => p.Name == request.Name, cancellationToken)) + return Result.Fail($"A product by the same name '{request.Name}' already exists"); + + var newProduct = context.Products.Add(new Entities.Product + { + Name = request.Name, + Summary = request.Summary, + Description = request.Description, + ImageUrl = request.ImageUrl, + Thumbnails = request.Thumbnails + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newProduct.Entity.Id) + : Result.Fail($"Failed to create new product '{request.Name}'"); + + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> CreateProductPriceAsync(Guid productId, decimal price, decimal discount = 0, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var newProductPrice = context.ProductPrices.Add(new Entities.ProductPrice + { + Price = price, + Discount = discount, + ProductId = productId + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(newProductPrice.Entity.Id) + : Result.Fail($"Failed to create new product price for product id {productId}"); + + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetProductAsync(Guid productId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var product = await context.Products.FirstOrDefaultAsync(p => p.Id == productId, cancellationToken); + + return product is not null + ? Result.Ok(product.ToModel()) + : Result.Fail(new Error($"Product with ID {productId} not found.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetProductPriceAsync(Guid productPriceId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Products.AnyAsync(p => p.Id == productPriceId, cancellationToken)) + return Result.Fail(new Error($"Product {productPriceId} not found.")); + + var productPrice = await context.ProductPrices.AsNoTracking() + .OrderByDescending(pp => pp.CreatedAt) + .FirstOrDefaultAsync(pp => pp.Id == productPriceId, cancellationToken); + + return productPrice is not null + ? Result.Ok(productPrice.ToModel()) + : Result.Fail(new Error($"Product price {productPriceId} not found.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetProductPricesAsync(int maxRecords = 1000, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var products = await context.ProductPrices.AsNoTracking() + .OrderByDescending(o => o.Id) + .Take(maxRecords) + .ToArrayAsync(cancellationToken); + + return Result.Ok(products.Select(p => p.ToModel()).ToArray()); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetProductsAsync(int maxRecords = 1000, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var products = await context.Products.AsNoTracking() + .OrderByDescending(o => o.Id) + .Take(maxRecords) + .ToArrayAsync(cancellationToken); + + return Result.Ok(products.Select(p => p.ToModel()).ToArray()); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> ReplaceProductPriceAsync(Guid productPriceId, decimal price, decimal discount = 0, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var existingPrice = await context.ProductPrices.FirstOrDefaultAsync(p => p.Id == productPriceId, cancellationToken); + + if (existingPrice is null) + return Result.Fail($"Could not find product price with ID {productPriceId}"); + + existingPrice.Active = false; + existingPrice.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + if (!(await context.SaveChangesAsync(cancellationToken) > 0)) + return Result.Fail($"Failed to deactivate existing price of ID {productPriceId}, try again later"); + + var result = await CreateProductPriceAsync(existingPrice.ProductId, price, discount, cancellationToken); + + if(result.IsFailed) + { + var deactivatedPrice = await context.ProductPrices.FirstOrDefaultAsync(p => p.Id == productPriceId, cancellationToken); + + existingPrice.Active = true; + existingPrice.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Fail("Reverted to old price, creation of new price failed") + : Result.Fail($"Failed to reactivate price of ID {productPriceId} after new price creation failed"); + } + + return Result.Ok(result.Value); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } +} diff --git a/LiteCharms.Features/Shop/Products/Queries/GetProductPriceQuery.cs b/LiteCharms.Features/Shop/Products/Queries/GetProductPriceQuery.cs deleted file mode 100644 index f118bcc..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/GetProductPriceQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries; - -public class GetProductPriceQuery : IRequest> -{ - public Guid ProductId { get; set; } - - private GetProductPriceQuery(Guid productId) => ProductId = productId; - - public static GetProductPriceQuery Create(Guid productId) - { - if (productId == Guid.Empty) - throw new ArgumentException("ProductId is required.", nameof(productId)); - - return new(productId); - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/GetProductPricesQuery.cs b/LiteCharms.Features/Shop/Products/Queries/GetProductPricesQuery.cs deleted file mode 100644 index 5ba4be2..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/GetProductPricesQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries; - -public class GetProductPricesQuery : IRequest> -{ - public int MaxRecords { get; set; } - - private GetProductPricesQuery(int maxRecords = 1000) => MaxRecords = maxRecords; - - public static GetProductPricesQuery Create(int maxRecords = 1000) - { - if (maxRecords <= 0) - throw new ArgumentOutOfRangeException(nameof(maxRecords), "MaxRecords must be greater than zero."); - - return new(maxRecords); - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/GetProductQuery.cs b/LiteCharms.Features/Shop/Products/Queries/GetProductQuery.cs deleted file mode 100644 index cd811ac..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/GetProductQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries; - -public class GetProductQuery : IRequest> -{ - public Guid ProductId { get; set; } - - private GetProductQuery(Guid productId) => ProductId = productId; - - public static GetProductQuery Create(Guid productId) - { - if(productId == Guid.Empty) - throw new ArgumentException("Product ID is required.", nameof(productId)); - - return new(productId); - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/GetProductsQuery.cs b/LiteCharms.Features/Shop/Products/Queries/GetProductsQuery.cs deleted file mode 100644 index a60670d..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/GetProductsQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries; - -public class GetProductsQuery : IRequest> -{ - public int MaxRecords { get; set; } - - private GetProductsQuery(int maxRecords = 1000) => MaxRecords = maxRecords; - - public static GetProductsQuery Create(int maxRecords = 1000) - { - if (maxRecords <= 0) - throw new ArgumentException("MaxRecords must be a positive integer."); - - return new(maxRecords); - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPriceQueryHandler.cs b/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPriceQueryHandler.cs deleted file mode 100644 index c1383de..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPriceQueryHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries.Handlers; - -public class GetProductPriceQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetProductPriceQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if(!await context.Products.AnyAsync(p => p.Id == request.ProductId, cancellationToken)) - return Result.Fail(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) - .FirstOrDefaultAsync(cancellationToken); - - return productPrice is not null - ? Result.Ok(productPrice.ToModel()) - : Result.Fail(new Error($"Product price {request.ProductId} not found.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPricesQueryHandler.cs b/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPricesQueryHandler.cs deleted file mode 100644 index 1bc5489..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductPricesQueryHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries.Handlers; - -public class GetProductPricesQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetProductPricesQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var products = await context.ProductPrices.AsNoTracking() - .OrderByDescending(o => o.Id) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return Result.Ok(products.Select(p => p.ToModel()).ToArray()); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductQueryHandler.cs b/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductQueryHandler.cs deleted file mode 100644 index d60591e..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductQueryHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries.Handlers; - -public class GetProductQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetProductQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var product = await context.Products.FirstOrDefaultAsync(p => p.Id == request.ProductId, cancellationToken); - - return product is not null - ? Result.Ok(product.ToModel()) - : Result.Fail(new Error($"Product with ID {request.ProductId} not found.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductsQueryHandler.cs b/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductsQueryHandler.cs deleted file mode 100644 index 0ad9f40..0000000 --- a/LiteCharms.Features/Shop/Products/Queries/Handlers/GetProductsQueryHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Products.Models; - -namespace LiteCharms.Features.Products.Queries.Handlers; - -public class GetProductsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetProductsQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - var products = await context.Products.AsNoTracking() - .OrderByDescending(o => o.Id) - .Take(request.MaxRecords) - .ToArrayAsync(cancellationToken); - - return Result.Ok(products.Select(p => p.ToModel()).ToArray()); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToOrderCommand.cs b/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToOrderCommand.cs deleted file mode 100644 index 498aa4a..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToOrderCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.Quotes.Commands; - -public class AssignQuoteToOrderCommand : IRequest -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToShoppingCartCommand.cs b/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToShoppingCartCommand.cs deleted file mode 100644 index 61c0902..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/AssignQuoteToShoppingCartCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.Quotes.Commands; - -public class AssignQuoteToShoppingCartCommand : IRequest -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToOrderCommandHandler.cs b/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToOrderCommandHandler.cs deleted file mode 100644 index 266e8d9..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToOrderCommandHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Quotes.Commands.Handlers; - -public class AssignQuoteToOrderCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToShoppingCartCommandHandler.cs deleted file mode 100644 index a9f6b12..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/AssignQuoteToShoppingCartCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Quotes.Commands.Handlers; - -public class AssignQuoteToShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/UpdateQuoteStatusCommandHandler.cs b/LiteCharms.Features/Shop/Quotes/Commands/Handlers/UpdateQuoteStatusCommandHandler.cs deleted file mode 100644 index 8e80a52..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/Handlers/UpdateQuoteStatusCommandHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.Quotes.Commands.Handlers; - -public class UpdateQuoteStatusCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask 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)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Commands/UpdateQuoteStatusCommand.cs b/LiteCharms.Features/Shop/Quotes/Commands/UpdateQuoteStatusCommand.cs deleted file mode 100644 index 14c9684..0000000 --- a/LiteCharms.Features/Shop/Quotes/Commands/UpdateQuoteStatusCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -using LiteCharms.Features.Shop; -using LiteCharms.Models; - -namespace LiteCharms.Features.Quotes.Commands; - -public class UpdateQuoteStatusCommand : IRequest -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Queries/GetCustomerQuotesQuery.cs b/LiteCharms.Features/Shop/Quotes/Queries/GetCustomerQuotesQuery.cs deleted file mode 100644 index 37adc17..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/GetCustomerQuotesQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries; - -public class GetCustomerQuotesQuery : IRequest> -{ - 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); - } -} \ No newline at end of file diff --git a/LiteCharms.Features/Shop/Quotes/Queries/GetQuoteQuery.cs b/LiteCharms.Features/Shop/Quotes/Queries/GetQuoteQuery.cs deleted file mode 100644 index 756788e..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/GetQuoteQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries; - -public class GetQuoteQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Queries/GetQuotesQuery.cs b/LiteCharms.Features/Shop/Quotes/Queries/GetQuotesQuery.cs deleted file mode 100644 index 4e7db83..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/GetQuotesQuery.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries; - -public class GetQuotesQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetCustomerQuotesQueryHandler.cs b/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetCustomerQuotesQueryHandler.cs deleted file mode 100644 index 811df36..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetCustomerQuotesQueryHandler.cs +++ /dev/null @@ -1,31 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Quotes.Queries; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries.Handlers; - -public class GetCustomerQuotesQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(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(new Error($"No quotes found for customer with Id {request.CustomerId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuoteQueryHandler.cs b/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuoteQueryHandler.cs deleted file mode 100644 index 35c2d39..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuoteQueryHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries.Handlers; - -public class GetQuoteQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"Quote with ID {request.QuoteId} not found.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuotesHandler.cs b/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuotesHandler.cs deleted file mode 100644 index f7ad77b..0000000 --- a/LiteCharms.Features/Shop/Quotes/Queries/Handlers/GetQuotesHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.Quotes.Models; - -namespace LiteCharms.Features.Quotes.Queries.Handlers; - -public class GetQuotesHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(new Error($"No quotes found for the specified date range {request.From} - {request.To}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/Quotes/QuoteService.cs b/LiteCharms.Features/Shop/Quotes/QuoteService.cs new file mode 100644 index 0000000..7e726c1 --- /dev/null +++ b/LiteCharms.Features/Shop/Quotes/QuoteService.cs @@ -0,0 +1,154 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Models; +using LiteCharms.Features.Shop.Postgres; +using LiteCharms.Features.Shop.Quotes.Models; +using static LiteCharms.Features.Extensions.Timezones; + +namespace LiteCharms.Features.Shop.Quotes; + +public class QuoteService(IDbContextFactory contextFactory) +{ + public async ValueTask AssignQuoteToOrderAsync(Guid quoteId, Guid orderId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var quote = await context.Quotes.FirstOrDefaultAsync(o => o.Id == quoteId, cancellationToken); + + if (quote is null) + return Result.Fail(new Error($"Quote with id {orderId} not found")); + + if (!await context.Orders.AnyAsync(q => q.Id == orderId, cancellationToken)) + return Result.Fail(new Error($"Order with id {quoteId} not found")); + + if (quote.OrderId == orderId) + return Result.Fail(new Error($"Quote with id {quoteId} is already assigned to order with id {orderId}")); + + quote.OrderId = orderId; + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail(new Error($"Failed to assign quote with id {quoteId} to order with id {orderId}")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask AssignQuoteToShoppingCartAsync(Guid quoteId, Guid shoppingCartId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var quote = await context.Quotes.FirstOrDefaultAsync(o => o.Id == quoteId, cancellationToken); + + if (quote is null) + return Result.Fail(new Error($"Quote with id {quoteId} not found")); + + if (!await context.ShoppingCarts.AnyAsync(q => q.Id == shoppingCartId, cancellationToken)) + return Result.Fail(new Error($"Shopping Cart with id {shoppingCartId} not found")); + + quote.ShoppingCartId = shoppingCartId; + + 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)); + } + } + + public async ValueTask> GetCustomerQuotesAsync(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(new Error($"Customer with Id {customerId} does not exist.")); + + var quotes = await context.Quotes.AsNoTracking() + .Where(q => q.CustomerId == customerId).ToArrayAsync(cancellationToken); + + return quotes?.Length > 0 + ? Result.Ok(quotes.Select(q => q.ToModel()).ToArray()) + : Result.Fail(new Error($"No quotes found for customer with Id {customerId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetQuoteAsync(Guid quoteId, CancellationToken cancellationToken = default) + { + try + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var quote = await context.Quotes.AsNoTracking().FirstOrDefaultAsync(q => q.Id == quoteId, cancellationToken); + + return quote is not null + ? Result.Ok(quote.ToModel()) + : Result.Fail(new Error($"Quote with ID {quoteId} not found.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetQuotesAsync(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 quotes = await context.Quotes.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate) + .Take(range.MaxRecords) + .ToArrayAsync(cancellationToken); + + return quotes?.Length > 0 + ? Result.Ok(quotes.Select(o => o.ToModel()).ToArray()) + : Result.Fail(new Error($"No quotes found for the specified date range {range.From} - {range.To}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask UpdateQuoteStatusAsync(Guid quoteId, QuoteStatus status, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var quote = await context.Quotes.FirstOrDefaultAsync(q => q.Id == quoteId, cancellationToken); + + if (quote is null) + return Result.Fail(new Error("Quote not found.")); + + quote.Status = status; + quote.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + 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)); + } + } +} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddItemToShoppingCartCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddItemToShoppingCartCommand.cs deleted file mode 100644 index 009fccd..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddItemToShoppingCartCommand.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class AddItemToShoppingCartCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - public Guid ProductPriceId { get; set; } - - public int Quantity { get; set; } - - private AddItemToShoppingCartCommand(Guid shoppingCartId, Guid productPriceId, int quantity = 1) - { - ShoppingCartId = shoppingCartId; - ProductPriceId = productPriceId; - Quantity = quantity; - } - - public static AddItemToShoppingCartCommand Create(Guid shoppingCartId, Guid productPriceId, int quantity = 1) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId)); - - if (productPriceId == Guid.Empty) - throw new ArgumentException($"Product item required", nameof(productPriceId)); - - if(quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity)); - - return new(shoppingCartId, productPriceId, quantity); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddPackageToShoppingCartCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddPackageToShoppingCartCommand.cs deleted file mode 100644 index 81e8bac..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/AddPackageToShoppingCartCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class AddPackageToShoppingCartCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - public Guid PackageId { get; set; } - - private AddPackageToShoppingCartCommand(Guid shoppingCartId, Guid packageId) - { - ShoppingCartId = shoppingCartId; - PackageId = packageId; - } - - public static AddPackageToShoppingCartCommand Create(Guid shoppingCartId, Guid packageId) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId)); - - if (packageId == Guid.Empty) - throw new ArgumentException($"Package ID is required", nameof(packageId)); - - return new(shoppingCartId, packageId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/CreateShoppingCartCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/CreateShoppingCartCommand.cs deleted file mode 100644 index fdc7b0e..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/CreateShoppingCartCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class CreateShoppingCartCommand : IRequest> -{ - public Guid? CustomerId { get; set; } - - public Guid? OrderId { get; set; } - - public Guid? QuoteId { get; set; } - - private CreateShoppingCartCommand(Guid customerId, Guid? orderId = null, Guid? quoteId = null) - { - CustomerId = customerId; - OrderId = orderId; - QuoteId = quoteId; - } - - public static CreateShoppingCartCommand Create(Guid customerId, Guid? orderId = null, Guid? quoteId = null) - { - if (customerId == Guid.Empty) - throw new ArgumentException($"Customer ID is required", nameof(customerId)); - - return new(customerId, orderId, quoteId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/EmptyShoppingCartCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/EmptyShoppingCartCommand.cs deleted file mode 100644 index f535ca4..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/EmptyShoppingCartCommand.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class EmptyShoppingCartCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - private EmptyShoppingCartCommand(Guid shoppingCartId) => ShoppingCartId = shoppingCartId; - - public static EmptyShoppingCartCommand Create(Guid shoppingCartId) - { - if(shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required.", nameof(shoppingCartId)); - - return new(shoppingCartId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddItemToShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddItemToShoppingCartCommandHandler.cs deleted file mode 100644 index 7a2cd2c..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddItemToShoppingCartCommandHandler.cs +++ /dev/null @@ -1,40 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class AddItemToShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(AddItemToShoppingCartCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ProductPriceId, cancellationToken)) - return Result.Fail($"Product item could not be found with id {request.ProductPriceId}"); - - var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken); - - if (cart is null) - return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}"); - - if (cart.ShoppingCartItems?.Any(i => i.ProductPriceId == request.ProductPriceId) == true) - return Result.Fail($"Item already in shopping cart with id {request.ShoppingCartId}"); - - context.ShoppingCartItems.Add(new Entities.ShoppingCartItem - { - ShoppingCartId = request.ShoppingCartId, - ProductPriceId = request.ProductPriceId, - Quantity = request.Quantity - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to add cart item with id {request.ProductPriceId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddPackageToShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddPackageToShoppingCartCommandHandler.cs deleted file mode 100644 index a634375..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/AddPackageToShoppingCartCommandHandler.cs +++ /dev/null @@ -1,39 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class AddPackageToShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(AddPackageToShoppingCartCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken)) - return Result.Fail($"Package cold not be found by ID {request.PackageId}"); - - var shoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken); - - if (shoppingCart is null) - return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}"); - - if (!await context.ShoppingCartPackages.AnyAsync(cp => cp.ShoppingCartId == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Package {request.PackageId} is already in the cart"); - - var newShoppingCartPackage = context.ShoppingCartPackages.Add(new Entities.ShoppingCartPackage - { - ShoppingCartId = request.ShoppingCartId, - PackageId = request.PackageId - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Could not add package of id {request.PackageId} to shopping cart {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/CreateShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/CreateShoppingCartCommandHandler.cs deleted file mode 100644 index 2d51f2e..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/CreateShoppingCartCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class CreateShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(CreateShoppingCartCommand 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($"Customer could not be found with id {request.CustomerId}"); - - var cart = context.ShoppingCarts.Add(new Entities.ShoppingCart - { - CustomerId = request.CustomerId, - OrderId = request.OrderId, - QuoteId = request.QuoteId - }); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok(cart.Entity.Id) - : Result.Fail($"Failed to create shopping cart for customer id {request.CustomerId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/EmptyShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/EmptyShoppingCartCommandHandler.cs deleted file mode 100644 index 0043a5f..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/EmptyShoppingCartCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class EmptyShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(EmptyShoppingCartCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}"); - - if (await context.ShoppingCartItems.CountAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken) == 0) - return Result.Ok(); - - var cartItems = await context.ShoppingCartItems.Where(i => i.ShoppingCartId == request.ShoppingCartId).ToListAsync(cancellationToken); - - context.RemoveRange(cartItems); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Could not empty cart with id {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemovePackageFromShoppingCartCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemovePackageFromShoppingCartCommandHandler.cs deleted file mode 100644 index 2bd5b3a..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemovePackageFromShoppingCartCommandHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class RemovePackageFromShoppingCartCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(RemovePackageFromShoppingCartCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}"); - - if (!await context.ShoppingCartPackages.AnyAsync(p => p.Id == request.ShoppingCartPackageId, cancellationToken)) - return Result.Fail($"Shopping cart package {request.ShoppingCartPackageId} is not in the shopping cart {request.ShoppingCartId}"); - - var shoppingCartPackage = await context.ShoppingCartPackages.FirstOrDefaultAsync(cp => cp.Id == request.ShoppingCartPackageId, cancellationToken); - - if (shoppingCartPackage is null) - return Result.Ok(); - - context.ShoppingCartPackages.Remove(shoppingCartPackage!); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Could remove package of id {request.ShoppingCartPackageId} from shopping cart {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemoveShoppingCartItemCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemoveShoppingCartItemCommandHandler.cs deleted file mode 100644 index 442c944..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/RemoveShoppingCartItemCommandHandler.cs +++ /dev/null @@ -1,36 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class RemoveShoppingCartItemCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(RemoveShoppingCartItemCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ShoppingCartItemId, cancellationToken)) - return Result.Fail($"Product item could not be found with id {request.ShoppingCartItemId}"); - - var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken); - - if (cart is null) - return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartId}"); - - var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.Id == request.ShoppingCartItemId, cancellationToken); - - if (item is null) return Result.Ok(); - - context.ShoppingCartItems.Remove(item); - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to remove shopping cart item with id {request.ShoppingCartItemId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/UpdateShoppingCartItemCommandHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/UpdateShoppingCartItemCommandHandler.cs deleted file mode 100644 index 3e54242..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/Handlers/UpdateShoppingCartItemCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Features.Shop.Postgres; - -namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers; - -public class UpdateShoppingCartItemCommandHandler(IDbContextFactory contextFactory) : IRequestHandler -{ - public async ValueTask Handle(UpdateShoppingCartItemCommand request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}"); - - var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken); - - if(item is null) - return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartItemId}"); - - item.Quantity = request.Quantity; - - return await context.SaveChangesAsync(cancellationToken) > 0 - ? Result.Ok() - : Result.Fail($"Failed to update cart item quntity"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemovePackageFromShoppingCartCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemovePackageFromShoppingCartCommand.cs deleted file mode 100644 index 6aa8f25..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemovePackageFromShoppingCartCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class RemovePackageFromShoppingCartCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - public Guid ShoppingCartPackageId { get; set; } - - private RemovePackageFromShoppingCartCommand(Guid shoppingCartId, Guid shoppingCartPackageId) - { - ShoppingCartId = shoppingCartId; - ShoppingCartPackageId = shoppingCartPackageId; - } - - public static RemovePackageFromShoppingCartCommand Create(Guid shoppingCartId, Guid shoppingCartPackageId) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId)); - - if (shoppingCartPackageId == Guid.Empty) - throw new ArgumentException($"Shopping cart Package ID is required", nameof(shoppingCartPackageId)); - - return new(shoppingCartId, shoppingCartPackageId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemoveShoppingCartItemCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemoveShoppingCartItemCommand.cs deleted file mode 100644 index 26706d8..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/RemoveShoppingCartItemCommand.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class RemoveShoppingCartItemCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - public Guid ShoppingCartItemId { get; set; } - - private RemoveShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId) - { - ShoppingCartId = shoppingCartId; - ShoppingCartItemId = shoppingCartItemId; - } - - public static RemoveShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId)); - - if (shoppingCartItemId == Guid.Empty) - throw new ArgumentException($"Shopping cart item required", nameof(shoppingCartItemId)); - - return new(shoppingCartId, shoppingCartItemId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Commands/UpdateShoppingCartItemCommand.cs b/LiteCharms.Features/Shop/ShoppingCarts/Commands/UpdateShoppingCartItemCommand.cs deleted file mode 100644 index d7cebe0..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Commands/UpdateShoppingCartItemCommand.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace LiteCharms.Features.ShoppingCarts.Commands; - -public class UpdateShoppingCartItemCommand : IRequest -{ - public Guid ShoppingCartId { get; set; } - - public Guid ShoppingCartItemId { get; set; } - - public int Quantity { get; set; } - - private UpdateShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1) - { - ShoppingCartId = shoppingCartId; - ShoppingCartItemId = shoppingCartItemId; - Quantity = quantity; - } - - public static UpdateShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId)); - - if (shoppingCartItemId == Guid.Empty) - throw new ArgumentException($"Shopping cart item is required", nameof(shoppingCartItemId)); - - if (quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity)); - - return new(shoppingCartId, shoppingCartItemId, quantity); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetCustomerShoppingCartsQuery.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetCustomerShoppingCartsQuery.cs deleted file mode 100644 index e901ee3..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetCustomerShoppingCartsQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries; - -public class GetCustomerShoppingCartsQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartItemsQuery.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartItemsQuery.cs deleted file mode 100644 index e6ddec3..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartItemsQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries; - -public class GetShoppingCartItemsQuery : IRequest> -{ - public Guid ShoppingCartId { get; set; } - - private GetShoppingCartItemsQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId; - - public static GetShoppingCartItemsQuery Create(Guid shoppingCartId) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId)); - - return new(shoppingCartId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartPackagesQuery.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartPackagesQuery.cs deleted file mode 100644 index 611b48f..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartPackagesQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries; - -public class GetShoppingCartPackagesQuery : IRequest> -{ - public Guid ShoppingCartId { get; set; } - - private GetShoppingCartPackagesQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId; - - public static GetShoppingCartPackagesQuery Create(Guid shoppingCartId) - { - if (shoppingCartId == Guid.Empty) - throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId)); - - return new(shoppingCartId); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartQuery.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartQuery.cs deleted file mode 100644 index 1ef9784..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/GetShoppingCartQuery.cs +++ /dev/null @@ -1,18 +0,0 @@ -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries; - -public class GetShoppingCartQuery : IRequest> -{ - 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); - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetCustomerShoppingCartsQueryHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetCustomerShoppingCartsQueryHandler.cs deleted file mode 100644 index 8554fc6..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetCustomerShoppingCartsQueryHandler.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.ShoppingCarts.Models; -using LiteCharms.Features.ShoppingCarts.Queries; - -namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers; - -public class GetCustomerShoppingCartsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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(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(new Error($"No shopping carts found for customer with Id {request.CustomerId}.")); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartItemsQueryHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartItemsQueryHandler.cs deleted file mode 100644 index 0082f85..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartItemsQueryHandler.cs +++ /dev/null @@ -1,30 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers; - -public class GetShoppingCartItemsQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetShoppingCartItemsQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ShoppingCarts.AnyAsync(i => i.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}"); - - var items = await context.ShoppingCartItems.AsNoTracking() - .Where(i => i.ShoppingCartId == request.ShoppingCartId).ToArrayAsync(cancellationToken); - - return items?.Length > 0 - ? Result.Ok(items.Select(i => i.ToModel()).ToArray()) - : Result.Fail($"Failed to retrieve shopping cart items with id {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartPackagesQueryHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartPackagesQueryHandler.cs deleted file mode 100644 index 68e17cf..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartPackagesQueryHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers; - -public class GetShoppingCartPackagesQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> Handle(GetShoppingCartPackagesQuery request, CancellationToken cancellationToken) - { - try - { - using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - - if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken)) - return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}"); - - var packages = await context.ShoppingCartPackages.AsNoTracking() - .OrderByDescending(o => o.CreatedAt) - .Where(cp => cp.ShoppingCartId == request.ShoppingCartId) - .ToArrayAsync(cancellationToken); - - return packages?.Length > 0 - ? Result.Ok(packages.Select(p => p.ToModel()).ToArray()) - : Result.Fail($"Could not find packaged in shopping cart by ID {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartQueryHandler.cs b/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartQueryHandler.cs deleted file mode 100644 index 2cf05a9..0000000 --- a/LiteCharms.Features/Shop/ShoppingCarts/Queries/Handlers/GetShoppingCartQueryHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using LiteCharms.Extensions; -using LiteCharms.Features.Shop.Postgres; -using LiteCharms.Features.Shop.ShoppingCarts.Models; - -namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers; - -public class GetShoppingCartQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> -{ - public async ValueTask> 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($"Failed to find shopping cart with id {request.ShoppingCartId}"); - } - catch (Exception ex) - { - return Result.Fail(new Error(ex.Message).CausedBy(ex)); - } - } -} diff --git a/LiteCharms.Features/Shop/ShoppingCarts/ShoppingCartService.cs b/LiteCharms.Features/Shop/ShoppingCarts/ShoppingCartService.cs new file mode 100644 index 0000000..b4fdd2f --- /dev/null +++ b/LiteCharms.Features/Shop/ShoppingCarts/ShoppingCartService.cs @@ -0,0 +1,298 @@ +using LiteCharms.Features.Extensions; +using LiteCharms.Features.Shop.Postgres; +using LiteCharms.Features.Shop.ShoppingCarts.Models; +using static LiteCharms.Features.Extensions.Timezones; + +namespace LiteCharms.Features.Shop.ShoppingCarts; + +public class ShoppingCartService(IDbContextFactory contextFactory) +{ + public async ValueTask AddItemToShoppingCartAsync(Guid shoppingCartId, Guid productPriceId, int quantity, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ProductPrices.AnyAsync(c => c.Id == productPriceId, cancellationToken)) + return Result.Fail($"Product item could not be found with id {productPriceId}"); + + var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken); + + if (cart is null) + return Result.Fail($"Shopping cart could not be found with id {shoppingCartId}"); + + if (cart.ShoppingCartItems?.Any(i => i.ProductPriceId == productPriceId) == true) + return Result.Fail($"Item already in shopping cart with id {shoppingCartId}"); + + context.ShoppingCartItems.Add(new Entities.ShoppingCartItem + { + ShoppingCartId = shoppingCartId, + ProductPriceId = productPriceId, + Quantity = quantity + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to add cart item with id {productPriceId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask AddPackageToShoppingCartAsync(Guid shoppingCartId, Guid packageId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken)) + return Result.Fail($"Package cold not be found by ID {packageId}"); + + var shoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken); + + if (shoppingCart is null) + return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}"); + + if (!await context.ShoppingCartPackages.AnyAsync(cp => cp.ShoppingCartId == shoppingCartId, cancellationToken)) + return Result.Fail($"Package {packageId} is already in the cart"); + + var newShoppingCartPackage = context.ShoppingCartPackages.Add(new Entities.ShoppingCartPackage + { + ShoppingCartId = shoppingCartId, + PackageId = packageId + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Could not add package of id {packageId} to shopping cart {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> CreateShoppingCartAsync(Guid customerId, Guid orderId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken)) + return Result.Fail($"Customer could not be found with id {customerId}"); + + var cart = context.ShoppingCarts.Add(new Entities.ShoppingCart + { + CustomerId = customerId, + OrderId = orderId + }); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok(cart.Entity.Id) + : Result.Fail($"Failed to create shopping cart for customer id {customerId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask EmptyShoppingCartAsync(Guid shoppingCartId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken)) + return Result.Fail($"Shopping could not be found with id {shoppingCartId}"); + + if (await context.ShoppingCartItems.CountAsync(i => i.ShoppingCartId == shoppingCartId, cancellationToken) == 0) + return Result.Ok(); + + var cartItems = await context.ShoppingCartItems.Where(i => i.ShoppingCartId == shoppingCartId).ToListAsync(cancellationToken); + + context.RemoveRange(cartItems); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Could not empty cart with id {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetCustomerShoppingCartsAsync(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(new Error($"Customer with Id {customerId} does not exist.")); + + var shoppingCarts = await context.ShoppingCarts.Where(sc => sc.CustomerId == customerId).ToArrayAsync(cancellationToken); + + return shoppingCarts?.Length > 0 + ? Result.Ok(shoppingCarts.Select(c => c.ToModel()).ToArray()) + : Result.Fail(new Error($"No shopping carts found for customer with Id {customerId}.")); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetShoppingCartAsync(Guid shoppingCartId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var cart = await context.ShoppingCarts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken); + + return cart is not null + ? Result.Ok(cart.ToModel()) + : Result.Fail($"Failed to find shopping cart with id {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetShoppingCartItemsAsync(Guid shoppingCartId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ShoppingCarts.AnyAsync(i => i.Id == shoppingCartId, cancellationToken)) + return Result.Fail($"Shopping cart could not be found with id {shoppingCartId}"); + + var items = await context.ShoppingCartItems.AsNoTracking() + .Where(i => i.ShoppingCartId == shoppingCartId).ToArrayAsync(cancellationToken); + + return items?.Length > 0 + ? Result.Ok(items.Select(i => i.ToModel()).ToArray()) + : Result.Fail($"Failed to retrieve shopping cart items with id {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask> GetShoppingCartPackagesAsync(Guid shoppingCartId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken)) + return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}"); + + var packages = await context.ShoppingCartPackages.AsNoTracking() + .OrderByDescending(o => o.CreatedAt) + .Where(cp => cp.ShoppingCartId == shoppingCartId) + .ToArrayAsync(cancellationToken); + + return packages?.Length > 0 + ? Result.Ok(packages.Select(p => p.ToModel()).ToArray()) + : Result.Fail($"Could not find packaged in shopping cart by ID {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask RemovePackageFromShoppingCartAsync(Guid shoppingCartId, Guid shoppingCartPackageId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken)) + return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}"); + + if (!await context.ShoppingCartPackages.AnyAsync(p => p.Id == shoppingCartPackageId, cancellationToken)) + return Result.Fail($"Shopping cart package {shoppingCartPackageId} is not in the shopping cart {shoppingCartId}"); + + var shoppingCartPackage = await context.ShoppingCartPackages.FirstOrDefaultAsync(cp => cp.Id == shoppingCartPackageId, cancellationToken); + + if (shoppingCartPackage is null) + return Result.Ok(); + + context.ShoppingCartPackages.Remove(shoppingCartPackage!); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Could remove package of id {shoppingCartPackageId} from shopping cart {shoppingCartId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask RemoveShoppingCartItemAsync(Guid shoppingCartId, Guid shoppingCartItemId, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ProductPrices.AnyAsync(c => c.Id == shoppingCartItemId, cancellationToken)) + return Result.Fail($"Product item could not be found with id {shoppingCartItemId}"); + + var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken); + + if (cart is null) + return Result.Fail($"Shopping cart item could not be found with id {shoppingCartId}"); + + var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.Id == shoppingCartItemId, cancellationToken); + + if (item is null) return Result.Ok(); + + context.ShoppingCartItems.Remove(item); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to remove shopping cart item with id {shoppingCartItemId}"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + + public async ValueTask UpdateShoppingCartItemAsync(Guid shoppingCartId, Guid shoppingCartItemId, int quantity, CancellationToken cancellationToken = default) + { + try + { + using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken)) + return Result.Fail($"Shopping could not be found with id {shoppingCartId}"); + + var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.ShoppingCartId == shoppingCartId, cancellationToken); + + if (item is null) + return Result.Fail($"Shopping cart item could not be found with id {shoppingCartItemId}"); + + item.Quantity = quantity; + item.UpdatedAt = SouthAfricanTimeZone.UtcNow(); + + return await context.SaveChangesAsync(cancellationToken) > 0 + ? Result.Ok() + : Result.Fail($"Failed to update cart item quntity"); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } +}