Files
ShopSolution-backend/Webshop.Application/Services/Admin/AdminOrderService.cs
Tizian.Breuch 96f082b38f order by id fix
2025-08-01 14:56:40 +02:00

110 lines
4.6 KiB
C#

// src/Webshop.Application/Services/Admin/AdminOrderService.cs
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.Enums; // Wichtig für Enum-Konvertierung
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminOrderService : IAdminOrderService
{
private readonly IOrderRepository _orderRepository;
public AdminOrderService(IOrderRepository orderRepository)
{
_orderRepository = orderRepository;
}
public async Task<IEnumerable<OrderSummaryDto>> GetAllOrdersAsync()
{
var orders = await _orderRepository.GetAllAsync();
return orders.Select(o => new OrderSummaryDto
{
Id = o.Id,
OrderNumber = o.OrderNumber,
OrderDate = o.OrderDate,
CustomerName = o.Customer != null ? $"{o.Customer.FirstName} {o.Customer.LastName}" : o.GuestEmail ?? "Gastbestellung",
TotalAmount = o.OrderTotal,
// << KORREKTUR: String zu Enum parsen >>
Status = Enum.TryParse<OrderStatus>(o.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
PaymentStatus = Enum.TryParse<PaymentStatus>(o.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending
}).ToList();
}
public async Task<OrderDetailDto?> GetOrderByIdAsync(Guid id)
{
var order = await _orderRepository.GetByIdAsync(id);
if (order == null) return null;
return new OrderDetailDto
{
Id = order.Id,
OrderNumber = order.OrderNumber,
OrderDate = order.OrderDate,
CustomerId = order.CustomerId,
Status = Enum.TryParse<OrderStatus>(order.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
TotalAmount = order.OrderTotal,
// << KORREKTUR: Manuelles Mapping von Address-Entität zu AddressDto >>
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,
OrderItems = order.OrderItems.Select(oi => new OrderItemDto
{
Id = oi.Id,
ProductId = oi.ProductId,
ProductVariantId = oi.ProductVariantId,
ProductName = oi.ProductName,
ProductSKU = oi.ProductSKU,
Quantity = oi.Quantity,
UnitPrice = oi.UnitPrice,
TotalPrice = oi.TotalPrice
}).ToList()
};
}
public async Task<(bool Success, string? ErrorMessage)> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus)
{
var order = await _orderRepository.GetByIdAsync(id);
if (order == null)
{
return (false, "Bestellung nicht gefunden.");
}
// << KORREKTUR: Enum zu String konvertieren >>
order.OrderStatus = newStatus.ToString();
if (newStatus == OrderStatus.Shipped && string.IsNullOrEmpty(order.ShippingTrackingNumber))
{
// Hier könnten Sie Logik hinzufügen, um eine Tracking-Nummer zu verlangen
}
await _orderRepository.UpdateAsync(order);
return (true, null);
}
}
}