110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Interfaces; // Wichtig für ISupplierRepository
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application.DTOs.Suppliers;
|
|
using Webshop.Application.Services.Admin.Interfaces;
|
|
|
|
namespace Webshop.Application.Services.Admin
|
|
{
|
|
// Implementiert das Interface, das der Controller erwartet
|
|
public class AdminSupplierService : IAdminSupplierService
|
|
{
|
|
private readonly ISupplierRepository _supplierRepository;
|
|
|
|
// Lässt sich das Repository per Dependency Injection geben
|
|
public AdminSupplierService(ISupplierRepository supplierRepository)
|
|
{
|
|
_supplierRepository = supplierRepository;
|
|
}
|
|
|
|
public async Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync()
|
|
{
|
|
var suppliers = await _supplierRepository.GetAllSuppliersAsync();
|
|
// Mappt die Entity-Liste zu einer DTO-Liste für die API
|
|
return suppliers.Select(s => new SupplierDto
|
|
{
|
|
Id = s.Id,
|
|
Name = s.Name,
|
|
ContactPerson = s.ContactPerson,
|
|
Email = s.Email,
|
|
PhoneNumber = s.PhoneNumber,
|
|
AddressId = s.AddressId,
|
|
Notes = s.Notes
|
|
});
|
|
}
|
|
|
|
public async Task<SupplierDto?> GetSupplierByIdAsync(Guid id)
|
|
{
|
|
var supplier = await _supplierRepository.GetSupplierByIdAsync(id);
|
|
if (supplier == null) return null;
|
|
|
|
// Mappt die gefundene Entity zu einem DTO
|
|
return new SupplierDto
|
|
{
|
|
Id = supplier.Id,
|
|
Name = supplier.Name,
|
|
ContactPerson = supplier.ContactPerson,
|
|
Email = supplier.Email,
|
|
PhoneNumber = supplier.PhoneNumber,
|
|
AddressId = supplier.AddressId,
|
|
Notes = supplier.Notes
|
|
};
|
|
}
|
|
|
|
public async Task<SupplierDto> CreateSupplierAsync(SupplierDto supplierDto)
|
|
{
|
|
// Wandelt das DTO von der API in eine Datenbank-Entity um
|
|
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);
|
|
|
|
// Gibt ein DTO mit der neu generierten ID zurück
|
|
supplierDto.Id = supplier.Id;
|
|
return supplierDto;
|
|
}
|
|
|
|
public async Task<bool> UpdateSupplierAsync(Guid id, SupplierDto supplierDto)
|
|
{
|
|
var existingSupplier = await _supplierRepository.GetSupplierByIdAsync(id);
|
|
if (existingSupplier == null)
|
|
{
|
|
return false; // Konnte nicht gefunden werden
|
|
}
|
|
|
|
// Aktualisiert die Daten der gefundenen Entity
|
|
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 true;
|
|
}
|
|
|
|
public async Task<bool> DeleteSupplierAsync(Guid id)
|
|
{
|
|
var supplier = await _supplierRepository.GetSupplierByIdAsync(id);
|
|
if (supplier == null)
|
|
{
|
|
return false; // Konnte nicht gefunden werden
|
|
}
|
|
|
|
await _supplierRepository.DeleteSupplierAsync(id);
|
|
return true;
|
|
}
|
|
}
|
|
} |