Files
2025-10-27 21:39:50 -04:00

44 lines
1.4 KiB
C#

using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Prefab.Catalog.Domain.Exceptions;
using Prefab.Endpoints;
using Prefab.Shared;
using Prefab.Shared.Catalog.Products;
namespace Prefab.Catalog.Api.Controllers;
[ApiController]
[Route("catalog/products")]
public sealed class ProductsController : ControllerBase
{
[HttpGet("{slug}/config")]
[ProducesResponseType(typeof(GetProductDetail.Response), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetProductConfig(
[FromRoute] string slug,
[FromServices] IProductClient productClient,
CancellationToken cancellationToken)
{
try
{
var response = await productClient.GetProductDetail(slug, cancellationToken);
return Ok(response);
}
catch (CatalogNotFoundException ex)
{
return NotFound(ApiProblemDetails.NotFound(ex.Resource, ex.Identifier, ex.Message));
}
catch (RemoteProblemException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return NotFound();
}
catch (ArgumentException argumentException)
{
return ValidationProblem(
title: "Invalid product slug.",
detail: argumentException.Message);
}
}
}