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,91 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
public interface IOfferingCategoriesService
|
||||
{
|
||||
Task<IReadOnlyList<OfferingCategoryDto>> ListAsync();
|
||||
Task<OfferingCategoryDto> CreateAsync(string name);
|
||||
Task<OfferingCategoryDto?> RenameAsync(Guid id, string name);
|
||||
Task<bool> DeleteAsync(Guid id);
|
||||
}
|
||||
|
||||
public class OfferingCategoriesService(
|
||||
IOfferingCategoryRepository repository,
|
||||
ILogger<OfferingCategoriesService> logger) : IOfferingCategoriesService
|
||||
{
|
||||
public async Task<IReadOnlyList<OfferingCategoryDto>> ListAsync()
|
||||
{
|
||||
var categories = await repository.ListAsync();
|
||||
return categories.Select(c => ToDto(c.Category, c.OfferingCount)).ToList();
|
||||
}
|
||||
|
||||
public async Task<OfferingCategoryDto> CreateAsync(string name)
|
||||
{
|
||||
var trimmed = name.Trim();
|
||||
if (await repository.ExistsByNameAsync(trimmed))
|
||||
throw new OfferingCategoryNameConflictException(trimmed);
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var category = new OfferingCategory
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = trimmed,
|
||||
SortOrder = await repository.GetMaxSortOrderAsync() + 1,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
|
||||
await repository.AddAsync(category);
|
||||
await repository.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation("Offering category {CategoryId} created", category.Id);
|
||||
return ToDto(category, 0);
|
||||
}
|
||||
|
||||
public async Task<OfferingCategoryDto?> RenameAsync(Guid id, string name)
|
||||
{
|
||||
var category = await repository.GetByIdAsync(id);
|
||||
if (category is null)
|
||||
return null;
|
||||
|
||||
var trimmed = name.Trim();
|
||||
if (await repository.ExistsByNameAsync(trimmed, excludeId: id))
|
||||
throw new OfferingCategoryNameConflictException(trimmed);
|
||||
|
||||
category.Name = trimmed;
|
||||
category.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
await repository.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation("Offering category {CategoryId} renamed", category.Id);
|
||||
return ToDto(category, await repository.CountOfferingsAsync(id));
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id)
|
||||
{
|
||||
var category = await repository.GetByIdAsync(id);
|
||||
if (category is null)
|
||||
return false;
|
||||
|
||||
var inUse = await repository.CountOfferingsAsync(id);
|
||||
if (inUse > 0)
|
||||
throw new OfferingCategoryInUseException(inUse);
|
||||
|
||||
// Soft-deleted offerings may still point at the category; release them so the FK
|
||||
// (Restrict) no longer blocks the deletion. Both steps succeed or fail together.
|
||||
await using var transaction = await repository.BeginTransactionAsync();
|
||||
await repository.ClearCategoryOnDeletedOfferingsAsync(id);
|
||||
repository.Remove(category);
|
||||
await repository.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
|
||||
logger.LogInformation("Offering category {CategoryId} deleted", id);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static OfferingCategoryDto ToDto(OfferingCategory c, int offeringCount)
|
||||
=> new(c.Id.ToString(), c.Name, c.SortOrder, offeringCount);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
/// <summary>Thrown when an offering refers to a category that does not exist.</summary>
|
||||
public class OfferingCategoryInvalidException() : Exception("The category does not exist.");
|
||||
|
||||
/// <summary>Thrown when a category name is already taken (case-insensitive).</summary>
|
||||
public class OfferingCategoryNameConflictException(string name)
|
||||
: Exception($"A category named '{name}' already exists.");
|
||||
|
||||
/// <summary>Thrown when a category that is still used by offerings is deleted.</summary>
|
||||
public class OfferingCategoryInUseException(int offeringCount)
|
||||
: Exception($"The category is still used by {offeringCount} offering(s). Move them to another category first.");
|
||||
@@ -5,7 +5,10 @@ using SlpModularCms.Modules.Offerings.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsService> logger) : IOfferingsService
|
||||
public class OfferingsService(
|
||||
IOfferingRepository repository,
|
||||
IOfferingCategoryRepository categories,
|
||||
ILogger<OfferingsService> logger) : IOfferingsService
|
||||
{
|
||||
public async Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync()
|
||||
{
|
||||
@@ -15,6 +18,7 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
|
||||
public async Task<OfferingDto> CreateAsync(CreateOfferingRequest request, Guid callerId)
|
||||
{
|
||||
var category = await ResolveCategoryAsync(request.CategoryId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var offering = new Offering
|
||||
{
|
||||
@@ -26,6 +30,8 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
PriceNote = request.PriceNote,
|
||||
Features = request.Features,
|
||||
CtaLabel = request.CtaLabel,
|
||||
CategoryId = category?.Id,
|
||||
Category = category,
|
||||
Featured = request.Featured,
|
||||
DisplayOrder = await repository.GetMaxDisplayOrderAsync() + 1,
|
||||
CreatedAt = now,
|
||||
@@ -64,6 +70,8 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
if (offering is null)
|
||||
return null;
|
||||
|
||||
var category = await ResolveCategoryAsync(request.CategoryId);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Slug))
|
||||
{
|
||||
offering.Slug = await ResolveExplicitSlugAsync(request.Slug, excludeId: offering.Id);
|
||||
@@ -79,6 +87,8 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
offering.PriceNote = request.PriceNote;
|
||||
offering.Features = request.Features;
|
||||
offering.CtaLabel = request.CtaLabel;
|
||||
offering.CategoryId = category?.Id;
|
||||
offering.Category = category;
|
||||
offering.Featured = request.Featured;
|
||||
offering.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
offering.LastModifiedByUserId = callerId;
|
||||
@@ -172,6 +182,15 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
repository.Update(current);
|
||||
}
|
||||
|
||||
private async Task<OfferingCategory?> ResolveCategoryAsync(Guid? categoryId)
|
||||
{
|
||||
if (categoryId is null)
|
||||
return null;
|
||||
|
||||
return await categories.GetByIdAsync(categoryId.Value)
|
||||
?? throw new OfferingCategoryInvalidException();
|
||||
}
|
||||
|
||||
private async Task<string> ResolveSlugAsync(string? explicitSlug, string title)
|
||||
=> !string.IsNullOrWhiteSpace(explicitSlug)
|
||||
? await ResolveExplicitSlugAsync(explicitSlug, excludeId: null)
|
||||
@@ -216,6 +235,8 @@ public class OfferingsService(IOfferingRepository repository, ILogger<OfferingsS
|
||||
o.Features,
|
||||
o.CtaLabel,
|
||||
o.Featured,
|
||||
o.DisplayOrder
|
||||
o.DisplayOrder,
|
||||
o.CategoryId?.ToString(),
|
||||
o.Category?.Name
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user