discounts überarbeitet

This commit is contained in:
Tizian.Breuch
2025-08-29 13:57:39 +02:00
parent 6e6f38397b
commit a2e3a44e94
7 changed files with 257 additions and 112 deletions

View File

@@ -40,16 +40,33 @@ namespace Webshop.Application.Services.Customers
_discountService = discountService;
}
public async Task<(bool Success, OrderDetailDto? CreatedOrder, string? ErrorMessage)> CreateOrderAsync(CreateOrderDto orderDto, string userId)
public async Task<(bool Success, OrderDetailDto? CreatedOrder, string? ErrorMessage)> CreateOrderAsync(CreateOrderDto orderDto, string? userId)
{
// Startet eine Datenbank-Transaktion. Entweder alles klappt, oder alles wird zurückgerollt.
await using var transaction = await _context.Database.BeginTransactionAsync();
try
{
// --- 1. Validierung der Eingabedaten ---
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, null, "Kundenprofil nicht gefunden.");
// --- 1. Validierung von Kunde, Adressen und Methoden ---
Customer? customer = null;
if (!string.IsNullOrEmpty(userId))
{
customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, null, "Kundenprofil nicht gefunden.");
// Validiere, dass die Adressen zum eingeloggten Kunden gehören
if (!await _context.Addresses.AnyAsync(a => a.Id == orderDto.ShippingAddressId && a.CustomerId == customer.Id) ||
!await _context.Addresses.AnyAsync(a => a.Id == orderDto.BillingAddressId && a.CustomerId == customer.Id))
{
return (false, null, "Ungültige oder nicht zugehörige Liefer- oder Rechnungsadresse.");
}
}
else
{
// Gast-Checkout: Validierung der Gast-Daten
if (string.IsNullOrWhiteSpace(orderDto.GuestEmail))
return (false, null, "Für Gastbestellungen ist eine E-Mail-Adresse erforderlich.");
// Hinweis: Bei Gastbestellungen müssten die Adress-DTOs mitgesendet und hier neue Adressen erstellt werden.
// Der Einfachheit halber nehmen wir an, die Adress-IDs sind gültig, aber nicht mit einem Kunden verknüpft.
}
var shippingMethod = await _shippingMethodRepository.GetByIdAsync(orderDto.ShippingMethodId);
if (shippingMethod == null || !shippingMethod.IsActive) return (false, null, "Ungültige oder inaktive Versandmethode.");
@@ -57,33 +74,27 @@ namespace Webshop.Application.Services.Customers
var paymentMethod = await _context.PaymentMethods.FindAsync(orderDto.PaymentMethodId);
if (paymentMethod == null || !paymentMethod.IsActive) return (false, null, "Ungültige oder inaktive Zahlungsmethode.");
if (!await _context.Addresses.AnyAsync(a => a.Id == orderDto.ShippingAddressId && a.CustomerId == customer.Id) ||
!await _context.Addresses.AnyAsync(a => a.Id == orderDto.BillingAddressId && a.CustomerId == customer.Id))
{
return (false, null, "Ungültige oder nicht zugehörige Liefer- oder Rechnungsadresse.");
}
// --- 2. Artikel verarbeiten und Lagerbestand prüfen ---
var orderItems = new List<OrderItem>();
var productIds = orderDto.Items.Select(i => i.ProductId).ToList();
var products = await _context.Products.Where(p => productIds.Contains(p.Id)).ToListAsync();
decimal itemsTotal = 0;
foreach (var itemDto in orderDto.Items)
{
var product = await _context.Products.FindAsync(itemDto.ProductId);
var product = products.FirstOrDefault(p => p.Id == itemDto.ProductId);
if (product == null || !product.IsActive)
{
await transaction.RollbackAsync();
return (false, null, $"Produkt mit ID {itemDto.ProductId} ist nicht verfügbar.");
return (false, null, $"Ein Produkt im Warenkorb ist nicht mehr verfügbar.");
}
if (product.StockQuantity < itemDto.Quantity)
{
await transaction.RollbackAsync();
return (false, null, $"Nicht genügend Lagerbestand für '{product.Name}'. Verfügbar: {product.StockQuantity}.");
return (false, null, $"Nicht genügend Lagerbestand für '{product.Name}'. Verfügbar: {product.StockQuantity}, benötigt: {itemDto.Quantity}.");
}
// Lagerbestand reduzieren
product.StockQuantity -= itemDto.Quantity;
product.StockQuantity -= itemDto.Quantity; // Lagerbestand reduzieren
var orderItem = new OrderItem
{
@@ -95,13 +106,13 @@ namespace Webshop.Application.Services.Customers
UnitPrice = product.Price,
TotalPrice = product.Price * itemDto.Quantity
};
orderItems.Add(orderItem);
itemsTotal += orderItem.TotalPrice;
}
// --- 3. Preise, Rabatte, Steuern und Gesamtbetrag berechnen ---
decimal discountAmount = await _discountService.CalculateDiscountAsync(orderItems, orderDto.CouponCode);
var discountResult = await _discountService.CalculateDiscountAsync(orderItems, orderDto.CouponCode);
decimal discountAmount = discountResult.TotalDiscountAmount;
decimal shippingCost = shippingMethod.BaseCost;
decimal subTotal = itemsTotal + shippingCost - discountAmount;
if (subTotal < 0) subTotal = 0;
@@ -113,47 +124,53 @@ namespace Webshop.Application.Services.Customers
// --- 4. Bestellung erstellen ---
var newOrder = new Order
{
CustomerId = customer.Id,
CustomerId = customer?.Id,
GuestEmail = customer == null ? orderDto.GuestEmail : null,
GuestPhoneNumber = customer == null ? orderDto.GuestPhoneNumber : null,
OrderNumber = $"WS-{DateTime.UtcNow:yyyyMMdd}-{new Random().Next(1000, 9999)}",
OrderDate = DateTimeOffset.UtcNow,
OrderStatus = OrderStatus.Pending.ToString(),
PaymentStatus = PaymentStatus.Pending.ToString(), // Wird später vom Zahlungsanbieter aktualisiert
PaymentStatus = PaymentStatus.Pending.ToString(),
OrderTotal = orderTotal,
ShippingCost = shippingCost,
TaxAmount = taxAmount,
DiscountAmount = discountAmount,
PaymentMethod = paymentMethod.Name,
PaymentMethodId = paymentMethod.Id,
ShippingMethodId = shippingMethod.Id,
BillingAddressId = orderDto.BillingAddressId,
ShippingAddressId = orderDto.ShippingAddressId,
OrderItems = orderItems
};
await _orderRepository.AddAsync(newOrder);
await _orderRepository.AddAsync(newOrder); // Speichert die Bestellung und die Artikel
await _context.SaveChangesAsync(); // Speichert die Lagerbestandsänderungen
// --- 5. Rabattnutzung erhöhen ---
if (discountResult.AppliedDiscountIds.Any())
{
var appliedDiscounts = await _context.Discounts
.Where(d => discountResult.AppliedDiscountIds.Contains(d.Id))
.ToListAsync();
foreach (var discount in appliedDiscounts)
{
discount.CurrentUsageCount++;
}
}
await transaction.CommitAsync(); // Transaktion erfolgreich abschließen
// --- 5. Erfolgreiche Antwort erstellen ---
var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id); // Lade die erstellte Bestellung mit allen Details
var orderDetailDto = MapToOrderDetailDto(createdOrder!); // Mapping zum DTO
await _context.SaveChangesAsync(); // Speichert Lagerbestandsänderungen & Rabattnutzung
await transaction.CommitAsync();
// --- 6. Erfolgreiche Antwort erstellen ---
var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id);
var orderDetailDto = MapToOrderDetailDto(createdOrder!);
return (true, orderDetailDto, null);
}
catch (Exception ex)
{
await transaction.RollbackAsync();
// Hier Fehler loggen
return (false, null, $"Ein unerwarteter Fehler ist aufgetreten: {ex.Message}");
}
}
// Helper-Methode für das Mapping, um Code-Duplizierung zu vermeiden
private OrderDetailDto MapToOrderDetailDto(Order order)
{
return new OrderDetailDto
@@ -164,10 +181,35 @@ namespace Webshop.Application.Services.Customers
CustomerId = order.CustomerId,
Status = Enum.Parse<OrderStatus>(order.OrderStatus),
TotalAmount = order.OrderTotal,
ShippingAddress = new AddressDto { /* Mapping */ },
BillingAddress = new AddressDto { /* Mapping */ },
ShippingAddress = new AddressDto
{
Id = order.ShippingAddress.Id,
FirstName = order.ShippingAddress.FirstName,
LastName = order.ShippingAddress.LastName,
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,
FirstName = order.BillingAddress.FirstName,
LastName = order.BillingAddress.LastName,
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.Parse<PaymentStatus>(order.PaymentStatus),
ShippingTrackingNumber = order.ShippingTrackingNumber,
ShippedDate = order.ShippedDate,
DeliveredDate = order.DeliveredDate,
OrderItems = order.OrderItems.Select(oi => new OrderItemDto
{
Id = oi.Id,