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.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<ServiceResult<IEnumerable<ShippingMethodDto>>> 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<ServiceResult> 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
};
}
}