From fab0e517d7e5696bb7207df970f8243d2800c652 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Tue, 30 Sep 2025 19:27:58 +0200 Subject: [PATCH] fix shipping --- .../Admin/AdminShippingMethodService.cs | 29 +++++++++++++++---- Webshop.Domain/Entities/ShippingMethod.cs | 3 ++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Webshop.Application/Services/Admin/AdminShippingMethodService.cs b/Webshop.Application/Services/Admin/AdminShippingMethodService.cs index 0cf8c7d..7281303 100644 --- a/Webshop.Application/Services/Admin/AdminShippingMethodService.cs +++ b/Webshop.Application/Services/Admin/AdminShippingMethodService.cs @@ -3,20 +3,24 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; // Hinzufügen für .AnyAsync using Webshop.Application; using Webshop.Application.DTOs.Shipping; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; +using Webshop.Infrastructure.Data; // Hinzufügen für DbContext namespace Webshop.Application.Services.Admin { public class AdminShippingMethodService : IAdminShippingMethodService { private readonly IShippingMethodRepository _shippingMethodRepository; + private readonly ApplicationDbContext _context; - public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository) + public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository, ApplicationDbContext context) { _shippingMethodRepository = shippingMethodRepository; + _context = context; } public async Task>> GetAllAsync() @@ -50,13 +54,16 @@ namespace Webshop.Application.Services.Admin Name = shippingMethodDto.Name, Description = shippingMethodDto.Description, BaseCost = shippingMethodDto.Cost, - IsActive = shippingMethodDto.IsActive + IsActive = shippingMethodDto.IsActive, + // +++ KORREKTUR +++ + MinDeliveryDays = shippingMethodDto.MinDeliveryDays, + MaxDeliveryDays = shippingMethodDto.MaxDeliveryDays }; await _shippingMethodRepository.AddAsync(newMethod); - shippingMethodDto.Id = newMethod.Id; - return ServiceResult.Ok(shippingMethodDto); + // Verwende MapToDto, um eine konsistente Antwort zu gewährleisten + return ServiceResult.Ok(MapToDto(newMethod)); } public async Task UpdateAsync(ShippingMethodDto shippingMethodDto) @@ -77,6 +84,9 @@ namespace Webshop.Application.Services.Admin existingMethod.Description = shippingMethodDto.Description; existingMethod.BaseCost = shippingMethodDto.Cost; existingMethod.IsActive = shippingMethodDto.IsActive; + // +++ KORREKTUR +++ + existingMethod.MinDeliveryDays = shippingMethodDto.MinDeliveryDays; + existingMethod.MaxDeliveryDays = shippingMethodDto.MaxDeliveryDays; await _shippingMethodRepository.UpdateAsync(existingMethod); return ServiceResult.Ok(); @@ -90,6 +100,12 @@ namespace Webshop.Application.Services.Admin return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden."); } + var isUsedInOrders = await _context.Orders.AnyAsync(o => o.ShippingMethodId == id); + if (isUsedInOrders) + { + return ServiceResult.Fail(ServiceResultType.Conflict, "Diese Versandmethode kann nicht gelöscht werden, da sie noch in bestehenden Bestellungen verwendet wird."); + } + await _shippingMethodRepository.DeleteAsync(id); return ServiceResult.Ok(); } @@ -102,7 +118,10 @@ namespace Webshop.Application.Services.Admin Name = sm.Name, Description = sm.Description, Cost = sm.BaseCost, - IsActive = sm.IsActive + IsActive = sm.IsActive, + // +++ KORREKTUR +++ + MinDeliveryDays = sm.MinDeliveryDays, + MaxDeliveryDays = sm.MaxDeliveryDays }; } } diff --git a/Webshop.Domain/Entities/ShippingMethod.cs b/Webshop.Domain/Entities/ShippingMethod.cs index caddd44..fe4c767 100644 --- a/Webshop.Domain/Entities/ShippingMethod.cs +++ b/Webshop.Domain/Entities/ShippingMethod.cs @@ -32,4 +32,7 @@ public class ShippingMethod [Required] public bool RequiresTracking { get; set; } + + public int MinDeliveryDays { get; set; } + public int MaxDeliveryDays { get; set; } } \ No newline at end of file