order by id fix

This commit is contained in:
Tizian.Breuch
2025-08-01 14:56:40 +02:00
parent 587990295d
commit 96f082b38f
5 changed files with 64 additions and 35 deletions

View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.DTOs.Orders;
using Webshop.Domain.Entities;
using Webshop.Domain.Enums;
@@ -163,24 +164,23 @@ namespace Webshop.Application.Services.Customers
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null)
{
return null; // Kein Kunde, kann keine Bestellung abrufen
return null;
}
// Finde die Bestellung und stelle sicher, dass sie zum eingeloggten Kunden gehört
var order = await _context.Orders
.Include(o => o.BillingAddress)
.Include(o => o.ShippingAddress)
.Include(o => o.PaymentMethodInfo) // << HINZUFÜGEN, um PaymentMethodInfo zu laden >>
.Include(o => o.ShippingMethodInfo) // << HINZUFÜGEN, um ShippingMethodInfo zu laden >>
.Include(o => o.BillingAddress) // Lade die Address-Entität
.Include(o => o.ShippingAddress) // Lade die Address-Entität
.Include(o => o.PaymentMethodInfo)
.Include(o => o.OrderItems)
.ThenInclude(oi => oi.Product) // Optional: Dann das Produkt laden
.ThenInclude(oi => oi.Product)
.FirstOrDefaultAsync(o => o.Id == orderId && o.CustomerId == customer.Id);
if (order == null)
{
return null; // Bestellung nicht gefunden oder gehört nicht diesem Kunden
return null;
}
// << KORREKTUR: Manuelles Mapping von Address-Entität zu AddressDto >>
return new OrderDetailDto
{
Id = order.Id,
@@ -189,9 +189,27 @@ namespace Webshop.Application.Services.Customers
CustomerId = order.CustomerId,
Status = Enum.TryParse<OrderStatus>(order.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
TotalAmount = order.OrderTotal,
ShippingAddress = order.ShippingAddress, // Annahme: Address DTO Mapping
BillingAddress = order.BillingAddress, // Annahme: Address DTO Mapping
PaymentMethod = order.PaymentMethod, // << KORREKTUR: Dies ist ein String in Ihrer Entität >>
ShippingAddress = new AddressDto
{
Id = order.ShippingAddress.Id,
Street = order.ShippingAddress.Street,
HouseNumber = order.ShippingAddress.HouseNumber,
City = order.ShippingAddress.City,
PostalCode = order.ShippingAddress.PostalCode,
Country = order.ShippingAddress.Country,
Type = order.ShippingAddress.Type
},
BillingAddress = new AddressDto
{
Id = order.BillingAddress.Id,
Street = order.BillingAddress.Street,
HouseNumber = order.BillingAddress.HouseNumber,
City = order.BillingAddress.City,
PostalCode = order.BillingAddress.PostalCode,
Country = order.BillingAddress.Country,
Type = order.BillingAddress.Type
},
PaymentMethod = order.PaymentMethod,
PaymentStatus = Enum.TryParse<PaymentStatus>(order.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending,
ShippingTrackingNumber = order.ShippingTrackingNumber,
ShippedDate = order.ShippedDate,