Aanbod: categorieën als CMS-data met CRUD-routes en categoryId op het aanbod
Nieuwe tabel OfferingsCategories (unieke naam, case-insensitief) en nullable Offering.CategoryId met FK Restrict (migratie AddOfferingCategories). Routes onder /offerings/categories zijn AdminOnly; verwijderen van een gebruikte categorie geeft 409. Het aanbod neemt categoryId (moet bestaan, anders 400) en geeft category (naam) en categoryId terug; niets wordt geseed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Categories offerings can be placed in. Managed by administrators only; visitors see the
|
||||
/// category name on the offerings themselves.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("offerings/categories")]
|
||||
[Authorize(Policy = "AdminOnly")]
|
||||
public class OfferingCategoriesController(IOfferingCategoriesService service) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> List() => Ok(await service.ListAsync());
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] OfferingCategoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dto = await service.CreateAsync(request.Name);
|
||||
return Created($"/api/v1/offerings/categories/{dto.Id}", dto);
|
||||
}
|
||||
catch (OfferingCategoryNameConflictException ex)
|
||||
{
|
||||
return Conflict(Problem409(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
public async Task<IActionResult> Rename(Guid id, [FromBody] OfferingCategoryRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dto = await service.RenameAsync(id, request.Name);
|
||||
return dto is null ? NotFound() : Ok(dto);
|
||||
}
|
||||
catch (OfferingCategoryNameConflictException ex)
|
||||
{
|
||||
return Conflict(Problem409(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await service.DeleteAsync(id) ? NoContent() : NotFound();
|
||||
}
|
||||
catch (OfferingCategoryInUseException ex)
|
||||
{
|
||||
return Conflict(Problem409(ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
private static ProblemDetails Problem409(string title)
|
||||
=> new() { Status = StatusCodes.Status409Conflict, Title = title };
|
||||
}
|
||||
@@ -35,6 +35,10 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
|
||||
var dto = await service.CreateAsync(request, GetCallerId());
|
||||
return Created($"/api/v1/offerings/{dto.Id}", dto);
|
||||
}
|
||||
catch (OfferingCategoryInvalidException)
|
||||
{
|
||||
return InvalidCategory();
|
||||
}
|
||||
catch (OfferingSlugConflictException ex)
|
||||
{
|
||||
return Conflict(new ProblemDetails { Status = StatusCodes.Status409Conflict, Title = ex.Message });
|
||||
@@ -49,6 +53,10 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
|
||||
var dto = await service.UpdateAsync(id, request, GetCallerId());
|
||||
return dto is null ? NotFound() : Ok(dto);
|
||||
}
|
||||
catch (OfferingCategoryInvalidException)
|
||||
{
|
||||
return InvalidCategory();
|
||||
}
|
||||
catch (OfferingSlugConflictException ex)
|
||||
{
|
||||
return Conflict(new ProblemDetails { Status = StatusCodes.Status409Conflict, Title = ex.Message });
|
||||
@@ -80,6 +88,13 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
private IActionResult InvalidCategory() => BadRequest(new ValidationProblemDetails(
|
||||
new Dictionary<string, string[]> { ["categoryId"] = ["invalid"] })
|
||||
{
|
||||
Status = StatusCodes.Status400BadRequest,
|
||||
Title = "Invalid category",
|
||||
});
|
||||
|
||||
private Guid GetCallerId()
|
||||
{
|
||||
var claim = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub");
|
||||
|
||||
Reference in New Issue
Block a user