fix shipping

This commit is contained in:
Tizian.Breuch
2025-09-30 19:27:58 +02:00
parent 7aab45702d
commit fab0e517d7
2 changed files with 27 additions and 5 deletions

View File

@@ -3,20 +3,24 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; // Hinzufügen für .AnyAsync
using Webshop.Application; using Webshop.Application;
using Webshop.Application.DTOs.Shipping; using Webshop.Application.DTOs.Shipping;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces; using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data; // Hinzufügen für DbContext
namespace Webshop.Application.Services.Admin namespace Webshop.Application.Services.Admin
{ {
public class AdminShippingMethodService : IAdminShippingMethodService public class AdminShippingMethodService : IAdminShippingMethodService
{ {
private readonly IShippingMethodRepository _shippingMethodRepository; private readonly IShippingMethodRepository _shippingMethodRepository;
private readonly ApplicationDbContext _context;
public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository) public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository, ApplicationDbContext context)
{ {
_shippingMethodRepository = shippingMethodRepository; _shippingMethodRepository = shippingMethodRepository;
_context = context;
} }
public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync() public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync()
@@ -50,13 +54,16 @@ namespace Webshop.Application.Services.Admin
Name = shippingMethodDto.Name, Name = shippingMethodDto.Name,
Description = shippingMethodDto.Description, Description = shippingMethodDto.Description,
BaseCost = shippingMethodDto.Cost, BaseCost = shippingMethodDto.Cost,
IsActive = shippingMethodDto.IsActive IsActive = shippingMethodDto.IsActive,
// +++ KORREKTUR +++
MinDeliveryDays = shippingMethodDto.MinDeliveryDays,
MaxDeliveryDays = shippingMethodDto.MaxDeliveryDays
}; };
await _shippingMethodRepository.AddAsync(newMethod); await _shippingMethodRepository.AddAsync(newMethod);
shippingMethodDto.Id = newMethod.Id; // Verwende MapToDto, um eine konsistente Antwort zu gewährleisten
return ServiceResult.Ok(shippingMethodDto); return ServiceResult.Ok(MapToDto(newMethod));
} }
public async Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto) public async Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto)
@@ -77,6 +84,9 @@ namespace Webshop.Application.Services.Admin
existingMethod.Description = shippingMethodDto.Description; existingMethod.Description = shippingMethodDto.Description;
existingMethod.BaseCost = shippingMethodDto.Cost; existingMethod.BaseCost = shippingMethodDto.Cost;
existingMethod.IsActive = shippingMethodDto.IsActive; existingMethod.IsActive = shippingMethodDto.IsActive;
// +++ KORREKTUR +++
existingMethod.MinDeliveryDays = shippingMethodDto.MinDeliveryDays;
existingMethod.MaxDeliveryDays = shippingMethodDto.MaxDeliveryDays;
await _shippingMethodRepository.UpdateAsync(existingMethod); await _shippingMethodRepository.UpdateAsync(existingMethod);
return ServiceResult.Ok(); return ServiceResult.Ok();
@@ -90,6 +100,12 @@ namespace Webshop.Application.Services.Admin
return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden."); 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); await _shippingMethodRepository.DeleteAsync(id);
return ServiceResult.Ok(); return ServiceResult.Ok();
} }
@@ -102,7 +118,10 @@ namespace Webshop.Application.Services.Admin
Name = sm.Name, Name = sm.Name,
Description = sm.Description, Description = sm.Description,
Cost = sm.BaseCost, Cost = sm.BaseCost,
IsActive = sm.IsActive IsActive = sm.IsActive,
// +++ KORREKTUR +++
MinDeliveryDays = sm.MinDeliveryDays,
MaxDeliveryDays = sm.MaxDeliveryDays
}; };
} }
} }

View File

@@ -32,4 +32,7 @@ public class ShippingMethod
[Required] [Required]
public bool RequiresTracking { get; set; } public bool RequiresTracking { get; set; }
public int MinDeliveryDays { get; set; }
public int MaxDeliveryDays { get; set; }
} }