Offerings: layout (card|banner) op aanbod, max 1 banner, admin-keuze, additief in GET /api/v1/offerings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,6 +33,7 @@ public class OfferingsService(
|
||||
CategoryId = category?.Id,
|
||||
Category = category,
|
||||
Featured = request.Featured,
|
||||
Layout = request.Layout ?? OfferingLayouts.Card,
|
||||
DisplayOrder = await repository.GetMaxDisplayOrderAsync() + 1,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
@@ -43,10 +44,11 @@ public class OfferingsService(
|
||||
// transactional per NFR Design Pattern 1 so a mid-operation failure can never
|
||||
// leave two offerings both featured or the new one unsaved after the old one
|
||||
// was already un-featured.
|
||||
if (request.Featured)
|
||||
if (request.Featured || offering.Layout == OfferingLayouts.Banner)
|
||||
{
|
||||
await using var transaction = await repository.BeginTransactionAsync();
|
||||
await UnfeatureCurrentAsync();
|
||||
if (request.Featured) await UnfeatureCurrentAsync();
|
||||
if (offering.Layout == OfferingLayouts.Banner) await DemoteCurrentBannerAsync();
|
||||
await repository.AddAsync(offering);
|
||||
await repository.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
@@ -90,13 +92,16 @@ public class OfferingsService(
|
||||
offering.CategoryId = category?.Id;
|
||||
offering.Category = category;
|
||||
offering.Featured = request.Featured;
|
||||
// Layout is additive: older clients omit it, which keeps the stored value.
|
||||
if (request.Layout is not null) offering.Layout = request.Layout;
|
||||
offering.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
offering.LastModifiedByUserId = callerId;
|
||||
|
||||
if (request.Featured)
|
||||
if (request.Featured || offering.Layout == OfferingLayouts.Banner)
|
||||
{
|
||||
await using var transaction = await repository.BeginTransactionAsync();
|
||||
await UnfeatureCurrentAsync(exceptId: offering.Id);
|
||||
if (request.Featured) await UnfeatureCurrentAsync(exceptId: offering.Id);
|
||||
if (offering.Layout == OfferingLayouts.Banner) await DemoteCurrentBannerAsync(exceptId: offering.Id);
|
||||
repository.Update(offering);
|
||||
await repository.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
@@ -182,6 +187,18 @@ public class OfferingsService(
|
||||
repository.Update(current);
|
||||
}
|
||||
|
||||
// At most one banner per site: promoting another offering demotes the current one to a card.
|
||||
private async Task DemoteCurrentBannerAsync(Guid? exceptId = null)
|
||||
{
|
||||
var current = await repository.GetBannerAsync();
|
||||
if (current is null || current.Id == exceptId)
|
||||
return;
|
||||
|
||||
current.Layout = OfferingLayouts.Card;
|
||||
current.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
repository.Update(current);
|
||||
}
|
||||
|
||||
private async Task<OfferingCategory?> ResolveCategoryAsync(Guid? categoryId)
|
||||
{
|
||||
if (categoryId is null)
|
||||
@@ -237,6 +254,7 @@ public class OfferingsService(
|
||||
o.Featured,
|
||||
o.DisplayOrder,
|
||||
o.CategoryId?.ToString(),
|
||||
o.Category?.Name
|
||||
o.Category?.Name,
|
||||
o.Layout
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user