This commit is contained in:
Tizian.Breuch
2025-09-25 16:35:20 +02:00
parent 88f520a51b
commit d3c03ef431
4 changed files with 60 additions and 102 deletions

View File

@@ -4,16 +4,17 @@ using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Public.Interfaces; // <-- WICHTIGES USING HINZUF<55>GEN
using Webshop.Application.Services.Public.Interfaces;
using Microsoft.AspNetCore.Http;
using Webshop.Application;
namespace Webshop.Api.Controllers.Public
{
[ApiController]
[Route("api/v1/public/[controller]")] // Route explizit gemacht f<>r Klarheit
[Route("api/v1/public/[controller]")]
[AllowAnonymous]
public class ProductsController : ControllerBase
{
// --- KORREKTUR: Abh<62>ngigkeit vom Interface, nicht von der Klasse ---
private readonly IProductService _productService;
public ProductsController(IProductService productService)
@@ -21,43 +22,35 @@ namespace Webshop.Api.Controllers.Public
_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()
[ProducesResponseType(typeof(IEnumerable<ProductDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllProducts()
{
var products = await _productService.GetAllProductsAsync();
return Ok(products);
var result = await _productService.GetAllProductsAsync();
return Ok(result.Value);
}
/// <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)
[HttpGet("{slug}")]
[ProducesResponseType(typeof(ProductDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetProductBySlug(string slug)
{
var product = await _productService.GetProductBySlugAsync(slug);
var result = await _productService.GetProductBySlugAsync(slug);
if (product == null)
return result.Type switch
{
return NotFound();
}
return Ok(product);
ServiceResultType.Success => Ok(result.Value),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
};
}
/// <summary>
/// Ruft eine Liste der Sonderangebote f<>r die Startseite ab, sortiert nach Anzeigereihenfolge.
/// </summary>
[HttpGet("featured")]
[ProducesResponseType(typeof(IEnumerable<ProductDto>), 200)]
public async Task<ActionResult<IEnumerable<ProductDto>>> GetFeaturedProducts()
[ProducesResponseType(typeof(IEnumerable<ProductDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetFeaturedProducts()
{
var products = await _productService.GetFeaturedProductsAsync();
return Ok(products);
var result = await _productService.GetFeaturedProductsAsync();
return Ok(result.Value);
}
}
}