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:
@@ -3,6 +3,7 @@ using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
@@ -71,6 +72,17 @@ public class OfferingsControllerTests
|
||||
result.Should().BeOfType<CreatedAtActionResult>().Which.StatusCode.Should().Be(201);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns409_WhenSlugTaken()
|
||||
{
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.ThrowsAsync(new OfferingSlugConflictException("taken"));
|
||||
|
||||
var result = await CreateSut().Create(CreateRequest());
|
||||
|
||||
result.Should().BeOfType<ConflictObjectResult>().Which.StatusCode.Should().Be(409);
|
||||
}
|
||||
|
||||
// --- Update ---
|
||||
|
||||
[Fact]
|
||||
@@ -94,6 +106,17 @@ public class OfferingsControllerTests
|
||||
result.Should().BeOfType<NotFoundResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_Returns409_WhenSlugTaken()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.ThrowsAsync(new OfferingSlugConflictException("taken"));
|
||||
|
||||
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
||||
|
||||
result.Should().BeOfType<ConflictObjectResult>().Which.StatusCode.Should().Be(409);
|
||||
}
|
||||
|
||||
// --- Delete ---
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -143,6 +143,64 @@ public class OfferingsServiceTests
|
||||
await _repo.DidNotReceive().ExistsBySlugAsync(Arg.Any<string>(), Arg.Any<Guid?>());
|
||||
}
|
||||
|
||||
// --- Explicit slug override (admin-supplied via CMS) ---
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_NormalizesExplicitSlug()
|
||||
{
|
||||
_repo.GetMaxDisplayOrderAsync().Returns(-1);
|
||||
_repo.ExistsBySlugAsync("my-custom-slug", null).Returns(false);
|
||||
|
||||
var request = new CreateOfferingRequest(
|
||||
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: " My Custom Slug!! ");
|
||||
var dto = await CreateSut().CreateAsync(request, Guid.NewGuid());
|
||||
|
||||
dto.Slug.Should().Be("my-custom-slug");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_Throws_WhenExplicitSlugAlreadyTaken()
|
||||
{
|
||||
_repo.GetMaxDisplayOrderAsync().Returns(-1);
|
||||
_repo.ExistsBySlugAsync("taken", null).Returns(true);
|
||||
|
||||
var request = new CreateOfferingRequest(
|
||||
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "taken");
|
||||
|
||||
var act = () => CreateSut().CreateAsync(request, Guid.NewGuid());
|
||||
|
||||
await act.Should().ThrowAsync<OfferingSlugConflictException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_UsesExplicitSlug_EvenWhenTitleUnchanged()
|
||||
{
|
||||
var target = ExistingOffering();
|
||||
_repo.GetByIdAsync(target.Id).Returns(target);
|
||||
_repo.ExistsBySlugAsync("manually-set", target.Id).Returns(false);
|
||||
|
||||
var request = new UpdateOfferingRequest(
|
||||
target.Title, "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "manually-set");
|
||||
var dto = await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
|
||||
|
||||
dto!.Slug.Should().Be("manually-set");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_Throws_WhenExplicitSlugTakenByAnotherOffering()
|
||||
{
|
||||
var target = ExistingOffering();
|
||||
_repo.GetByIdAsync(target.Id).Returns(target);
|
||||
_repo.ExistsBySlugAsync("taken", target.Id).Returns(true);
|
||||
|
||||
var request = new UpdateOfferingRequest(
|
||||
target.Title, "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "taken");
|
||||
|
||||
var act = () => CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
|
||||
|
||||
await act.Should().ThrowAsync<OfferingSlugConflictException>();
|
||||
}
|
||||
|
||||
// --- UpdateAsync ---
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user