Files
ShopSolution-backend/Webshop.Application/Services/Admin/AdminSupplierService.cs
Tizian.Breuch 6b0fe1a343 adminsupplier
2025-09-25 14:49:20 +02:00

121 lines
4.8 KiB
C#

// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Suppliers;
using Webshop.Application.Services.Admin.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminSupplierService : IAdminSupplierService
{
private readonly ISupplierRepository _supplierRepository;
public AdminSupplierService(ISupplierRepository supplierRepository)
{
_supplierRepository = supplierRepository;
}
public async Task<ServiceResult<IEnumerable<SupplierDto>>> GetAllSuppliersAsync()
{
var suppliers = await _supplierRepository.GetAllSuppliersAsync();
var dtos = suppliers.Select(MapToDto);
return ServiceResult.Ok<IEnumerable<SupplierDto>>(dtos);
}
public async Task<ServiceResult<SupplierDto>> GetSupplierByIdAsync(Guid id)
{
var supplier = await _supplierRepository.GetSupplierByIdAsync(id);
if (supplier == null)
{
return ServiceResult.Fail<SupplierDto>(ServiceResultType.NotFound, $"Lieferant mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToDto(supplier));
}
public async Task<ServiceResult<SupplierDto>> CreateSupplierAsync(SupplierDto supplierDto)
{
var allSuppliers = await _supplierRepository.GetAllSuppliersAsync();
if (allSuppliers.Any(s => s.Name.Equals(supplierDto.Name, StringComparison.OrdinalIgnoreCase)))
{
return ServiceResult.Fail<SupplierDto>(ServiceResultType.Conflict, $"Ein Lieferant mit dem Namen '{supplierDto.Name}' existiert bereits.");
}
var supplier = new Supplier
{
Name = supplierDto.Name,
ContactPerson = supplierDto.ContactPerson,
Email = supplierDto.Email,
PhoneNumber = supplierDto.PhoneNumber,
AddressId = supplierDto.AddressId,
Notes = supplierDto.Notes
};
await _supplierRepository.AddSupplierAsync(supplier);
supplierDto.Id = supplier.Id;
return ServiceResult.Ok(supplierDto);
}
public async Task<ServiceResult> UpdateSupplierAsync(SupplierDto supplierDto)
{
var existingSupplier = await _supplierRepository.GetSupplierByIdAsync(supplierDto.Id);
if (existingSupplier == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Lieferant mit ID '{supplierDto.Id}' nicht gefunden.");
}
var allSuppliers = await _supplierRepository.GetAllSuppliersAsync();
if (allSuppliers.Any(s => s.Name.Equals(supplierDto.Name, StringComparison.OrdinalIgnoreCase) && s.Id != supplierDto.Id))
{
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderer Lieferant mit dem Namen '{supplierDto.Name}' existiert bereits.");
}
existingSupplier.Name = supplierDto.Name;
existingSupplier.ContactPerson = supplierDto.ContactPerson;
existingSupplier.Email = supplierDto.Email;
existingSupplier.PhoneNumber = supplierDto.PhoneNumber;
existingSupplier.AddressId = supplierDto.AddressId;
existingSupplier.Notes = supplierDto.Notes;
await _supplierRepository.UpdateSupplierAsync(existingSupplier);
return ServiceResult.Ok();
}
public async Task<ServiceResult> DeleteSupplierAsync(Guid id)
{
var supplier = await _supplierRepository.GetSupplierByIdAsync(id);
if (supplier == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Lieferant mit ID '{id}' nicht gefunden.");
}
// Hier könnte man noch prüfen, ob der Lieferant Produkten zugeordnet ist (Conflict)
// if (await _productRepository.AnyAsync(p => p.SupplierId == id))
// {
// return ServiceResult.Fail(ServiceResultType.Conflict, "Lieferant kann nicht gelöscht werden, da ihm Produkte zugeordnet sind.");
// }
await _supplierRepository.DeleteSupplierAsync(id);
return ServiceResult.Ok();
}
private SupplierDto MapToDto(Supplier s)
{
return new SupplierDto
{
Id = s.Id,
Name = s.Name,
ContactPerson = s.ContactPerson,
Email = s.Email,
PhoneNumber = s.PhoneNumber,
AddressId = s.AddressId,
Notes = s.Notes
};
}
}
}