Aanbod: RESTful routes zonder /admin, één OfferingDto en herordenen via PUT /offerings/order

Admin en bezoeker gebruiken dezelfde resource; alleen GET is anoniem, de rest vereist AdminOnly. OfferingAdminDto vervalt (displayOrder zit nu in OfferingDto), move-up/move-down zijn vervangen door de order-route die een onbekende of onvolledige lijst met 400 afwijst. De admin-frontend en alle tests zijn mee aangepast.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 13:06:08 +01:00
co-authored by Claude Sonnet 5.5
parent e6710781e5
commit bc763c8ef6
39 changed files with 968 additions and 395 deletions
@@ -8,6 +8,11 @@ using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Modules.Offerings.Controllers;
/// <summary>
/// The offerings resource. Reading is public; every write requires the AdminOnly policy.
/// There is deliberately no separate "admin" route: an administrator uses the same resource
/// and verbs, and access is decided by authorization alone.
/// </summary>
[ApiController]
[Route("offerings")]
[Authorize(Policy = "AdminOnly")]
@@ -18,24 +23,17 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
[EnableRateLimiting("offerings-public")]
public async Task<IActionResult> GetOfferings()
{
var offerings = await service.GetPublicOfferingsAsync();
var offerings = await service.GetOfferingsAsync();
return Ok(offerings);
}
[HttpGet("admin")]
public async Task<IActionResult> GetAllForAdmin()
{
var offerings = await service.GetAllForAdminAsync();
return Ok(offerings);
}
[HttpPost("admin")]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateOfferingRequest request)
{
try
{
var dto = await service.CreateAsync(request, GetCallerId());
return CreatedAtAction(nameof(GetAllForAdmin), null, dto);
return Created($"/api/v1/offerings/{dto.Id}", dto);
}
catch (OfferingSlugConflictException ex)
{
@@ -43,7 +41,7 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
}
}
[HttpPut("admin/{id:guid}")]
[HttpPut("{id:guid}")]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateOfferingRequest request)
{
try
@@ -57,32 +55,29 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
}
}
[HttpDelete("admin/{id:guid}")]
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete(Guid id)
{
var deleted = await service.DeleteAsync(id, GetCallerId());
return deleted ? NoContent() : NotFound();
}
[HttpPut("admin/reorder")]
/// <summary>
/// Sets the display order of the whole collection. The list must contain every existing
/// offering exactly once.
/// </summary>
[HttpPut("order")]
public async Task<IActionResult> Reorder([FromBody] ReorderOfferingsRequest request)
{
await service.ReorderAsync(request.OrderedIds, GetCallerId());
return NoContent();
}
[HttpPost("admin/{id:guid}/move-up")]
public async Task<IActionResult> MoveUp(Guid id)
{
await service.MoveUpAsync(id, GetCallerId());
return NoContent();
}
[HttpPost("admin/{id:guid}/move-down")]
public async Task<IActionResult> MoveDown(Guid id)
{
await service.MoveDownAsync(id, GetCallerId());
return NoContent();
try
{
await service.ReorderAsync(request.OrderedIds, GetCallerId());
return NoContent();
}
catch (OfferingOrderInvalidException ex)
{
return BadRequest(new ProblemDetails { Status = StatusCodes.Status400BadRequest, Title = ex.Message });
}
}
private Guid GetCallerId()
@@ -1,17 +0,0 @@
using System.Diagnostics.CodeAnalysis;
namespace SlpModularCms.Modules.Offerings.Models;
[ExcludeFromCodeCoverage]
public record OfferingAdminDto(
string Id,
string Slug,
string Title,
string Description,
string Price,
string PriceNote,
IReadOnlyList<string> Features,
string CtaLabel,
bool Featured,
int DisplayOrder
);
@@ -2,6 +2,10 @@ using System.Diagnostics.CodeAnalysis;
namespace SlpModularCms.Modules.Offerings.Models;
/// <summary>
/// The one representation of an offering, for anonymous visitors and administrators alike.
/// <c>DisplayOrder</c> is informational (the array order already is the display order).
/// </summary>
[ExcludeFromCodeCoverage]
public record OfferingDto(
string Id,
@@ -12,5 +16,6 @@ public record OfferingDto(
string PriceNote,
IReadOnlyList<string> Features,
string CtaLabel,
bool Featured
bool Featured,
int DisplayOrder
);
@@ -12,8 +12,6 @@ public interface IOfferingRepository
void Update(Offering offering);
Task<int> GetMaxDisplayOrderAsync();
Task<Offering?> GetFeaturedAsync();
Task<Offering?> GetPreviousAsync(int displayOrder);
Task<Offering?> GetNextAsync(int displayOrder);
Task SaveChangesAsync();
Task<IDbContextTransaction> BeginTransactionAsync();
}
@@ -28,18 +28,6 @@ public class OfferingRepository(OfferingsDbContext context) : IOfferingRepositor
public async Task<Offering?> GetFeaturedAsync()
=> await context.Offerings.FirstOrDefaultAsync(o => o.Featured);
public async Task<Offering?> GetPreviousAsync(int displayOrder)
=> await context.Offerings
.Where(o => o.DisplayOrder < displayOrder)
.OrderByDescending(o => o.DisplayOrder)
.FirstOrDefaultAsync();
public async Task<Offering?> GetNextAsync(int displayOrder)
=> await context.Offerings
.Where(o => o.DisplayOrder > displayOrder)
.OrderBy(o => o.DisplayOrder)
.FirstOrDefaultAsync();
public async Task SaveChangesAsync()
=> await context.SaveChangesAsync();
@@ -4,12 +4,14 @@ namespace SlpModularCms.Modules.Offerings.Services;
public interface IOfferingsService
{
Task<IReadOnlyList<OfferingDto>> GetPublicOfferingsAsync();
Task<IReadOnlyList<OfferingAdminDto>> GetAllForAdminAsync();
Task<OfferingAdminDto> CreateAsync(CreateOfferingRequest request, Guid callerId);
Task<OfferingAdminDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId);
Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync();
Task<OfferingDto> CreateAsync(CreateOfferingRequest request, Guid callerId);
Task<OfferingDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId);
Task<bool> DeleteAsync(Guid id, Guid callerId);
/// <summary>
/// Sets the display order of all offerings. <paramref name="orderedIds"/> must contain every
/// existing offering exactly once; otherwise <see cref="OfferingOrderInvalidException"/>.
/// </summary>
Task ReorderAsync(IReadOnlyList<Guid> orderedIds, Guid callerId);
Task MoveUpAsync(Guid id, Guid callerId);
Task MoveDownAsync(Guid id, Guid callerId);
}
@@ -0,0 +1,7 @@
namespace SlpModularCms.Modules.Offerings.Services;
/// <summary>
/// Thrown when a reorder request does not list every existing offering exactly once.
/// </summary>
public class OfferingOrderInvalidException()
: Exception("The order must list every existing offering exactly once.");
@@ -7,19 +7,13 @@ namespace SlpModularCms.Modules.Offerings.Services;
public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsService> logger) : IOfferingsService
{
public async Task<IReadOnlyList<OfferingDto>> GetPublicOfferingsAsync()
public async Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync()
{
var offerings = await repository.GetAllAsync();
return offerings.Select(ToPublicDto).ToList();
return offerings.Select(ToDto).ToList();
}
public async Task<IReadOnlyList<OfferingAdminDto>> GetAllForAdminAsync()
{
var offerings = await repository.GetAllAsync();
return offerings.Select(ToAdminDto).ToList();
}
public async Task<OfferingAdminDto> CreateAsync(CreateOfferingRequest request, Guid callerId)
public async Task<OfferingDto> CreateAsync(CreateOfferingRequest request, Guid callerId)
{
var now = DateTimeOffset.UtcNow;
var offering = new Offering
@@ -61,10 +55,10 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
offering.Id, "Created", callerId);
return ToAdminDto(offering);
return ToDto(offering);
}
public async Task<OfferingAdminDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId)
public async Task<OfferingDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId)
{
var offering = await repository.GetByIdAsync(id);
if (offering is null)
@@ -107,7 +101,7 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
offering.Id, "Updated", callerId);
return ToAdminDto(offering);
return ToDto(offering);
}
public async Task<bool> DeleteAsync(Guid id, Guid callerId)
@@ -134,15 +128,25 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
public async Task ReorderAsync(IReadOnlyList<Guid> orderedIds, Guid callerId)
{
var offerings = await repository.GetAllAsync();
// The list must be a permutation of the existing offerings: no unknown ids, no duplicates,
// none missing. Silently skipping mismatches would leave the display order half-applied.
var existingIds = offerings.Select(o => o.Id).ToHashSet();
if (orderedIds.Count != existingIds.Count
|| orderedIds.Distinct().Count() != orderedIds.Count
|| !orderedIds.All(existingIds.Contains))
{
throw new OfferingOrderInvalidException();
}
await using var transaction = await repository.BeginTransactionAsync();
var byId = offerings.ToDictionary(o => o.Id);
var now = DateTimeOffset.UtcNow;
for (var i = 0; i < orderedIds.Count; i++)
{
var offering = await repository.GetByIdAsync(orderedIds[i]);
if (offering is null)
continue;
var offering = byId[orderedIds[i]];
offering.DisplayOrder = i;
offering.UpdatedAt = now;
offering.LastModifiedByUserId = callerId;
@@ -157,50 +161,6 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
orderedIds.Count, callerId);
}
public async Task MoveUpAsync(Guid id, Guid callerId)
{
var offering = await repository.GetByIdAsync(id);
if (offering is null)
return;
var previous = await repository.GetPreviousAsync(offering.DisplayOrder);
if (previous is null)
return; // BR-OFF-03: already first, no-op
await using var transaction = await repository.BeginTransactionAsync();
SwapDisplayOrder(offering, previous, callerId);
await repository.SaveChangesAsync();
await transaction.CommitAsync();
}
public async Task MoveDownAsync(Guid id, Guid callerId)
{
var offering = await repository.GetByIdAsync(id);
if (offering is null)
return;
var next = await repository.GetNextAsync(offering.DisplayOrder);
if (next is null)
return; // BR-OFF-03: already last, no-op
await using var transaction = await repository.BeginTransactionAsync();
SwapDisplayOrder(offering, next, callerId);
await repository.SaveChangesAsync();
await transaction.CommitAsync();
}
private void SwapDisplayOrder(Offering a, Offering b, Guid callerId)
{
(a.DisplayOrder, b.DisplayOrder) = (b.DisplayOrder, a.DisplayOrder);
var now = DateTimeOffset.UtcNow;
a.UpdatedAt = now;
b.UpdatedAt = now;
a.LastModifiedByUserId = callerId;
b.LastModifiedByUserId = callerId;
repository.Update(a);
repository.Update(b);
}
private async Task UnfeatureCurrentAsync(Guid? exceptId = null)
{
var current = await repository.GetFeaturedAsync();
@@ -246,19 +206,7 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
return slug;
}
private static OfferingDto ToPublicDto(Offering o) => new(
o.Id.ToString(),
o.Slug,
o.Title,
o.Description,
o.Price,
o.PriceNote,
o.Features,
o.CtaLabel,
o.Featured
);
private static OfferingAdminDto ToAdminDto(Offering o) => new(
private static OfferingDto ToDto(Offering o) => new(
o.Id.ToString(),
o.Slug,
o.Title,