supplier
This commit is contained in:
@@ -1,22 +1,23 @@
|
|||||||
// src/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs
|
// src/Webshop.Api/Controllers/Admin/AdminSuppliersController.cs
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Webshop.Application.DTOs; // SupplierDto
|
using Webshop.Application.DTOs;
|
||||||
using Webshop.Application.Services.Admin; // AdminSupplierService
|
using Webshop.Application.Services.Admin; // Wichtig f<>r IAdminSupplierService
|
||||||
using System.Collections.Generic; // F<>r IEnumerable
|
using System;
|
||||||
using System.Threading.Tasks; // F<>r Task
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Webshop.Api.Controllers.Admin
|
namespace Webshop.Api.Controllers.Admin
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/v1/admin/[controller]")] // z.B. /api/v1/admin/suppliers
|
[Route("api/v1/admin/[controller]")]
|
||||||
[Authorize(Roles = "Admin")] // Nur Admins
|
[Authorize(Roles = "Admin")]
|
||||||
public class AdminSuppliersController : ControllerBase
|
public class AdminSuppliersController : ControllerBase
|
||||||
{
|
{
|
||||||
// HIER MUSS NUR EINE EINZIGE DEKLARATION STEHEN
|
// Der Controller h<>ngt vom Interface ab, nicht von der konkreten Klasse
|
||||||
private readonly AdminSupplierService _adminSupplierService;
|
private readonly IAdminSupplierService _adminSupplierService;
|
||||||
|
|
||||||
public AdminSuppliersController(AdminSupplierService adminSupplierService)
|
public AdminSuppliersController(IAdminSupplierService adminSupplierService)
|
||||||
{
|
{
|
||||||
_adminSupplierService = adminSupplierService;
|
_adminSupplierService = adminSupplierService;
|
||||||
}
|
}
|
||||||
@@ -28,20 +29,43 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
return Ok(suppliers);
|
return Ok(suppliers);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpGet("{id:guid}")]
|
||||||
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}")]
|
|
||||||
public async Task<ActionResult<SupplierDto>> GetSupplierById(Guid id)
|
public async Task<ActionResult<SupplierDto>> GetSupplierById(Guid id)
|
||||||
{
|
{
|
||||||
var supplier = await _adminSupplierService.GetSupplierByIdAsync(id);
|
var supplier = await _adminSupplierService.GetSupplierByIdAsync(id);
|
||||||
if (supplier == null) return NotFound();
|
if (supplier == null) return NotFound();
|
||||||
return Ok(supplier);
|
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
|
builder.Services.AddAuthorization(); // Aktiviert die Autorisierung
|
||||||
|
|
||||||
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
||||||
|
// Repositories
|
||||||
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
||||||
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>(); // NEU
|
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
|
||||||
|
|
||||||
// AUTH Services
|
// AUTH Services
|
||||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||||
|
|
||||||
// PUBLIC Services
|
// PUBLIC Services
|
||||||
builder.Services.AddScoped<ProductService>(); // Ihr ProductService ist hier registriert
|
builder.Services.AddScoped<IProductService, ProductService>();
|
||||||
|
|
||||||
// ADMIN Services
|
// ADMIN Services
|
||||||
builder.Services.AddScoped<AdminUserService>();
|
builder.Services.AddScoped<IAdminUserService, AdminUserService>();
|
||||||
builder.Services.AddScoped<AdminProductService>();
|
builder.Services.AddScoped<IAdminProductService, AdminProductService>();
|
||||||
builder.Services.AddScoped<AdminSupplierService>(); // NEU
|
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
|
||||||
|
|
||||||
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
|
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
|
||||||
// builder.Services.AddScoped<CustomerOrderService>();
|
// builder.Services.AddScoped<CustomerOrderService>();
|
||||||
|
|||||||
@@ -1,27 +1,109 @@
|
|||||||
// Auto-generiert von CreateWebshopFiles.ps1
|
// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Webshop.Application.DTOs;
|
using Webshop.Application.DTOs;
|
||||||
using Webshop.Domain.Entities;
|
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
|
namespace Webshop.Application.Services.Admin
|
||||||
{
|
{
|
||||||
|
// Implementiert das Interface, das der Controller erwartet
|
||||||
public class AdminSupplierService : IAdminSupplierService
|
public class AdminSupplierService : IAdminSupplierService
|
||||||
{
|
{
|
||||||
private readonly ISupplierRepository _supplierRepository;
|
private readonly ISupplierRepository _supplierRepository;
|
||||||
|
|
||||||
|
// Lässt sich das Repository per Dependency Injection geben
|
||||||
public AdminSupplierService(ISupplierRepository supplierRepository)
|
public AdminSupplierService(ISupplierRepository supplierRepository)
|
||||||
{
|
{
|
||||||
_supplierRepository = supplierRepository;
|
_supplierRepository = supplierRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HIER KOMMT DER VORHERIGE ADMINSUPPLIERSERVICE CODE HIN (GetAllSuppliersAsync, CreateSupplierAsync etc.)
|
public async Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync()
|
||||||
// Hier sind Platzhalter-Implementierungen, die Sie durch den vollständigen Code ersetzen müssen:
|
{
|
||||||
public async Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync() { return new List<SupplierDto>(); }
|
var suppliers = await _supplierRepository.GetAllSuppliersAsync();
|
||||||
public async Task<SupplierDto?> GetSupplierByIdAsync(Guid id) { return null; }
|
// Mappt die Entity-Liste zu einer DTO-Liste für die API
|
||||||
public async Task<SupplierDto> CreateSupplierAsync(SupplierDto supplierDto) { return null; }
|
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
|
// src/Webshop.Application/Services/Admin/IAdminSupplierService.cs
|
||||||
|
|
||||||
|
using System; // Für Guid
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Webshop.Application.DTOs; // SupplierDto
|
using Webshop.Application.DTOs; // Für SupplierDto
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin
|
namespace Webshop.Application.Services.Admin
|
||||||
{
|
{
|
||||||
public interface IAdminSupplierService
|
public interface IAdminSupplierService
|
||||||
{
|
{
|
||||||
|
// Diese Methoden waren bereits vorhanden
|
||||||
Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync();
|
Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync();
|
||||||
Task<SupplierDto?> GetSupplierByIdAsync(Guid id);
|
Task<SupplierDto?> GetSupplierByIdAsync(Guid id);
|
||||||
Task<SupplierDto> CreateSupplierAsync(SupplierDto supplierDto);
|
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