119 lines
5.1 KiB
C#
119 lines
5.1 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;
|
|
using Webshop.Application.DTOs.Customers;
|
|
using Webshop.Application.DTOs.Orders;
|
|
using Webshop.Domain.Enums;
|
|
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<ServiceResult<IEnumerable<OrderSummaryDto>>> GetAllOrdersAsync()
|
|
{
|
|
var orders = await _orderRepository.GetAllAsync();
|
|
var dtos = 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,
|
|
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();
|
|
|
|
return ServiceResult.Ok<IEnumerable<OrderSummaryDto>>(dtos);
|
|
}
|
|
|
|
public async Task<ServiceResult<OrderDetailDto>> GetOrderByIdAsync(Guid id)
|
|
{
|
|
var order = await _orderRepository.GetByIdAsync(id);
|
|
if (order == null)
|
|
{
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.NotFound, $"Bestellung mit ID '{id}' nicht gefunden.");
|
|
}
|
|
|
|
var orderDetailDto = 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,
|
|
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()
|
|
};
|
|
|
|
return ServiceResult.Ok(orderDetailDto);
|
|
}
|
|
|
|
public async Task<ServiceResult> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus)
|
|
{
|
|
if (!Enum.IsDefined(typeof(OrderStatus), newStatus))
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Der Status '{newStatus}' ist ungültig.");
|
|
}
|
|
|
|
var order = await _orderRepository.GetByIdAsync(id);
|
|
if (order == null)
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Bestellung mit ID '{id}' nicht gefunden.");
|
|
}
|
|
|
|
order.OrderStatus = newStatus.ToString();
|
|
|
|
// Hier könnte zusätzliche Logik stehen (z.B. Tracking-Nummer verlangen)
|
|
if (newStatus == OrderStatus.Shipped && string.IsNullOrEmpty(order.ShippingTrackingNumber))
|
|
{
|
|
// return ServiceResult.Fail(ServiceResultType.InvalidInput, "Für den Status 'Versandt' wird eine Tracking-Nummer benötigt.");
|
|
}
|
|
|
|
await _orderRepository.UpdateAsync(order);
|
|
return ServiceResult.Ok();
|
|
}
|
|
}
|
|
} |