109 lines
4.3 KiB
C#
109 lines
4.3 KiB
C#
// src/Webshop.Application/Services/Admin/AdminShippingMethodService.cs
|
|
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;
|
|
|
|
namespace Webshop.Application.Services.Admin
|
|
{
|
|
public class AdminShippingMethodService : IAdminShippingMethodService
|
|
{
|
|
private readonly IShippingMethodRepository _shippingMethodRepository;
|
|
|
|
public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository)
|
|
{
|
|
_shippingMethodRepository = shippingMethodRepository;
|
|
}
|
|
|
|
public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync()
|
|
{
|
|
var methods = await _shippingMethodRepository.GetAllAsync();
|
|
var dtos = methods.Select(MapToDto).ToList();
|
|
return ServiceResult.Ok<IEnumerable<ShippingMethodDto>>(dtos);
|
|
}
|
|
|
|
public async Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id)
|
|
{
|
|
var sm = await _shippingMethodRepository.GetByIdAsync(id);
|
|
if (sm == null)
|
|
{
|
|
return ServiceResult.Fail<ShippingMethodDto>(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
|
|
}
|
|
return ServiceResult.Ok(MapToDto(sm));
|
|
}
|
|
|
|
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(),
|
|
Name = shippingMethodDto.Name,
|
|
Description = shippingMethodDto.Description,
|
|
BaseCost = shippingMethodDto.Cost,
|
|
IsActive = shippingMethodDto.IsActive
|
|
};
|
|
|
|
await _shippingMethodRepository.AddAsync(newMethod);
|
|
|
|
shippingMethodDto.Id = newMethod.Id;
|
|
return ServiceResult.Ok(shippingMethodDto);
|
|
}
|
|
|
|
public async Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto)
|
|
{
|
|
var existingMethod = await _shippingMethodRepository.GetByIdAsync(shippingMethodDto.Id);
|
|
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;
|
|
existingMethod.BaseCost = shippingMethodDto.Cost;
|
|
existingMethod.IsActive = shippingMethodDto.IsActive;
|
|
|
|
await _shippingMethodRepository.UpdateAsync(existingMethod);
|
|
return ServiceResult.Ok();
|
|
}
|
|
|
|
public async Task<ServiceResult> DeleteAsync(Guid id)
|
|
{
|
|
var method = await _shippingMethodRepository.GetByIdAsync(id);
|
|
if (method == null)
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
|
|
}
|
|
|
|
await _shippingMethodRepository.DeleteAsync(id);
|
|
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
|
|
};
|
|
}
|
|
}
|
|
} |