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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user