From e915d5a105b8d8fee43a3ece35966466b64cb977 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 10 Oct 2025 13:35:08 +0200 Subject: [PATCH] =?UTF-8?q?order=20service=20=C3=BCberarbeitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Webshop.Api/Program.cs | 2 +- .../Services/Customers/OrderService.cs | 25 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 381c798..ac3ccd1 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -271,7 +271,7 @@ using (var scope = app.Services.CreateScope()) if (customerUser == null) { customerUser = new ApplicationUser { UserName = "customer@yourwebshop.com", Email = "customer@yourwebshop.com", EmailConfirmed = true, CreatedDate = DateTimeOffset.UtcNow, LastActive = DateTimeOffset.UtcNow }; - var createCustomerResult = await userManager.CreateAsync(customerUser, "SecureCustomerPass12-3!"); + var createCustomerResult = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!"); if (createCustomerResult.Succeeded) { await userManager.AddToRoleAsync(customerUser, "Customer"); diff --git a/Webshop.Application/Services/Customers/OrderService.cs b/Webshop.Application/Services/Customers/OrderService.cs index a2fcbcd..340f706 100644 --- a/Webshop.Application/Services/Customers/OrderService.cs +++ b/Webshop.Application/Services/Customers/OrderService.cs @@ -30,13 +30,15 @@ namespace Webshop.Application.Services.Customers var customer = await _customerRepository.GetByUserIdAsync(userId); if (customer == null) { - // A valid user might not have a customer profile yet. Return empty list. return ServiceResult.Ok>(new List()); } var orders = await _context.Orders .Where(o => o.CustomerId == customer.Id) .OrderByDescending(o => o.OrderDate) + // +++ KORREKTUR: Verknüpfte Entitäten explizit laden +++ + .Include(o => o.ShippingMethodInfo) + .Include(o => o.PaymentMethodInfo) .ToListAsync(); var dtos = orders.Select(o => new OrderSummaryDto @@ -47,7 +49,10 @@ namespace Webshop.Application.Services.Customers CustomerName = $"{customer.FirstName} {customer.LastName}", TotalAmount = o.OrderTotal, Status = Enum.TryParse(o.OrderStatus, true, out var os) ? os : OrderStatus.Pending, - PaymentStatus = Enum.TryParse(o.PaymentStatus, true, out var ps) ? ps : PaymentStatus.Pending + PaymentStatus = Enum.TryParse(o.PaymentStatus, true, out var ps) ? ps : PaymentStatus.Pending, + // +++ KORREKTUR: Daten aus den geladenen Entitäten zuweisen +++ + ShippingMethodName = o.ShippingMethodInfo?.Name, + PaymentMethodName = o.PaymentMethodInfo?.Name }).ToList(); return ServiceResult.Ok>(dtos); @@ -65,6 +70,9 @@ namespace Webshop.Application.Services.Customers .Include(o => o.BillingAddress) .Include(o => o.ShippingAddress) .Include(o => o.OrderItems) + // +++ KORREKTUR: Verknüpfte Entitäten explizit laden +++ + .Include(o => o.ShippingMethodInfo) + .Include(o => o.PaymentMethodInfo) .FirstOrDefaultAsync(o => o.Id == orderId); if (order == null) @@ -82,6 +90,8 @@ namespace Webshop.Application.Services.Customers private OrderDetailDto MapToOrderDetailDto(Order order) { + // Die `MapToDto`-Methode muss die neuen Daten ebenfalls verwenden. + // Die `PaymentMethod`-Eigenschaft in `OrderDetailDto` scheint den Namen zu erwarten. return new OrderDetailDto { Id = order.Id, @@ -98,7 +108,9 @@ namespace Webshop.Application.Services.Customers City = order.ShippingAddress.City, PostalCode = order.ShippingAddress.PostalCode, Country = order.ShippingAddress.Country, - Type = order.ShippingAddress.Type + Type = order.ShippingAddress.Type, + FirstName = order.ShippingAddress.FirstName, + LastName = order.ShippingAddress.LastName }, BillingAddress = new AddressDto { @@ -108,9 +120,12 @@ namespace Webshop.Application.Services.Customers City = order.BillingAddress.City, PostalCode = order.BillingAddress.PostalCode, Country = order.BillingAddress.Country, - Type = order.BillingAddress.Type + Type = order.BillingAddress.Type, + FirstName = order.BillingAddress.FirstName, + LastName = order.BillingAddress.LastName }, - PaymentMethod = order.PaymentMethod, + // +++ KORREKTUR: Den Namen aus der geladenen Entität verwenden +++ + PaymentMethod = order.PaymentMethodInfo?.Name, PaymentStatus = Enum.TryParse(order.PaymentStatus, true, out var ps) ? ps : PaymentStatus.Pending, ShippingTrackingNumber = order.ShippingTrackingNumber, ShippedDate = order.ShippedDate,