discounts überarbeitet
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// src/Webshop.Application/Services/Admin/AdminDiscountService.cs
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -8,16 +9,19 @@ using Webshop.Application.Services.Admin.Interfaces;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Enums;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Infrastructure.Data;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminDiscountService : IAdminDiscountService
|
||||
{
|
||||
private readonly IDiscountRepository _discountRepository;
|
||||
private readonly ApplicationDbContext _context; // F<>r Unique-Pr<50>fungen
|
||||
|
||||
public AdminDiscountService(IDiscountRepository discountRepository)
|
||||
public AdminDiscountService(IDiscountRepository discountRepository, ApplicationDbContext context)
|
||||
{
|
||||
_discountRepository = discountRepository;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync()
|
||||
@@ -32,19 +36,41 @@ namespace Webshop.Application.Services.Admin
|
||||
return discount != null ? MapToDto(discount) : null;
|
||||
}
|
||||
|
||||
public async Task<DiscountDto?> CreateDiscountAsync(DiscountDto discountDto)
|
||||
public async Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto)
|
||||
{
|
||||
// Validierung: Gutscheincode muss eindeutig sein, wenn er ben<65>tigt wird
|
||||
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
||||
{
|
||||
var existing = await _context.Discounts.FirstOrDefaultAsync(d => d.CouponCode == discountDto.CouponCode);
|
||||
if (existing != null)
|
||||
{
|
||||
return ServiceResult.Fail<DiscountDto>(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' existiert bereits.");
|
||||
}
|
||||
}
|
||||
|
||||
var discount = MapToEntity(discountDto);
|
||||
discount.Id = Guid.NewGuid();
|
||||
|
||||
await _discountRepository.AddAsync(discount);
|
||||
return MapToDto(discount);
|
||||
return ServiceResult.Ok(MapToDto(discount));
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateDiscountAsync(DiscountDto discountDto)
|
||||
public async Task<ServiceResult> UpdateDiscountAsync(DiscountDto discountDto)
|
||||
{
|
||||
var existingDiscount = await _discountRepository.GetByIdAsync(discountDto.Id);
|
||||
if (existingDiscount == null) return false;
|
||||
if (existingDiscount == null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
||||
}
|
||||
|
||||
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
||||
{
|
||||
var existing = await _context.Discounts.FirstOrDefaultAsync(d => d.CouponCode == discountDto.CouponCode && d.Id != discountDto.Id);
|
||||
if (existing != null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' wird bereits von einem anderen Rabatt verwendet.");
|
||||
}
|
||||
}
|
||||
|
||||
// Update simple properties
|
||||
existingDiscount.Name = discountDto.Name;
|
||||
@@ -59,33 +85,39 @@ namespace Webshop.Application.Services.Admin
|
||||
existingDiscount.MinimumOrderAmount = discountDto.MinimumOrderAmount;
|
||||
existingDiscount.MaximumUsageCount = discountDto.MaximumUsageCount;
|
||||
|
||||
// << HIER IST DER WICHTIGE, WIEDERHERGESTELLTE TEIL >>
|
||||
// Sync assigned products
|
||||
existingDiscount.ProductDiscounts.Clear();
|
||||
foreach (var productId in discountDto.AssignedProductIds)
|
||||
{
|
||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { ProductId = productId });
|
||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { DiscountId = existingDiscount.Id, ProductId = productId });
|
||||
}
|
||||
|
||||
// Sync assigned categories
|
||||
existingDiscount.categorieDiscounts.Clear();
|
||||
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
||||
{
|
||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { categorieId = categoryId });
|
||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { DiscountId = existingDiscount.Id, categorieId = categoryId });
|
||||
}
|
||||
// << ENDE DES WICHTIGEN TEILS >>
|
||||
|
||||
await _discountRepository.UpdateAsync(existingDiscount);
|
||||
return true;
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteDiscountAsync(Guid id)
|
||||
public async Task<ServiceResult> DeleteDiscountAsync(Guid id)
|
||||
{
|
||||
var discount = await _discountRepository.GetByIdAsync(id);
|
||||
if (discount == null) return false;
|
||||
if (discount == null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
||||
}
|
||||
|
||||
await _discountRepository.DeleteAsync(id);
|
||||
return true;
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
|
||||
// Helper methods for mapping
|
||||
private DiscountDto MapToDto(Discount discount)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user