AdminShippingMethod

This commit is contained in:
Tizian.Breuch
2025-09-25 14:45:30 +02:00
parent f5543d35b4
commit 8f222ef4aa
3 changed files with 123 additions and 54 deletions

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Shipping;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
@@ -18,38 +19,31 @@ namespace Webshop.Application.Services.Admin
_shippingMethodRepository = shippingMethodRepository;
}
public async Task<IEnumerable<ShippingMethodDto>> GetAllAsync()
public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync()
{
var methods = await _shippingMethodRepository.GetAllAsync();
return methods.Select(sm => new ShippingMethodDto
{
Id = sm.Id,
Name = sm.Name,
Description = sm.Description,
Cost = sm.BaseCost,
IsActive = sm.IsActive,
MinDeliveryDays = 0, // Annahme, diese Felder existieren in Ihrer Entität
MaxDeliveryDays = 0 // Annahme, diese Felder existieren in Ihrer Entität
}).ToList();
var dtos = methods.Select(MapToDto).ToList();
return ServiceResult.Ok<IEnumerable<ShippingMethodDto>>(dtos);
}
public async Task<ShippingMethodDto?> GetByIdAsync(Guid id)
public async Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id)
{
var sm = await _shippingMethodRepository.GetByIdAsync(id);
if (sm == null) return null;
return new ShippingMethodDto
if (sm == null)
{
Id = sm.Id,
Name = sm.Name,
Description = sm.Description,
Cost = sm.BaseCost,
IsActive = sm.IsActive
};
return ServiceResult.Fail<ShippingMethodDto>(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToDto(sm));
}
public async Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto)
public async Task<ServiceResult<ShippingMethodDto>> CreateAsync(ShippingMethodDto shippingMethodDto)
{
var allMethods = await _shippingMethodRepository.GetAllAsync();
if (allMethods.Any(sm => sm.Name.Equals(shippingMethodDto.Name, StringComparison.OrdinalIgnoreCase)))
{
return ServiceResult.Fail<ShippingMethodDto>(ServiceResultType.Conflict, $"Eine Versandmethode mit dem Namen '{shippingMethodDto.Name}' existiert bereits.");
}
var newMethod = new ShippingMethod
{
Id = Guid.NewGuid(),
@@ -60,14 +54,24 @@ namespace Webshop.Application.Services.Admin
};
await _shippingMethodRepository.AddAsync(newMethod);
shippingMethodDto.Id = newMethod.Id;
return shippingMethodDto;
return ServiceResult.Ok(shippingMethodDto);
}
public async Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto)
public async Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto)
{
var existingMethod = await _shippingMethodRepository.GetByIdAsync(shippingMethodDto.Id);
if (existingMethod == null) return false;
if (existingMethod == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{shippingMethodDto.Id}' nicht gefunden.");
}
var allMethods = await _shippingMethodRepository.GetAllAsync();
if (allMethods.Any(sm => sm.Name.Equals(shippingMethodDto.Name, StringComparison.OrdinalIgnoreCase) && sm.Id != shippingMethodDto.Id))
{
return ServiceResult.Fail(ServiceResultType.Conflict, $"Eine andere Versandmethode mit dem Namen '{shippingMethodDto.Name}' existiert bereits.");
}
existingMethod.Name = shippingMethodDto.Name;
existingMethod.Description = shippingMethodDto.Description;
@@ -75,16 +79,31 @@ namespace Webshop.Application.Services.Admin
existingMethod.IsActive = shippingMethodDto.IsActive;
await _shippingMethodRepository.UpdateAsync(existingMethod);
return true;
return ServiceResult.Ok();
}
public async Task<bool> DeleteAsync(Guid id)
public async Task<ServiceResult> DeleteAsync(Guid id)
{
var method = await _shippingMethodRepository.GetByIdAsync(id);
if (method == null) return false;
if (method == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
}
await _shippingMethodRepository.DeleteAsync(id);
return true;
return ServiceResult.Ok();
}
private ShippingMethodDto MapToDto(ShippingMethod sm)
{
return new ShippingMethodDto
{
Id = sm.Id,
Name = sm.Name,
Description = sm.Description,
Cost = sm.BaseCost,
IsActive = sm.IsActive
};
}
}
}

View File

@@ -2,16 +2,17 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping; // Für ShippingMethodDto
using Webshop.Application;
using Webshop.Application.DTOs.Shipping;
namespace Webshop.Application.Services.Admin
{
public interface IAdminShippingMethodService
{
Task<IEnumerable<ShippingMethodDto>> GetAllAsync();
Task<ShippingMethodDto?> GetByIdAsync(Guid id);
Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> DeleteAsync(Guid id);
Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync();
Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id);
Task<ServiceResult<ShippingMethodDto>> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<ServiceResult> DeleteAsync(Guid id);
}
}