supplier und migration
This commit is contained in:
44
Webshop.Api/Controllers/Admin/HomeController.cs
Normal file
44
Webshop.Api/Controllers/Admin/HomeController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>();
|
||||
|
||||
14
Webshop.Application/DTOs/SupplierDto.cs
Normal file
14
Webshop.Application/DTOs/SupplierDto.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
68
Webshop.Application/Services/Admin/AdminSupplierService.cs
Normal file
68
Webshop.Application/Services/Admin/AdminSupplierService.cs
Normal 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
|
||||
}
|
||||
}
|
||||
14
Webshop.Domain/Interfaces/ISupplierRepository.cs
Normal file
14
Webshop.Domain/Interfaces/ISupplierRepository.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
50
Webshop.Infrastructure/Repositories/SupplierRepository.cs
Normal file
50
Webshop.Infrastructure/Repositories/SupplierRepository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user