supplier
This commit is contained in:
@@ -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<ActionResult<SupplierDto>> 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<ActionResult<SupplierDto>> GetSupplierById(Guid id)
|
||||
{
|
||||
var supplier = await _adminSupplierService.GetSupplierByIdAsync(id);
|
||||
if (supplier == null) return NotFound();
|
||||
return Ok(supplier);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<SupplierDto>> 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<IActionResult> 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<IActionResult> DeleteSupplier(Guid id)
|
||||
{
|
||||
var success = await _adminSupplierService.DeleteSupplierAsync(id);
|
||||
if (!success) return NotFound();
|
||||
|
||||
return NoContent(); // Standardantwort f<>r erfolgreiches L<>schen
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<IProductRepository, ProductRepository>();
|
||||
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>(); // NEU
|
||||
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
|
||||
|
||||
// AUTH Services
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
|
||||
// PUBLIC Services
|
||||
builder.Services.AddScoped<ProductService>(); // Ihr ProductService ist hier registriert
|
||||
builder.Services.AddScoped<IProductService, ProductService>();
|
||||
|
||||
// ADMIN Services
|
||||
builder.Services.AddScoped<AdminUserService>();
|
||||
builder.Services.AddScoped<AdminProductService>();
|
||||
builder.Services.AddScoped<AdminSupplierService>(); // NEU
|
||||
builder.Services.AddScoped<IAdminUserService, AdminUserService>();
|
||||
builder.Services.AddScoped<IAdminProductService, AdminProductService>();
|
||||
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
|
||||
|
||||
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
|
||||
// builder.Services.AddScoped<CustomerOrderService>();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<IEnumerable<SupplierDto>> GetAllSuppliersAsync();
|
||||
Task<SupplierDto?> GetSupplierByIdAsync(Guid id);
|
||||
Task<SupplierDto> CreateSupplierAsync(SupplierDto supplierDto);
|
||||
// Task<bool> UpdateSupplierAsync(SupplierDto supplierDto); // Beispiel für zukünftige Methode
|
||||
// Task<bool> DeleteSupplierAsync(Guid id); // Beispiel für zukünftige Methode
|
||||
|
||||
// --- HIER DIE FEHLENDEN METHODEN ERGÄNZEN ---
|
||||
Task<bool> UpdateSupplierAsync(Guid id, SupplierDto supplierDto);
|
||||
Task<bool> DeleteSupplierAsync(Guid id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user