Offerings: banner krijgt positie top|bottom (backend, migratie, admin-tab, OpenAPI)

- OfferingBanner.Position ('top'|'bottom'); bestaande banners worden 'bottom' (migratie AddOfferingBannerPosition)
- GET /api/v1/offerings/banner?position=top|bottom: zonder parameter bottom, 204 zonder actieve banner, 400 bij ongeldige waarde; antwoord bevat position
- Max 1 actieve banner per positie (laatst gewijzigde binnen de planning wint); top en bottom kunnen tegelijk actief zijn
- Top: tekst max 120 tekens, titel optioneel; bottom: titel verplicht
- Admin: Banner-tab met twee secties (Boven/Onder), eigen aanmaken en aan/uit per plek
- OpenAPI-snapshot met de hand bijgewerkt (geen lokale MariaDB): CI bevestigt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 12:35:20 +01:00
co-authored by Claude Sonnet 5.5
parent a349cfe475
commit 4b09089ffb
19 changed files with 781 additions and 138 deletions
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using SlpModularCms.Core.Availability;
using SlpModularCms.Modules.Offerings.Data.Entities;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Services;
@@ -42,7 +43,10 @@ public class OfferingBannersController(IOfferingBannersService service) : Contro
}
}
/// <summary>The banner visitors see: 200 with the one active banner, otherwise 204.</summary>
/// <summary>
/// The banner visitors see at a position (<c>?position=top|bottom</c>, default bottom):
/// 200 with the one active banner, 204 when none, 400 for an unknown position.
/// </summary>
[ApiController]
[Route("offerings/banner")]
public class PublicOfferingBannerController(IOfferingBannersService service) : ControllerBase
@@ -51,10 +55,14 @@ public class PublicOfferingBannerController(IOfferingBannersService service) : C
[AllowAnonymous]
[PublicSiteEndpoint]
[EnableRateLimiting("offerings-public")]
public async Task<IActionResult> Get()
public async Task<IActionResult> Get([FromQuery] string? position = null)
{
position = string.IsNullOrEmpty(position) ? OfferingBannerPositions.Bottom : position;
if (!OfferingBannerPositions.IsValid(position))
return BadRequest(new { error = "Position must be 'top' or 'bottom'." });
Response.Headers.CacheControl = "public, max-age=60";
var current = await service.GetActiveAsync();
var current = await service.GetActiveAsync(position);
return current is null ? NoContent() : Ok(current);
}
}
@@ -1,5 +1,19 @@
namespace SlpModularCms.Modules.Offerings.Data.Entities;
/// <summary>The two places a banner can be shown.</summary>
public static class OfferingBannerPositions
{
/// <summary>Narrow bar above the page, one line.</summary>
public const string Top = "top";
/// <summary>Large banner; also the default for banners that existed before positions did.</summary>
public const string Bottom = "bottom";
public const int TopTextMaxLength = 120;
public static bool IsValid(string? position) => position is Top or Bottom;
}
/// <summary>
/// A site-wide banner for offers and campaigns. Several can be stored (for example
/// planned ones with a start and end); the public API shows the one that is active right now.
@@ -7,6 +21,10 @@ namespace SlpModularCms.Modules.Offerings.Data.Entities;
public class OfferingBanner
{
public Guid Id { get; set; }
/// <summary>Where the banner is shown: see <see cref="OfferingBannerPositions"/>.</summary>
public string Position { get; set; } = OfferingBannerPositions.Bottom;
/// <summary>Required for a bottom banner; may be empty for a top banner.</summary>
public string Title { get; set; } = string.Empty;
public string Text { get; set; } = string.Empty;
@@ -58,6 +58,7 @@ public class OfferingsDbContext(DbContextOptions<OfferingsDbContext> options) :
{
entity.ToTable("OfferingsBanners");
entity.HasKey(e => e.Id);
entity.Property(e => e.Position).IsRequired().HasMaxLength(10).HasDefaultValue(OfferingBannerPositions.Bottom);
entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
entity.Property(e => e.Text).IsRequired().HasMaxLength(300);
entity.Property(e => e.LinkLabel).HasMaxLength(50);
@@ -0,0 +1,189 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SlpModularCms.Modules.Offerings.Data;
#nullable disable
namespace SlpModularCms.Modules.Offerings.Migrations
{
[DbContext(typeof(OfferingsDbContext))]
[Migration("20261114100000_AddOfferingBannerPosition")]
partial class AddOfferingBannerPosition
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("SlpModularCms.Modules.Offerings.Data.Entities.OfferingBanner", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<DateTimeOffset?>("EndsAt")
.HasColumnType("datetime");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("LinkLabel")
.HasMaxLength(50)
.HasColumnType("varchar(50)");
b.Property<string>("LinkUrl")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<DateTimeOffset?>("StartsAt")
.HasColumnType("datetime");
b.Property<string>("Position")
.IsRequired()
.ValueGeneratedOnAdd()
.HasMaxLength(10)
.HasColumnType("varchar(10)")
.HasDefaultValue("bottom");
b.Property<string>("Text")
.IsRequired()
.HasMaxLength(300)
.HasColumnType("varchar(300)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.ToTable("OfferingsBanners", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Offerings.Data.Entities.Offering", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<Guid?>("CategoryId")
.HasColumnType("char(36)");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<string>("CtaLabel")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("varchar(50)");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetime");
b.Property<string>("Description")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<int>("DisplayOrder")
.HasColumnType("int");
b.Property<bool>("Featured")
.HasColumnType("tinyint(1)");
b.Property<string>("Features")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("IsDeleted")
.HasColumnType("tinyint(1)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("Price")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("varchar(50)");
b.Property<string>("PriceNote")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Slug")
.IsRequired()
.HasMaxLength(130)
.HasColumnType("varchar(130)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("CategoryId");
b.ToTable("OfferingsOfferings", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Offerings.Data.Entities.OfferingCategory", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<int>("SortOrder")
.HasColumnType("int");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("OfferingsCategories", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Offerings.Data.Entities.Offering", b =>
{
b.HasOne("SlpModularCms.Modules.Offerings.Data.Entities.OfferingCategory", "Category")
.WithMany()
.HasForeignKey("CategoryId")
.OnDelete(DeleteBehavior.Restrict);
b.Navigation("Category");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SlpModularCms.Modules.Offerings.Migrations
{
/// <inheritdoc />
public partial class AddOfferingBannerPosition : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// Existing banners were the large one: they become 'bottom'.
migrationBuilder.AddColumn<string>(
name: "Position",
table: "OfferingsBanners",
type: "varchar(10)",
maxLength: 10,
nullable: false,
defaultValue: "bottom");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Position",
table: "OfferingsBanners");
}
}
}
@@ -48,6 +48,13 @@ namespace SlpModularCms.Modules.Offerings.Migrations
b.Property<DateTimeOffset?>("StartsAt")
.HasColumnType("datetime");
b.Property<string>("Position")
.IsRequired()
.ValueGeneratedOnAdd()
.HasMaxLength(10)
.HasColumnType("varchar(10)")
.HasDefaultValue("bottom");
b.Property<string>("Text")
.IsRequired()
.HasMaxLength(300)
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using SlpModularCms.Modules.Offerings.Data.Entities;
namespace SlpModularCms.Modules.Offerings.Models;
@@ -7,6 +8,7 @@ namespace SlpModularCms.Modules.Offerings.Models;
[ExcludeFromCodeCoverage]
public record OfferingBannerDto(
string Id,
string Position,
string Title,
string Text,
string? LinkLabel,
@@ -18,20 +20,31 @@ public record OfferingBannerDto(
/// <summary>What visitors get: only the content to show, never the scheduling.</summary>
[ExcludeFromCodeCoverage]
public record PublicOfferingBannerDto(string Title, string Text, string? LinkLabel, string? LinkUrl);
public record PublicOfferingBannerDto(string Title, string Text, string? LinkLabel, string? LinkUrl, string Position);
/// <summary>Create and full-replace (PUT) body. Empty link fields are stored as null.</summary>
public record OfferingBannerRequest(
[Required, MaxLength(100)] string Title,
[MaxLength(100)] string Title,
[Required, MaxLength(300)] string Text,
[MaxLength(50)] string? LinkLabel = null,
[MaxLength(500)] string? LinkUrl = null,
bool IsActive = true,
DateTimeOffset? StartsAt = null,
DateTimeOffset? EndsAt = null) : IValidatableObject
DateTimeOffset? EndsAt = null,
string Position = OfferingBannerPositions.Bottom) : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!OfferingBannerPositions.IsValid(Position))
yield return new ValidationResult("Position must be 'top' or 'bottom'.", [nameof(Position)]);
else if (Position == OfferingBannerPositions.Top)
{
if (Text?.Trim().Length > OfferingBannerPositions.TopTextMaxLength)
yield return new ValidationResult($"A top banner text can be at most {OfferingBannerPositions.TopTextMaxLength} characters.", [nameof(Text)]);
}
else if (string.IsNullOrWhiteSpace(Title))
yield return new ValidationResult("A bottom banner needs a title.", [nameof(Title)]);
var hasLabel = !string.IsNullOrWhiteSpace(LinkLabel);
var hasUrl = !string.IsNullOrWhiteSpace(LinkUrl);
@@ -10,8 +10,8 @@ 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();
/// <summary>The one banner at <paramref name="position"/> that is switched on and inside its schedule right now, or null.</summary>
Task<PublicOfferingBannerDto?> GetActiveAsync(string position);
Task<OfferingBannerDto> CreateAsync(OfferingBannerRequest request, Guid callerId);
Task<OfferingBannerDto?> UpdateAsync(Guid id, OfferingBannerRequest request, Guid callerId);
@@ -29,17 +29,19 @@ public class OfferingBannersService(
public async Task<IReadOnlyList<OfferingBannerDto>> ListAsync()
=> (await repository.GetAllAsync()).Select(ToDto).ToList();
public async Task<PublicOfferingBannerDto?> GetActiveAsync()
public async Task<PublicOfferingBannerDto?> GetActiveAsync(string position)
{
var now = _time.GetUtcNow();
// Several can be switched on (planned ones, overlaps); the most recently changed wins.
// Several can be switched on (planned ones, overlaps); per position the most recently changed wins,
// so a top and a bottom banner can be shown at the same time.
var current = (await repository.GetSwitchedOnAsync())
.Where(b => b.Position == position)
.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);
return current is null ? null : new PublicOfferingBannerDto(current.Title, current.Text, current.LinkLabel, current.LinkUrl, current.Position);
}
public async Task<OfferingBannerDto> CreateAsync(OfferingBannerRequest request, Guid callerId)
@@ -83,7 +85,8 @@ public class OfferingBannersService(
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.Position = r.Position;
b.Title = (r.Title ?? string.Empty).Trim();
b.Text = r.Text.Trim();
b.LinkLabel = hasLink ? r.LinkLabel!.Trim() : null;
b.LinkUrl = hasLink ? r.LinkUrl!.Trim() : null;
@@ -101,5 +104,5 @@ public class OfferingBannersService(
}
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);
b.Id.ToString(), b.Position, b.Title, b.Text, b.LinkLabel, b.LinkUrl, b.IsActive, b.StartsAt, b.EndsAt, b.UpdatedAt);
}