This commit is contained in:
Tizian.Breuch
2025-07-25 16:13:36 +02:00
parent cd48011cc3
commit f318340f98
2 changed files with 32 additions and 11 deletions

View File

@@ -1,32 +1,53 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Api/Controllers/Public/ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Webshop.Application.Services.Public;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Public.Interfaces; // <-- WICHTIGES USING HINZUF<55>GEN
namespace Webshop.Api.Controllers.Public
{
[ApiController]
[Route("api/v1/public/[controller]")]
[Route("api/v1/public/products")] // Route explizit gemacht f<>r Klarheit
[AllowAnonymous]
public class ProductsController : ControllerBase
{
private readonly ProductService _productService; // ServiceName nach Ihrer Konvention beibehalten
// --- KORREKTUR: Abh<62>ngigkeit vom Interface, nicht von der Klasse ---
private readonly IProductService _productService;
public ProductsController(ProductService productService)
public ProductsController(IProductService productService)
{
_productService = productService;
}
/// <summary>
/// Ruft eine Liste aller <20>ffentlichen, aktiven Produkte ab.
/// </summary>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ProductDto>), 200)]
public async Task<ActionResult<IEnumerable<ProductDto>>> GetAllProducts()
{
var products = await _productService.GetAllProductsAsync();
return Ok(products);
}
/// <summary>
/// Ruft ein einzelnes <20>ffentliches Produkt anhand seines URL-Slugs ab.
/// </summary>
[HttpGet("{slug}")] // Ergibt die Route z.B. /api/v1/public/products/mein-produkt-slug
[ProducesResponseType(typeof(ProductDto), 200)]
[ProducesResponseType(404)] // Not Found
public async Task<ActionResult<ProductDto>> GetProductBySlug(string slug)
{
var product = await _productService.GetProductBySlugAsync(slug);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}
}

View File

@@ -1,4 +1,4 @@
// src/Webshop.Application/Services/Public/IProductService.cs
// RICHTIG - vollständig
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Products;
@@ -8,7 +8,7 @@ namespace Webshop.Application.Services.Public.Interfaces
public interface IProductService
{
Task<IEnumerable<ProductDto>> GetAllProductsAsync();
// Task<ProductDto?> GetProductByIdAsync(Guid id); // Wenn Sie eine einzelne öffentliche Produktansicht brauchen
// Task<ProductDto> CreateProductAsync(ProductDto productDto); // Nur wenn Public Service auch Erstellung erlaubt
Task<ProductDto?> GetProductBySlugAsync(string slug);
}
}