supplier
This commit is contained in:
@@ -1,27 +1,109 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
|
||||
using Webshop.Domain.Interfaces; // Wichtig für ISupplierRepository
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// HIER KOMMT DER VORHERIGE ADMINSUPPLIERSERVICE CODE HIN (GetAllSuppliersAsync, CreateSupplierAsync etc.)
|
||||
// Hier sind Platzhalter-Implementierungen, die Sie durch den vollständigen Code ersetzen müssen:
|
||||
public async Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync() { return new List<SupplierDto>(); }
|
||||
public async Task<SupplierDto?> GetSupplierByIdAsync(Guid id) { return null; }
|
||||
public async Task<SupplierDto> CreateSupplierAsync(SupplierDto supplierDto) { return null; }
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user