Offerings: OfferingBanner (planbare banner, GET /offerings/banner) en layout verwijderd (backend)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
public interface IOfferingBannersService
|
||||
{
|
||||
Task<IReadOnlyList<OfferingBannerDto>> ListAsync();
|
||||
|
||||
/// <summary>The one banner that is switched on and inside its schedule right now, or null.</summary>
|
||||
Task<PublicOfferingBannerDto?> GetActiveAsync();
|
||||
|
||||
Task<OfferingBannerDto> CreateAsync(OfferingBannerRequest request, Guid callerId);
|
||||
Task<OfferingBannerDto?> UpdateAsync(Guid id, OfferingBannerRequest request, Guid callerId);
|
||||
Task<bool> DeleteAsync(Guid id);
|
||||
}
|
||||
|
||||
public class OfferingBannersService(
|
||||
IOfferingBannerRepository repository,
|
||||
ILogger<OfferingBannersService> logger,
|
||||
TimeProvider? time = null,
|
||||
IIndexHtmlCache? indexCache = null) : IOfferingBannersService
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
|
||||
public async Task<IReadOnlyList<OfferingBannerDto>> ListAsync()
|
||||
=> (await repository.GetAllAsync()).Select(ToDto).ToList();
|
||||
|
||||
public async Task<PublicOfferingBannerDto?> GetActiveAsync()
|
||||
{
|
||||
var now = _time.GetUtcNow();
|
||||
|
||||
// Several can be switched on (planned ones, overlaps); the most recently changed wins.
|
||||
var current = (await repository.GetSwitchedOnAsync())
|
||||
.Where(b => (b.StartsAt is null || b.StartsAt <= now) && (b.EndsAt is null || now < b.EndsAt))
|
||||
.OrderByDescending(b => b.UpdatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
return current is null ? null : new PublicOfferingBannerDto(current.Title, current.Text, current.LinkLabel, current.LinkUrl);
|
||||
}
|
||||
|
||||
public async Task<OfferingBannerDto> CreateAsync(OfferingBannerRequest request, Guid callerId)
|
||||
{
|
||||
var now = _time.GetUtcNow();
|
||||
var banner = new OfferingBanner { Id = Guid.NewGuid(), CreatedAt = now };
|
||||
Apply(banner, request, now, callerId);
|
||||
|
||||
await repository.AddAsync(banner);
|
||||
await repository.SaveChangesAsync();
|
||||
|
||||
Changed(banner.Id, "Created", callerId);
|
||||
return ToDto(banner);
|
||||
}
|
||||
|
||||
public async Task<OfferingBannerDto?> UpdateAsync(Guid id, OfferingBannerRequest request, Guid callerId)
|
||||
{
|
||||
var banner = await repository.GetByIdAsync(id);
|
||||
if (banner is null) return null;
|
||||
|
||||
Apply(banner, request, _time.GetUtcNow(), callerId);
|
||||
repository.Update(banner);
|
||||
await repository.SaveChangesAsync();
|
||||
|
||||
Changed(id, "Updated", callerId);
|
||||
return ToDto(banner);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id)
|
||||
{
|
||||
var banner = await repository.GetByIdAsync(id);
|
||||
if (banner is null) return false;
|
||||
|
||||
repository.Remove(banner);
|
||||
await repository.SaveChangesAsync();
|
||||
|
||||
Changed(id, "Deleted", null);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void Apply(OfferingBanner b, OfferingBannerRequest r, DateTimeOffset now, Guid callerId)
|
||||
{
|
||||
var hasLink = !string.IsNullOrWhiteSpace(r.LinkUrl) && !string.IsNullOrWhiteSpace(r.LinkLabel);
|
||||
b.Title = r.Title.Trim();
|
||||
b.Text = r.Text.Trim();
|
||||
b.LinkLabel = hasLink ? r.LinkLabel!.Trim() : null;
|
||||
b.LinkUrl = hasLink ? r.LinkUrl!.Trim() : null;
|
||||
b.IsActive = r.IsActive;
|
||||
b.StartsAt = r.StartsAt;
|
||||
b.EndsAt = r.EndsAt;
|
||||
b.UpdatedAt = now;
|
||||
b.LastModifiedByUserId = callerId;
|
||||
}
|
||||
|
||||
private void Changed(Guid id, string action, Guid? callerId)
|
||||
{
|
||||
indexCache?.Invalidate();
|
||||
logger.LogInformation("Offering banner {BannerId} {Action} by user {LastModifiedByUserId}", id, action, callerId);
|
||||
}
|
||||
|
||||
private static OfferingBannerDto ToDto(OfferingBanner b) => new(
|
||||
b.Id.ToString(), b.Title, b.Text, b.LinkLabel, b.LinkUrl, b.IsActive, b.StartsAt, b.EndsAt, b.UpdatedAt);
|
||||
}
|
||||
@@ -35,7 +35,6 @@ 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,
|
||||
@@ -46,11 +45,10 @@ 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 || offering.Layout == OfferingLayouts.Banner)
|
||||
if (request.Featured)
|
||||
{
|
||||
await using var transaction = await repository.BeginTransactionAsync();
|
||||
if (request.Featured) await UnfeatureCurrentAsync();
|
||||
if (offering.Layout == OfferingLayouts.Banner) await DemoteCurrentBannerAsync();
|
||||
await UnfeatureCurrentAsync();
|
||||
await repository.AddAsync(offering);
|
||||
await repository.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
@@ -95,16 +93,13 @@ 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 || offering.Layout == OfferingLayouts.Banner)
|
||||
if (request.Featured)
|
||||
{
|
||||
await using var transaction = await repository.BeginTransactionAsync();
|
||||
if (request.Featured) await UnfeatureCurrentAsync(exceptId: offering.Id);
|
||||
if (offering.Layout == OfferingLayouts.Banner) await DemoteCurrentBannerAsync(exceptId: offering.Id);
|
||||
await UnfeatureCurrentAsync(exceptId: offering.Id);
|
||||
repository.Update(offering);
|
||||
await repository.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
@@ -193,18 +188,6 @@ 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)
|
||||
@@ -260,7 +243,6 @@ public class OfferingsService(
|
||||
o.Featured,
|
||||
o.DisplayOrder,
|
||||
o.CategoryId?.ToString(),
|
||||
o.Category?.Name,
|
||||
o.Layout
|
||||
o.Category?.Name
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user