diff --git a/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs b/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs index d588e26..c4225fb 100644 --- a/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs +++ b/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs @@ -1,22 +1,23 @@ // src/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Webshop.Application.DTOs; // SupplierDto -using Webshop.Application.Services.Admin; // AdminSupplierService -using System.Collections.Generic; // Für IEnumerable -using System.Threading.Tasks; // Für Task +using Webshop.Application.DTOs; +using Webshop.Application.Services.Admin; // Wichtig für IAdminSupplierService +using System; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Webshop.Api.Controllers.Admin { [ApiController] - [Route("api/v1/admin/[controller]")] // z.B. /api/v1/admin/suppliers - [Authorize(Roles = "Admin")] // Nur Admins + [Route("api/v1/admin/[controller]")] + [Authorize(Roles = "Admin")] public class AdminSuppliersController : ControllerBase { - // HIER MUSS NUR EINE EINZIGE DEKLARATION STEHEN - private readonly AdminSupplierService _adminSupplierService; + // Der Controller hängt vom Interface ab, nicht von der konkreten Klasse + private readonly IAdminSupplierService _adminSupplierService; - public AdminSuppliersController(AdminSupplierService adminSupplierService) + public AdminSuppliersController(IAdminSupplierService adminSupplierService) { _adminSupplierService = adminSupplierService; } @@ -28,20 +29,43 @@ namespace Webshop.Api.Controllers.Admin return Ok(suppliers); } - [HttpPost] - public async Task> CreateSupplier([FromBody] SupplierDto supplierDto) - { - if (!ModelState.IsValid) return BadRequest(ModelState); - var createdSupplier = await _adminSupplierService.CreateSupplierAsync(supplierDto); - return CreatedAtAction(nameof(GetSupplierById), new { id = createdSupplier.Id }, createdSupplier); - } - - [HttpGet("{id}")] + [HttpGet("{id:guid}")] public async Task> GetSupplierById(Guid id) { var supplier = await _adminSupplierService.GetSupplierByIdAsync(id); if (supplier == null) return NotFound(); return Ok(supplier); } + + [HttpPost] + public async Task> CreateSupplier([FromBody] SupplierDto supplierDto) + { + if (supplierDto == null) return BadRequest("Supplier data is required."); + if (!ModelState.IsValid) return BadRequest(ModelState); + + var createdSupplier = await _adminSupplierService.CreateSupplierAsync(supplierDto); + return CreatedAtAction(nameof(GetSupplierById), new { id = createdSupplier.Id }, createdSupplier); + } + + [HttpPut("{id:guid}")] + public async Task UpdateSupplier(Guid id, [FromBody] SupplierDto supplierDto) + { + if (id != supplierDto.Id) return BadRequest("Mismatched ID in route and body."); + if (!ModelState.IsValid) return BadRequest(ModelState); + + var success = await _adminSupplierService.UpdateSupplierAsync(id, supplierDto); + if (!success) return NotFound(); + + return NoContent(); // Standardantwort für erfolgreiches Update + } + + [HttpDelete("{id:guid}")] + public async Task DeleteSupplier(Guid id) + { + var success = await _adminSupplierService.DeleteSupplierAsync(id); + if (!success) return NotFound(); + + return NoContent(); // Standardantwort für erfolgreiches Löschen + } } } \ No newline at end of file diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 215c100..a6d5e34 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -65,19 +65,20 @@ builder.Services.AddAuthentication(options => builder.Services.AddAuthorization(); // Aktiviert die Autorisierung // 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection) +// Repositories builder.Services.AddScoped(); -builder.Services.AddScoped(); // NEU +builder.Services.AddScoped(); // AUTH Services builder.Services.AddScoped(); // PUBLIC Services -builder.Services.AddScoped(); // Ihr ProductService ist hier registriert +builder.Services.AddScoped(); // ADMIN Services -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); // NEU +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); // CUSTOMER Services (später Implementierungen hinzufügen) // builder.Services.AddScoped(); diff --git a/Webshop.Application/Services/Admin/AdminSupplierService.cs b/Webshop.Application/Services/Admin/AdminSupplierService.cs index 56f25c0..532cb85 100644 --- a/Webshop.Application/Services/Admin/AdminSupplierService.cs +++ b/Webshop.Application/Services/Admin/AdminSupplierService.cs @@ -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> GetAllSuppliersAsync() { return new List(); } - public async Task GetSupplierByIdAsync(Guid id) { return null; } - public async Task CreateSupplierAsync(SupplierDto supplierDto) { return null; } + public async Task> 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 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 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 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 DeleteSupplierAsync(Guid id) + { + var supplier = await _supplierRepository.GetSupplierByIdAsync(id); + if (supplier == null) + { + return false; // Konnte nicht gefunden werden + } + + await _supplierRepository.DeleteSupplierAsync(id); + return true; + } } -} +} \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/IAdminSupplierService.cs b/Webshop.Application/Services/Admin/IAdminSupplierService.cs index e2943db..5429fd3 100644 --- a/Webshop.Application/Services/Admin/IAdminSupplierService.cs +++ b/Webshop.Application/Services/Admin/IAdminSupplierService.cs @@ -1,16 +1,21 @@ // src/Webshop.Application/Services/Admin/IAdminSupplierService.cs + +using System; // Für Guid using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Application.DTOs; // SupplierDto +using Webshop.Application.DTOs; // Für SupplierDto namespace Webshop.Application.Services.Admin { public interface IAdminSupplierService { + // Diese Methoden waren bereits vorhanden Task> GetAllSuppliersAsync(); Task GetSupplierByIdAsync(Guid id); Task CreateSupplierAsync(SupplierDto supplierDto); - // Task UpdateSupplierAsync(SupplierDto supplierDto); // Beispiel für zukünftige Methode - // Task DeleteSupplierAsync(Guid id); // Beispiel für zukünftige Methode + + // --- HIER DIE FEHLENDEN METHODEN ERGÄNZEN --- + Task UpdateSupplierAsync(Guid id, SupplierDto supplierDto); + Task DeleteSupplierAsync(Guid id); } } \ No newline at end of file