changes
This commit is contained in:
40
README.md
Normal file
40
README.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
ShopSolution.sln
|
||||||
|
├───Webshop.Api
|
||||||
|
│ ├───Controllers
|
||||||
|
│ │ ├───Admin // Controller für Admin-Dashboard (z.B. AdminProductsController, AdminUsersController)
|
||||||
|
│ │ ├───Auth // Controller für Login/Register (AuthController)
|
||||||
|
│ │ ├───Customer // Controller für eingeloggte Kunden (z.B. OrdersController, ProfileController)
|
||||||
|
│ │ └───Public // Controller für öffentliche/nicht authentifizierte Zugriffe (z.B. ProductsController)
|
||||||
|
│ ├───appsettings.json
|
||||||
|
│ ├───Dockerfile
|
||||||
|
│ └───Program.cs
|
||||||
|
│
|
||||||
|
├───Webshop.Application
|
||||||
|
│ ├───DTOs
|
||||||
|
│ │ ├───Auth // DTOs für Authentifizierungs-Flows
|
||||||
|
│ │ ├───Users // DTOs für Benutzerdetails
|
||||||
|
│ │ ├───ProductDto.cs // Standard-Produkt-DTO (für öffentliche Ansicht)
|
||||||
|
│ │ └───AdminProductDto.cs // Produkt-DTO mit Admin-spezifischen Details (z.B. Einkaufspreis)
|
||||||
|
│ ├───Services
|
||||||
|
│ │ ├───Admin // Geschäftslogik für Admin-Dashboard
|
||||||
|
│ │ │ ├───AdminProductService.cs
|
||||||
|
│ │ │ └───AdminUserService.cs
|
||||||
|
│ │ ├───Auth // Geschäftslogik für Authentifizierung
|
||||||
|
│ │ │ ├───IAuthService.cs
|
||||||
|
│ │ │ └───AuthService.cs
|
||||||
|
│ │ ├───Customer // Geschäftslogik für eingeloggte Kunden
|
||||||
|
│ │ │ └───// CustomerOrderService.cs (Platzhalter)
|
||||||
|
│ │ │ └───// CustomerProfileService.cs (Platzhalter)
|
||||||
|
│ │ └───Public // Geschäftslogik für öffentliche/nicht authentifizierte Features
|
||||||
|
│ │ └───ProductCatalogService.cs // (ehemals ProductService.cs)
|
||||||
|
│
|
||||||
|
├───Webshop.Domain
|
||||||
|
│ ├───Entities // Ihre Kerngeschäftsobjekte (Product, Category, Customer, Order, etc.)
|
||||||
|
│ │ └───Address.cs // (Adress.cs umbenannt)
|
||||||
|
│ ├───Enums // Ihre Enumerationen
|
||||||
|
│ ├───Interfaces // Ihre Repository-Interfaces (IProductRepository, etc.)
|
||||||
|
│
|
||||||
|
└───Webshop.Infrastructure
|
||||||
|
├───Data // Ihr DbContext
|
||||||
|
├───Migrations // Von EF Core generierte Migrationsdateien
|
||||||
|
└───Repositories // Ihre Repository-Implementierungen (ProductRepository etc.)
|
||||||
62
Webshop.Api/Controllers/Admin/AdminProductsController.cs
Normal file
62
Webshop.Api/Controllers/Admin/AdminProductsController.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Webshop.Application.DTOs; // ProductDto and AdminProductDto
|
||||||
|
using Webshop.Application.Services.Admin;
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Admin
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/admin/[controller]")] // z.B. /api/v1/admin/products
|
||||||
|
[Authorize(Roles = "Admin")] // Nur Benutzer mit der Rolle "Admin" dürfen zugreifen
|
||||||
|
public class AdminProductsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly AdminProductService _adminProductService;
|
||||||
|
|
||||||
|
public AdminProductsController(AdminProductService adminProductService)
|
||||||
|
{
|
||||||
|
_adminProductService = adminProductService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet] // /api/v1/admin/products
|
||||||
|
public async Task<ActionResult<IEnumerable<AdminProductDto>>> GetAdminProducts()
|
||||||
|
{
|
||||||
|
var products = await _adminProductService.GetAllAdminProductsAsync();
|
||||||
|
return Ok(products);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")] // /api/v1/admin/products/{id}
|
||||||
|
public async Task<ActionResult<AdminProductDto>> GetAdminProduct(Guid id)
|
||||||
|
{
|
||||||
|
var product = await _adminProductService.GetAdminProductByIdAsync(id);
|
||||||
|
if (product == null) return NotFound();
|
||||||
|
return Ok(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost] // /api/v1/admin/products
|
||||||
|
public async Task<ActionResult<AdminProductDto>> CreateAdminProduct([FromBody] AdminProductDto productDto)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
var createdProduct = await _adminProductService.CreateAdminProductAsync(productDto);
|
||||||
|
return CreatedAtAction(nameof(GetAdminProduct), new { id = createdProduct.Id }, createdProduct);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")] // /api/v1/admin/products/{id}
|
||||||
|
public async Task<IActionResult> UpdateAdminProduct(Guid id, [FromBody] AdminProductDto productDto)
|
||||||
|
{
|
||||||
|
if (id != productDto.Id) return BadRequest();
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
|
var success = await _adminProductService.UpdateAdminProductAsync(productDto);
|
||||||
|
if (!success) return NotFound();
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")] // /api/v1/admin/products/{id}
|
||||||
|
public async Task<IActionResult> DeleteAdminProduct(Guid id)
|
||||||
|
{
|
||||||
|
var success = await _adminProductService.DeleteAdminProductAsync(id);
|
||||||
|
if (!success) return NotFound();
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Webshop.Api/Controllers/Admin/AdminUsersController.cs
Normal file
36
Webshop.Api/Controllers/Admin/AdminUsersController.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Webshop.Application.DTOs.Users; // UserDto
|
||||||
|
using Webshop.Application.Services.Admin;
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Admin
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/admin/[controller]")] // z.B. /api/v1/admin/users
|
||||||
|
[Authorize(Roles = "Admin")] // Nur Benutzer mit der Rolle "Admin" dürfen zugreifen
|
||||||
|
public class AdminUsersController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly AdminUserService _adminUserService;
|
||||||
|
|
||||||
|
public AdminUsersController(AdminUserService adminUserService)
|
||||||
|
{
|
||||||
|
_adminUserService = adminUserService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet] // /api/v1/admin/users
|
||||||
|
public async Task<ActionResult<IEnumerable<UserDto>>> GetAllUsers()
|
||||||
|
{
|
||||||
|
var users = await _adminUserService.GetAllUsersAsync();
|
||||||
|
return Ok(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{userId}")] // /api/v1/admin/users/{userId}
|
||||||
|
public async Task<ActionResult<UserDto>> GetUserById(string userId)
|
||||||
|
{
|
||||||
|
var user = await _adminUserService.GetUserByIdAsync(userId);
|
||||||
|
if (user == null) return NotFound();
|
||||||
|
return Ok(user);
|
||||||
|
}
|
||||||
|
// TODO: Hier könnten weitere Methoden für User-Verwaltung (Rollen ändern, löschen etc.) hinzukommen
|
||||||
|
}
|
||||||
|
}
|
||||||
49
Webshop.Api/Controllers/Auth/AuthController.cs
Normal file
49
Webshop.Api/Controllers/Auth/AuthController.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Webshop.Application.DTOs.Auth;
|
||||||
|
using Webshop.Application.Services.Auth;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Auth // Beachten Sie den Namespace
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/[controller]")] // z.B. /api/v1/auth
|
||||||
|
public class AuthController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IAuthService _authService;
|
||||||
|
|
||||||
|
public AuthController(IAuthService authService)
|
||||||
|
{
|
||||||
|
_authService = authService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("register")] // /api/v1/auth/register (für Kunden)
|
||||||
|
[AllowAnonymous] // Jeder darf sich registrieren
|
||||||
|
public async Task<IActionResult> Register([FromBody] RegisterRequestDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
var result = await _authService.RegisterUserAsync(request);
|
||||||
|
if (!result.IsAuthSuccessful) return BadRequest(new { Message = result.ErrorMessage });
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("login/customer")] // /api/v1/auth/login/customer (für Kunden-Login)
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> LoginCustomer([FromBody] LoginRequestDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
var result = await _authService.LoginUserAsync(request);
|
||||||
|
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("login/admin")] // /api/v1/auth/login/admin (für Admin-Dashboard Login)
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> LoginAdmin([FromBody] LoginRequestDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
var result = await _authService.LoginAdminAsync(request);
|
||||||
|
if (!result.IsAuthSuccessful) return Unauthorized(new { Message = result.ErrorMessage });
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Webshop.Api/Controllers/Customer/OrdersController.cs
Normal file
23
Webshop.Api/Controllers/Customer/OrdersController.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
// using Webshop.Application.DTOs.Orders; // Später erstellen
|
||||||
|
// using Webshop.Application.Services.Customer; // Später erstellen
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Customer // Beachten Sie den Namespace
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/orders
|
||||||
|
[Authorize(Roles = "Customer")] // Nur Benutzer mit der Rolle "Customer" dürfen zugreifen
|
||||||
|
public class OrdersController : ControllerBase
|
||||||
|
{
|
||||||
|
// Beispiel-Endpunkt für eingeloggte Kunden
|
||||||
|
[HttpGet("my-orders")]
|
||||||
|
public async Task<IActionResult> GetMyOrders()
|
||||||
|
{
|
||||||
|
// Hier würden Sie die Bestellungen des eingeloggten Benutzers abrufen.
|
||||||
|
// Die Benutzer-ID können Sie über User.FindFirstValue(ClaimTypes.NameIdentifier) abrufen.
|
||||||
|
// return Ok(await _orderService.GetOrdersForCurrentUserAsync(User.FindFirstValue(ClaimTypes.NameIdentifier)));
|
||||||
|
return Ok(new { Message = "Dies sind Ihre Bestellungen (Platzhalter)." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
// src/Webshop.Api/Controllers/ProductsController.cs
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Webshop.Application.DTOs;
|
|
||||||
using Webshop.Application.Services;
|
|
||||||
|
|
||||||
namespace Webshop.Api.Controllers
|
|
||||||
{
|
|
||||||
[ApiController]
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
public class ProductsController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly ProductService _productService;
|
|
||||||
|
|
||||||
public ProductsController(ProductService productService)
|
|
||||||
{
|
|
||||||
_productService = productService;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
public async Task<ActionResult<IEnumerable<ProductDto>>> GetAllProducts()
|
|
||||||
{
|
|
||||||
var products = await _productService.GetAllProductsAsync();
|
|
||||||
return Ok(products);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
public async Task<ActionResult<ProductDto>> CreateProduct([FromBody] ProductDto productDto)
|
|
||||||
{
|
|
||||||
var createdProduct = await _productService.CreateProductAsync(productDto);
|
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetAllProducts), new { id = createdProduct.Id }, createdProduct);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
29
Webshop.Api/Controllers/Public/ProductsController.cs
Normal file
29
Webshop.Api/Controllers/Public/ProductsController.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Webshop.Application.DTOs; // ProductDto
|
||||||
|
using Webshop.Application.Services.Public; // ProductCatalogService
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Public // Beachten Sie den Namespace
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/public/[controller]")] // z.B. /api/v1/public/products
|
||||||
|
[AllowAnonymous] // Jeder darf hier zugreifen (Gast oder eingeloggter User)
|
||||||
|
public class ProductsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ProductService _productCatalogService; // Umbenannt
|
||||||
|
|
||||||
|
public ProductsController(ProductService productCatalogService) // Injiziert den umbenannten Service
|
||||||
|
{
|
||||||
|
_productCatalogService = productCatalogService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet] // /api/v1/public/products
|
||||||
|
public async Task<ActionResult<IEnumerable<ProductDto>>> GetAllProducts()
|
||||||
|
{
|
||||||
|
var products = await _productCatalogService.GetAllProductsAsync(); // Ruft Service-Methode auf
|
||||||
|
return Ok(products);
|
||||||
|
}
|
||||||
|
// Keine POST, PUT, DELETE hier für öffentliche Zugriffe.
|
||||||
|
// Diese gehören in den AdminProductsController.
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,31 +3,32 @@ using Microsoft.AspNetCore.Identity;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Webshop.Application.Services;
|
using Webshop.Application.Services.Public; // Produktkatalog Service (jetzt ProductService)
|
||||||
|
using Webshop.Application.Services.Auth; // Auth Services
|
||||||
|
using Webshop.Application.Services.Admin; // Admin Services
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
using Webshop.Infrastructure.Data;
|
using Webshop.Infrastructure.Data;
|
||||||
using Webshop.Infrastructure.Repositories;
|
using Webshop.Infrastructure.Repositories;
|
||||||
|
using Microsoft.AspNetCore.HttpOverrides; // F<>r UseForwardedHeaders
|
||||||
|
using Microsoft.Extensions.Logging; // F<>r ILogger
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// --- START: DIENSTE ZUM CONTAINER HINZUF<55>GEN ---
|
// --- START: DIENSTE ZUM CONTAINER HINZUF<55>GEN ---
|
||||||
|
|
||||||
// 1. Datenbank-Kontext (DbContext) registrieren
|
// 1. Datenbank-Kontext (DbContext) registrieren
|
||||||
// Sagt der Anwendung, wie sie sich mit der PostgreSQL-Datenbank verbinden soll.
|
|
||||||
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||||||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
|
||||||
|
);
|
||||||
|
|
||||||
// 2. ASP.NET Core Identity f<>r Benutzerverwaltung registrieren
|
// 2. ASP.NET Core Identity f<>r Benutzerverwaltung registrieren
|
||||||
// Verwendet unseren DbContext, um Benutzer- und Rollen-Daten zu speichern.
|
|
||||||
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
|
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
// 3. JWT-Authentifizierung konfigurieren
|
// 3. JWT-Authentifizierung konfigurieren
|
||||||
// Richtet ein, wie die API die von Benutzern gesendeten Tokens validieren soll.
|
|
||||||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||||||
var secretKey = jwtSettings["Secret"] ?? throw new InvalidOperationException("JWT Secret not found");
|
var secretKey = jwtSettings["Secret"] ?? throw new InvalidOperationException("JWT Secret not found in configuration.");
|
||||||
|
|
||||||
builder.Services.AddAuthentication(options =>
|
builder.Services.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
@@ -47,14 +48,25 @@ builder.Services.AddAuthentication(options =>
|
|||||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
builder.Services.AddAuthorization(); // Aktiviert die Autorisierung
|
||||||
|
|
||||||
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
// 4. Unsere eigenen Interfaces und Klassen registrieren (Dependency Injection)
|
||||||
// Sagt: "Immer wenn jemand nach 'IProductRepository' fragt, gib ihm eine neue 'ProductRepository'-Instanz."
|
|
||||||
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
||||||
|
|
||||||
# region Services
|
// AUTH Services
|
||||||
builder.Services.AddScoped<ProductService>();
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||||
# endregion
|
|
||||||
|
// PUBLIC Services
|
||||||
|
builder.Services.AddScoped<ProductService>(); // Ihr ProductService ist hier registriert
|
||||||
|
|
||||||
|
// ADMIN Services
|
||||||
|
builder.Services.AddScoped<AdminUserService>();
|
||||||
|
builder.Services.AddScoped<AdminProductService>();
|
||||||
|
|
||||||
|
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
|
||||||
|
// builder.Services.AddScoped<CustomerOrderService>();
|
||||||
|
|
||||||
|
|
||||||
// 5. Controller und Swagger/OpenAPI hinzuf<75>gen
|
// 5. Controller und Swagger/OpenAPI hinzuf<75>gen
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
@@ -64,13 +76,14 @@ builder.Services.AddSwaggerGen();
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Optional: Automatisches Anwenden von Migrationen beim Start (nur f<>r Entwicklung/Tests)
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var services = scope.ServiceProvider;
|
var services = scope.ServiceProvider;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var context = services.GetRequiredService<ApplicationDbContext>();
|
var context = services.GetRequiredService<ApplicationDbContext>();
|
||||||
context.Database.Migrate();
|
context.Database.Migrate(); // Wendet ausstehende Migrationen an
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -83,21 +96,21 @@ using (var scope = app.Services.CreateScope())
|
|||||||
|
|
||||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||||
{
|
{
|
||||||
ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Swagger immer aktivieren (auch in Produktion f<>r API-Dokumentation)
|
||||||
|
// F<>r die Produktion w<>re es sicherer, dies an `app.Environment.IsDevelopment()` zu binden
|
||||||
// if (app.Environment.IsDevelopment())
|
// if (app.Environment.IsDevelopment())
|
||||||
// {
|
// {
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI();
|
app.UseSwaggerUI();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//app.UseHttpsRedirection();
|
// app.UseHttpsRedirection(); // Auskommentiert f<>r Docker HTTP-Entwicklung
|
||||||
|
|
||||||
// WICHTIG: Die Reihenfolge ist entscheidend!
|
// WICHTIG: Die Reihenfolge ist entscheidend!
|
||||||
// Zuerst pr<70>fen, wer der Benutzer ist (Authentifizierung)...
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
// ...dann pr<70>fen, was der Benutzer darf (Autorisierung).
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
@@ -105,3 +118,69 @@ app.MapControllers();
|
|||||||
// --- ENDE: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
// --- ENDE: HTTP REQUEST PIPELINE KONFIGURIEREN ---
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
// --- TEMPOR<4F>RER INITIALER ADMIN- UND KUNDEN-SETUP (NUR F<>R ERSTE ENTWICKLUNG!) ---
|
||||||
|
// Dieser Block erstellt Rollen und initiale Benutzer, falls sie noch nicht existieren.
|
||||||
|
// Entfernen oder kommentiere dies aus, NACHDEM du deine ersten Benutzer erstellt hast!
|
||||||
|
using (var scope = app.Services.CreateScope()) // Eigener Scope, da app.Run() blockierend ist
|
||||||
|
{
|
||||||
|
var serviceProvider = scope.ServiceProvider;
|
||||||
|
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||||
|
var userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
|
||||||
|
|
||||||
|
string[] roleNames = { "Admin", "Customer" };
|
||||||
|
|
||||||
|
foreach (var roleName in roleNames)
|
||||||
|
{
|
||||||
|
var roleExist = await roleManager.RoleExistsAsync(roleName);
|
||||||
|
if (!roleExist)
|
||||||
|
{
|
||||||
|
await roleManager.CreateAsync(new IdentityRole(roleName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Erstelle einen initialen Admin-Benutzer
|
||||||
|
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com"); // << ANPASSEN >>
|
||||||
|
if (adminUser == null)
|
||||||
|
{
|
||||||
|
adminUser = new IdentityUser
|
||||||
|
{
|
||||||
|
UserName = "admin@yourwebshop.com", // << ANPASSEN >>
|
||||||
|
Email = "admin@yourwebshop.com", // << ANPASSEN >>
|
||||||
|
EmailConfirmed = true
|
||||||
|
};
|
||||||
|
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!"); // << ANPASSEN >>
|
||||||
|
if (createAdmin.Succeeded)
|
||||||
|
{
|
||||||
|
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||||
|
Console.WriteLine("Admin user created.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error creating admin user: {string.Join(", ", createAdmin.Errors.Select(e => e.Description))}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Erstelle einen initialen Kunden-Benutzer
|
||||||
|
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com"); // << ANPASSEN >>
|
||||||
|
if (customerUser == null)
|
||||||
|
{
|
||||||
|
customerUser = new IdentityUser
|
||||||
|
{
|
||||||
|
UserName = "customer@yourwebshop.com", // << ANPASSEN >>
|
||||||
|
Email = "customer@yourwebshop.com", // << ANPASSEN >>
|
||||||
|
EmailConfirmed = true
|
||||||
|
};
|
||||||
|
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!"); // << ANPASSEN >>
|
||||||
|
if (createCustomer.Succeeded)
|
||||||
|
{
|
||||||
|
await userManager.AddToRoleAsync(customerUser, "Customer");
|
||||||
|
Console.WriteLine("Customer user created.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error creating customer user: {string.Join(", ", createCustomer.Errors.Select(e => e.Description))}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --- ENDE DES TEMPOR<4F>REN SETUP-BLOCKS ---
|
||||||
22
Webshop.Application/DTOs/AdminProductDto.cs
Normal file
22
Webshop.Application/DTOs/AdminProductDto.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
namespace Webshop.Application.DTOs
|
||||||
|
{
|
||||||
|
public class AdminProductDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
public string SKU { get; set; } = string.Empty;
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
public decimal? OldPrice { get; set; }
|
||||||
|
public bool IsActive { get; set; } = true;
|
||||||
|
public bool IsInStock { get; set; } = true;
|
||||||
|
public int StockQuantity { get; set; }
|
||||||
|
public decimal? Weight { get; set; }
|
||||||
|
public string? ImageUrl { get; set; }
|
||||||
|
public string Slug { get; set; } = string.Empty;
|
||||||
|
public DateTimeOffset CreatedDate { get; set; }
|
||||||
|
public DateTimeOffset? LastModifiedDate { get; set; }
|
||||||
|
public Guid? SupplierId { get; set; }
|
||||||
|
public decimal? PurchasePrice { get; set; } // Admin-spezifisches Feld
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Webshop.Application/DTOs/Auth/AuthResponseDto.cs
Normal file
12
Webshop.Application/DTOs/Auth/AuthResponseDto.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Webshop.Application.DTOs.Auth
|
||||||
|
{
|
||||||
|
public class AuthResponseDto
|
||||||
|
{
|
||||||
|
public bool IsAuthSuccessful { get; set; }
|
||||||
|
public string ErrorMessage { get; set; } = string.Empty;
|
||||||
|
public string Token { get; set; } = string.Empty;
|
||||||
|
public string UserId { get; set; } = string.Empty;
|
||||||
|
public string Email { get; set; } = string.Empty;
|
||||||
|
public List<string> Roles { get; set; } = new List<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Webshop.Application/DTOs/Auth/LoginRequestDto.cs
Normal file
14
Webshop.Application/DTOs/Auth/LoginRequestDto.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Webshop.Application.DTOs.Auth
|
||||||
|
{
|
||||||
|
public class LoginRequestDto
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "E-Mail ist erforderlich.")]
|
||||||
|
[EmailAddress(ErrorMessage = "Ungültiges E-Mail-Format.")]
|
||||||
|
public string Email { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "Passwort ist erforderlich.")]
|
||||||
|
public string Password { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
Webshop.Application/DTOs/Auth/RegisterRequestDto.cs
Normal file
22
Webshop.Application/DTOs/Auth/RegisterRequestDto.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Webshop.Application.DTOs.Auth
|
||||||
|
{
|
||||||
|
public class RegisterRequestDto
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "E-Mail ist erforderlich.")]
|
||||||
|
[EmailAddress(ErrorMessage = "Ungültiges E-Mail-Format.")]
|
||||||
|
public string Email { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "Passwort ist erforderlich.")]
|
||||||
|
[MinLength(6, ErrorMessage = "Passwort muss mindestens 6 Zeichen lang sein.")]
|
||||||
|
public string Password { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "Passwortbestätigung ist erforderlich.")]
|
||||||
|
[Compare("Password", ErrorMessage = "Passwörter stimmen nicht überein.")]
|
||||||
|
public string ConfirmPassword { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string? FirstName { get; set; }
|
||||||
|
public string? LastName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Webshop.Application/DTOs/Users/UserDto.cs
Normal file
12
Webshop.Application/DTOs/Users/UserDto.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Webshop.Application.DTOs.Users
|
||||||
|
{
|
||||||
|
public class UserDto
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
public string Email { get; set; } = string.Empty;
|
||||||
|
public string UserName { get; set; } = string.Empty;
|
||||||
|
public List<string> Roles { get; set; } = new List<string>();
|
||||||
|
public DateTimeOffset CreatedDate { get; set; } // Placeholder, as IdentityUser doesn't have it directly
|
||||||
|
public bool EmailConfirmed { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ using Webshop.Application.DTOs;
|
|||||||
using Webshop.Domain.Entities;
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
namespace Webshop.Application.Services
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
public class ProductService
|
public class ProductService
|
||||||
{
|
{
|
||||||
@@ -14,4 +14,10 @@
|
|||||||
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Services\Auth\" />
|
||||||
|
<Folder Include="Services\Customer\" />
|
||||||
|
<Folder Include="Services\Admin\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user