admindiscount
This commit is contained in:
@@ -4,47 +4,49 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
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, ApplicationDbContext context)
|
||||
public AdminDiscountService(IDiscountRepository discountRepository)
|
||||
{
|
||||
_discountRepository = discountRepository;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync()
|
||||
public async Task<ServiceResult<IEnumerable<DiscountDto>>> GetAllDiscountsAsync()
|
||||
{
|
||||
var discounts = await _discountRepository.GetAllAsync();
|
||||
return discounts.Select(MapToDto).ToList();
|
||||
var dtos = discounts.Select(MapToDto).ToList();
|
||||
return ServiceResult.Ok<IEnumerable<DiscountDto>>(dtos);
|
||||
}
|
||||
|
||||
public async Task<DiscountDto?> GetDiscountByIdAsync(Guid id)
|
||||
public async Task<ServiceResult<DiscountDto>> GetDiscountByIdAsync(Guid id)
|
||||
{
|
||||
var discount = await _discountRepository.GetByIdAsync(id);
|
||||
return discount != null ? MapToDto(discount) : null;
|
||||
if (discount == null)
|
||||
{
|
||||
return ServiceResult.Fail<DiscountDto>(ServiceResultType.NotFound, $"Rabatt mit ID '{id}' nicht gefunden.");
|
||||
}
|
||||
return ServiceResult.Ok(MapToDto(discount));
|
||||
}
|
||||
|
||||
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);
|
||||
var existing = await _discountRepository.GetByCouponCodeAsync(discountDto.CouponCode);
|
||||
if (existing != null)
|
||||
{
|
||||
return ServiceResult.Fail<DiscountDto>(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' existiert bereits.");
|
||||
return ServiceResult.Fail<DiscountDto>(ServiceResultType.Conflict, $"Der Gutscheincode '{discountDto.CouponCode}' existiert bereits.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,15 +62,15 @@ namespace Webshop.Application.Services.Admin
|
||||
var existingDiscount = await _discountRepository.GetByIdAsync(discountDto.Id);
|
||||
if (existingDiscount == null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, $"Rabatt mit ID '{discountDto.Id}' 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)
|
||||
var existing = await _discountRepository.GetByCouponCodeAsync(discountDto.CouponCode);
|
||||
if (existing != null && existing.Id != discountDto.Id)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' wird bereits von einem anderen Rabatt verwendet.");
|
||||
return ServiceResult.Fail(ServiceResultType.Conflict, $"Der Gutscheincode '{discountDto.CouponCode}' wird bereits von einem anderen Rabatt verwendet.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,25 +83,29 @@ namespace Webshop.Application.Services.Admin
|
||||
existingDiscount.EndDate = discountDto.EndDate;
|
||||
existingDiscount.IsActive = discountDto.IsActive;
|
||||
existingDiscount.RequiresCouponCode = discountDto.RequiresCouponCode;
|
||||
existingDiscount.CouponCode = discountDto.CouponCode;
|
||||
existingDiscount.CouponCode = discountDto.RequiresCouponCode ? discountDto.CouponCode : null;
|
||||
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)
|
||||
if (discountDto.AssignedProductIds != null)
|
||||
{
|
||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { DiscountId = existingDiscount.Id, ProductId = productId });
|
||||
foreach (var productId in discountDto.AssignedProductIds)
|
||||
{
|
||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { DiscountId = existingDiscount.Id, ProductId = productId });
|
||||
}
|
||||
}
|
||||
|
||||
// Sync assigned categories
|
||||
existingDiscount.categorieDiscounts.Clear();
|
||||
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
||||
if (discountDto.AssignedCategoryIds != null)
|
||||
{
|
||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { DiscountId = existingDiscount.Id, categorieId = categoryId });
|
||||
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
||||
{
|
||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { DiscountId = existingDiscount.Id, categorieId = categoryId });
|
||||
}
|
||||
}
|
||||
// << ENDE DES WICHTIGEN TEILS >>
|
||||
|
||||
await _discountRepository.UpdateAsync(existingDiscount);
|
||||
return ServiceResult.Ok();
|
||||
@@ -110,7 +116,7 @@ namespace Webshop.Application.Services.Admin
|
||||
var discount = await _discountRepository.GetByIdAsync(id);
|
||||
if (discount == null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, $"Rabatt mit ID '{id}' nicht gefunden.");
|
||||
}
|
||||
|
||||
await _discountRepository.DeleteAsync(id);
|
||||
@@ -154,7 +160,7 @@ namespace Webshop.Application.Services.Admin
|
||||
EndDate = dto.EndDate,
|
||||
IsActive = dto.IsActive,
|
||||
RequiresCouponCode = dto.RequiresCouponCode,
|
||||
CouponCode = dto.CouponCode,
|
||||
CouponCode = dto.RequiresCouponCode ? dto.CouponCode : null,
|
||||
MinimumOrderAmount = dto.MinimumOrderAmount,
|
||||
MaximumUsageCount = dto.MaximumUsageCount,
|
||||
CurrentUsageCount = dto.CurrentUsageCount,
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application; // << NEU: F<>r ServiceResult >>
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminDiscountService
|
||||
{
|
||||
Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync();
|
||||
Task<DiscountDto?> GetDiscountByIdAsync(Guid id);
|
||||
Task<ServiceResult<IEnumerable<DiscountDto>>> GetAllDiscountsAsync();
|
||||
Task<ServiceResult<DiscountDto>> GetDiscountByIdAsync(Guid id);
|
||||
Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto);
|
||||
Task<ServiceResult> UpdateDiscountAsync(DiscountDto discountDto);
|
||||
Task<ServiceResult> DeleteDiscountAsync(Guid id);
|
||||
|
||||
Reference in New Issue
Block a user