From 16832ec2143f9e27ba92e52871cb7dea5728a13c Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Mon, 15 Jun 2026 12:06:33 +0200 Subject: [PATCH 1/2] Added GetOrderItems to OrderService --- .../Orders/OrderService.cs | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs b/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs index e94cded..225f76d 100644 --- a/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs +++ b/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs @@ -43,7 +43,7 @@ public sealed class OrderService(IDbContextFactory contex if (!await context.Orders.AnyAsync(o => o.Id == orderId, cancellationToken)) return Result.Fail("Order not found."); - if(!await context.Books.AnyAsync(ab => ab.Id == request.AuthorBookId, cancellationToken)) + if (!await context.Books.AnyAsync(ab => ab.Id == request.AuthorBookId, cancellationToken)) return Result.Fail("Author book not found."); if (!await context.Prices.AnyAsync(pp => pp.Id == request.ProductPriceId, cancellationToken)) @@ -51,7 +51,7 @@ public sealed class OrderService(IDbContextFactory contex var existingItem = await context.OrderItems.FirstOrDefaultAsync(i => i.ProductPriceId == request.ProductPriceId && i.OrderId == orderId, cancellationToken); - if(existingItem is not null) + if (existingItem is not null) { existingItem.Quantity += request.Quantity; @@ -82,7 +82,7 @@ public sealed class OrderService(IDbContextFactory contex { try { - if(items.Length == 0) + if (items.Length == 0) return Result.Fail("No items to add."); await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); @@ -203,13 +203,33 @@ public sealed class OrderService(IDbContextFactory contex } } + public async ValueTask> GetOrderItemsAsync(long orderId, CancellationToken cancellationToken = default) + { + try + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + var orderItems = await context.OrderItems + .Where(o => o.OrderId == orderId) + .ToListAsync(cancellationToken); + + return orderItems.Count > 0 + ? Result.Ok(orderItems.Select(i => i.ToModel()).ToArray()) + : Result.Fail("Order item not found or failed to remove."); + } + catch (Exception ex) + { + return Result.Fail(new Error(ex.Message).CausedBy(ex)); + } + } + public async ValueTask> GetOrdersByCustomerAsync(long customerId, CancellationToken cancellationToken = default) { try { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if(!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken)) + if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken)) return Result.Fail("Customer not found."); var orders = await context.Orders @@ -276,16 +296,16 @@ public sealed class OrderService(IDbContextFactory contex { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if(!await context.Orders.AnyAsync(o => o.Id == orderId, cancellationToken)) + if (!await context.Orders.AnyAsync(o => o.Id == orderId, cancellationToken)) return Result.Fail("Order not found."); - if(!await context.Addresses.AnyAsync(a => a.Id == request.AddressId, cancellationToken)) + if (!await context.Addresses.AnyAsync(a => a.Id == request.AddressId, cancellationToken)) return Result.Fail("Address not found."); - if(!await context.ShippingProviders.AnyAsync(sp => sp.Id == request.ShippingProviderId && sp.Enabled, cancellationToken)) + if (!await context.ShippingProviders.AnyAsync(sp => sp.Id == request.ShippingProviderId && sp.Enabled, cancellationToken)) return Result.Fail("Shipping provider not found or disabled."); - if(await context.Shippings.AnyAsync(s => s.OrderId == orderId, cancellationToken)) + if (await context.Shippings.AnyAsync(s => s.OrderId == orderId, cancellationToken)) return Result.Fail("Shipping already exists for this order."); var shipping = context.Shippings.Add(new Entities.Shipping @@ -355,7 +375,7 @@ public sealed class OrderService(IDbContextFactory contex try { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - + var rowsDeleted = await context.Shippings .Where(s => s.Id == shippingId && s.OrderId == orderId) .ExecuteDeleteAsync(cancellationToken); @@ -398,7 +418,7 @@ public sealed class OrderService(IDbContextFactory contex { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if(await context.ShippingProviders.AnyAsync(sp => sp.Type == request.Type, cancellationToken)) + if (await context.ShippingProviders.AnyAsync(sp => sp.Type == request.Type, cancellationToken)) return Result.Fail("Shipping provider with the same type already exists."); var shippingProvider = context.ShippingProviders.Add(new Entities.ShippingProvider From 516062ed5dd413831aed9dfaa8140b1511ab0d1e Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Mon, 15 Jun 2026 12:08:19 +0200 Subject: [PATCH 2/2] Refactored order item retrieval error message --- LiteCharms.Features.MidrandBooks/Orders/OrderService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs b/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs index 225f76d..d0bb34a 100644 --- a/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs +++ b/LiteCharms.Features.MidrandBooks/Orders/OrderService.cs @@ -215,7 +215,7 @@ public sealed class OrderService(IDbContextFactory contex return orderItems.Count > 0 ? Result.Ok(orderItems.Select(i => i.ToModel()).ToArray()) - : Result.Fail("Order item not found or failed to remove."); + : Result.Fail($"Order items not found for order ID {orderId}"); } catch (Exception ex) {