Maak slug in CMS zichtbaar en inline aanpasbaar

De CMS-admin toonde het nieuwe Slug-veld nog niet — dat bestond alleen
in de API-respons. Voegt een read-only slugweergave onder het
titelveld toe met een potlood-icoon om 'm inline te bewerken.

Backend: Create/UpdateOfferingRequest accepteren nu een optionele
Slug-override. Zonder expliciete waarde blijft het bestaande gedrag
(auto-genereren uit titel, regenereren bij titelwijziging). Met een
expliciete waarde wordt die genormaliseerd en op uniekheid
gecontroleerd; een botsing geeft 409 Conflict (RFC 9457 ProblemDetails,
naar het patroon van MasterControlledAvailabilityException).

Frontend: de slug wordt alleen meegestuurd als de admin 'm daadwerkelijk
bewerkt heeft (dirtyFields.slug), zodat een ongemoeide slug bij een
titelwijziging gewoon blijft auto-regenereren.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-04 15:31:10 +02:00
co-authored by Claude Sonnet 5
parent cc09b46cfb
commit 1b974696bf
15 changed files with 332 additions and 14 deletions
@@ -1,5 +1,6 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using SlpModularCms.Modules.Offerings.Models;
@@ -31,15 +32,29 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
[HttpPost("admin")]
public async Task<IActionResult> Create([FromBody] CreateOfferingRequest request)
{
var dto = await service.CreateAsync(request, GetCallerId());
return CreatedAtAction(nameof(GetAllForAdmin), null, dto);
try
{
var dto = await service.CreateAsync(request, GetCallerId());
return CreatedAtAction(nameof(GetAllForAdmin), null, dto);
}
catch (OfferingSlugConflictException ex)
{
return Conflict(new ProblemDetails { Status = StatusCodes.Status409Conflict, Title = ex.Message });
}
}
[HttpPut("admin/{id:guid}")]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateOfferingRequest request)
{
var dto = await service.UpdateAsync(id, request, GetCallerId());
return dto is null ? NotFound() : Ok(dto);
try
{
var dto = await service.UpdateAsync(id, request, GetCallerId());
return dto is null ? NotFound() : Ok(dto);
}
catch (OfferingSlugConflictException ex)
{
return Conflict(new ProblemDetails { Status = StatusCodes.Status409Conflict, Title = ex.Message });
}
}
[HttpDelete("admin/{id:guid}")]
@@ -9,5 +9,6 @@ public record CreateOfferingRequest(
[Required, MaxLength(100)] string PriceNote,
[Required, FeaturesValidation] List<string> Features,
[Required, MaxLength(50)] string CtaLabel,
bool Featured = false
bool Featured = false,
[MaxLength(130)] string? Slug = null
);
@@ -9,5 +9,6 @@ public record UpdateOfferingRequest(
[Required, MaxLength(100)] string PriceNote,
[Required, FeaturesValidation] List<string> Features,
[Required, MaxLength(50)] string CtaLabel,
bool Featured = false
bool Featured = false,
[MaxLength(130)] string? Slug = null
);
@@ -0,0 +1,12 @@
namespace SlpModularCms.Modules.Offerings.Services;
/// <summary>
/// Thrown when an admin explicitly sets a slug that's already in use by another offering.
/// Auto-generated slugs never hit this (they get a numeric suffix instead) — this is only
/// for the CMS's explicit slug override.
/// </summary>
public class OfferingSlugConflictException(string slug)
: InvalidOperationException($"Slug '{slug}' is al in gebruik door een ander aanbod.")
{
public string Slug { get; } = slug;
}
@@ -25,7 +25,7 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
var offering = new Offering
{
Id = Guid.NewGuid(),
Slug = await GenerateUniqueSlugAsync(request.Title),
Slug = await ResolveSlugAsync(request.Slug, request.Title),
Title = request.Title,
Description = request.Description,
Price = request.Price,
@@ -70,8 +70,14 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
if (offering is null)
return null;
if (!string.Equals(offering.Title, request.Title, StringComparison.Ordinal))
if (!string.IsNullOrWhiteSpace(request.Slug))
{
offering.Slug = await ResolveExplicitSlugAsync(request.Slug, excludeId: offering.Id);
}
else if (!string.Equals(offering.Title, request.Title, StringComparison.Ordinal))
{
offering.Slug = await GenerateUniqueSlugAsync(request.Title, excludeId: offering.Id);
}
offering.Title = request.Title;
offering.Description = request.Description;
@@ -206,6 +212,25 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
repository.Update(current);
}
private async Task<string> ResolveSlugAsync(string? explicitSlug, string title)
=> !string.IsNullOrWhiteSpace(explicitSlug)
? await ResolveExplicitSlugAsync(explicitSlug, excludeId: null)
: await GenerateUniqueSlugAsync(title);
/// <summary>
/// Normalizes an admin-supplied slug and enforces uniqueness. Unlike auto-generation,
/// a taken slug is rejected outright (OfferingSlugConflictException) rather than
/// silently suffixed — the admin picked this value on purpose.
/// </summary>
private async Task<string> ResolveExplicitSlugAsync(string explicitSlug, Guid? excludeId)
{
var slug = Slugifier.Slugify(explicitSlug);
if (await repository.ExistsBySlugAsync(slug, excludeId))
throw new OfferingSlugConflictException(slug);
return slug;
}
private async Task<string> GenerateUniqueSlugAsync(string title, Guid? excludeId = null)
{
var baseSlug = Slugifier.Slugify(title);