supplier und migration

This commit is contained in:
Tizian.Breuch
2025-07-23 12:33:01 +02:00
parent c3292d6739
commit 1fe88fac3a
8 changed files with 195 additions and 3 deletions

View File

@@ -0,0 +1,44 @@
// 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;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")] // z.B. /api/v1/admin/suppliers
[Authorize(Roles = "Admin")] // Nur Admins
public class AdminSuppliersController : ControllerBase
{
private readonly AdminSupplierService _adminSupplierService;
public AdminSuppliersController(AdminSupplierService adminSupplierService)
{
_adminSupplierService = adminSupplierService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<SupplierDto>>> GetAllSuppliers()
{
var suppliers = await _adminSupplierService.GetAllSuppliersAsync();
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}")]
public async Task<ActionResult<SupplierDto>> GetSupplierById(Guid id)
{
var supplier = await _adminSupplierService.GetSupplierByIdAsync(id);
if (supplier == null) return NotFound();
return Ok(supplier);
}
}
}

View File

@@ -66,6 +66,7 @@ builder.Services.AddAuthorization(); // Aktiviert die Autorisierung
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>(); // NEU
// AUTH Services
builder.Services.AddScoped<IAuthService, AuthService>();
@@ -76,6 +77,7 @@ builder.Services.AddScoped<ProductService>(); // Ihr ProductService ist hier reg
// ADMIN Services
builder.Services.AddScoped<AdminUserService>();
builder.Services.AddScoped<AdminProductService>();
builder.Services.AddScoped<AdminSupplierService>(); // NEU
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
// builder.Services.AddScoped<CustomerOrderService>();

View File

@@ -0,0 +1,14 @@
// src/Webshop.Application/DTOs/SupplierDto.cs
namespace Webshop.Application.DTOs
{
public class SupplierDto
{
public Guid Id { get; set; } = Guid.NewGuid(); // Für PUT/GET, bei POST optional
public string Name { get; set; } = string.Empty;
public string? ContactPerson { get; set; }
public string? Email { get; set; }
public string? PhoneNumber { get; set; }
public Guid? AddressId { get; set; } // Wenn Sie Adressen haben, sonst null
public string? Notes { get; set; }
}
}

View File

@@ -0,0 +1,68 @@
// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
using Webshop.Application.DTOs; // SupplierDto
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using System.Collections.Generic; // Für IEnumerable
namespace Webshop.Application.Services.Admin
{
public class AdminSupplierService
{
private readonly ISupplierRepository _supplierRepository;
public AdminSupplierService(ISupplierRepository supplierRepository)
{
_supplierRepository = supplierRepository;
}
public async Task<IEnumerable<SupplierDto>> GetAllSuppliersAsync()
{
var suppliers = await _supplierRepository.GetAllSuppliersAsync();
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
}).ToList();
}
public async Task<SupplierDto?> GetSupplierByIdAsync(Guid id)
{
var supplier = await _supplierRepository.GetSupplierByIdAsync(id);
if (supplier == null) return null;
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)
{
var newSupplier = new Supplier
{
Id = Guid.NewGuid(), // API generiert die ID
Name = supplierDto.Name,
ContactPerson = supplierDto.ContactPerson,
Email = supplierDto.Email,
PhoneNumber = supplierDto.PhoneNumber,
AddressId = supplierDto.AddressId,
Notes = supplierDto.Notes
};
await _supplierRepository.AddSupplierAsync(newSupplier);
supplierDto.Id = newSupplier.Id; // ID zurückschreiben
return supplierDto;
}
// TODO: UpdateSupplierAsync, DeleteSupplierAsync
}
}

View File

@@ -0,0 +1,14 @@
// src/Webshop.Domain/Interfaces/ISupplierRepository.cs
using Webshop.Domain.Entities;
namespace Webshop.Domain.Interfaces
{
public interface ISupplierRepository
{
Task<IEnumerable<Supplier>> GetAllSuppliersAsync();
Task<Supplier?> GetSupplierByIdAsync(Guid id);
Task AddSupplierAsync(Supplier supplier);
Task UpdateSupplierAsync(Supplier supplier);
Task DeleteSupplierAsync(Guid id);
}
}

View File

@@ -12,8 +12,8 @@ using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250723094917_InitialCreate")]
partial class InitialCreate
[Migration("20250723102855_AddSuppliersAndOtherEntities")]
partial class AddSuppliersAndOtherEntities
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

@@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Webshop.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
public partial class AddSuppliersAndOtherEntities : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)

View File

@@ -0,0 +1,50 @@
// src/Webshop.Infrastructure/Repositories/SupplierRepository.cs
using Microsoft.EntityFrameworkCore; // Für ToListAsync
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Repositories
{
public class SupplierRepository : ISupplierRepository
{
private readonly ApplicationDbContext _context;
public SupplierRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Supplier>> GetAllSuppliersAsync()
{
return await _context.Suppliers.ToListAsync();
}
public async Task<Supplier?> GetSupplierByIdAsync(Guid id)
{
return await _context.Suppliers.FindAsync(id);
}
public async Task AddSupplierAsync(Supplier supplier)
{
_context.Suppliers.Add(supplier);
await _context.SaveChangesAsync();
}
public async Task UpdateSupplierAsync(Supplier supplier)
{
_context.Suppliers.Update(supplier);
await _context.SaveChangesAsync();
}
public async Task DeleteSupplierAsync(Guid id)
{
var supplier = await _context.Suppliers.FindAsync(id);
if (supplier != null)
{
_context.Suppliers.Remove(supplier);
await _context.SaveChangesAsync();
}
}
}
}