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:
@@ -48,6 +48,11 @@ public class HostPipelineTests : IClassFixture<AsfWebApplicationFactory>
|
||||
var banner = await client.GetAsync("/api/v1/offerings/banner");
|
||||
banner.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
|
||||
// Position: top works the same, no parameter means bottom, anything else is rejected.
|
||||
(await client.GetAsync("/api/v1/offerings/banner?position=top")).StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
(await client.GetAsync("/api/v1/offerings/banner?position=bottom")).StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
(await client.GetAsync("/api/v1/offerings/banner?position=left")).StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
|
||||
(await client.GetAsync("/api/v1/offerings")).StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
(await client.GetAsync("/api/v1/offerings/banners")).StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ public class OfferingBannersServiceTests
|
||||
new(_repo, Substitute.For<ILogger<OfferingBannersService>>(), _time, _cache);
|
||||
|
||||
private static OfferingBanner Banner(string title, bool active = true, DateTimeOffset? from = null, DateTimeOffset? to = null,
|
||||
DateTimeOffset? updated = null) => new()
|
||||
DateTimeOffset? updated = null, string position = "bottom") => new()
|
||||
{
|
||||
Id = Guid.NewGuid(), Title = title, Text = "Tekst", IsActive = active, StartsAt = from, EndsAt = to,
|
||||
Id = Guid.NewGuid(), Position = position, Title = title, Text = "Tekst", IsActive = active, StartsAt = from, EndsAt = to,
|
||||
UpdatedAt = updated ?? Now.AddDays(-1),
|
||||
};
|
||||
|
||||
@@ -40,7 +40,7 @@ public class OfferingBannersServiceTests
|
||||
public async Task GetActive_ReturnsNull_WhenNothingIsSwitchedOn()
|
||||
{
|
||||
Switched();
|
||||
(await Sut().GetActiveAsync()).Should().BeNull();
|
||||
(await Sut().GetActiveAsync("bottom")).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -51,9 +51,9 @@ public class OfferingBannersServiceTests
|
||||
banner.LinkUrl = "/aanbieding";
|
||||
Switched(banner);
|
||||
|
||||
var result = await Sut().GetActiveAsync();
|
||||
var result = await Sut().GetActiveAsync("bottom");
|
||||
|
||||
result.Should().Be(new PublicOfferingBannerDto("Actie", "Tekst", "Bekijk", "/aanbieding"));
|
||||
result.Should().Be(new PublicOfferingBannerDto("Actie", "Tekst", "Bekijk", "/aanbieding", "bottom"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -64,24 +64,24 @@ public class OfferingBannersServiceTests
|
||||
Banner("past", to: Now.AddHours(-1)),
|
||||
Banner("ended-exactly-now", to: Now));
|
||||
|
||||
(await Sut().GetActiveAsync()).Should().BeNull();
|
||||
(await Sut().GetActiveAsync("bottom")).Should().BeNull();
|
||||
|
||||
_time.Advance(TimeSpan.FromHours(2));
|
||||
(await Sut().GetActiveAsync())!.Title.Should().Be("future");
|
||||
(await Sut().GetActiveAsync("bottom"))!.Title.Should().Be("future");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActive_StartsInclusive()
|
||||
{
|
||||
Switched(Banner("now", from: Now));
|
||||
(await Sut().GetActiveAsync())!.Title.Should().Be("now");
|
||||
(await Sut().GetActiveAsync("bottom"))!.Title.Should().Be("now");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActive_PicksTheMostRecentlyChanged_WhenSeveralQualify()
|
||||
{
|
||||
Switched(Banner("old", updated: Now.AddDays(-5)), Banner("new", updated: Now.AddDays(-1)));
|
||||
(await Sut().GetActiveAsync())!.Title.Should().Be("new");
|
||||
(await Sut().GetActiveAsync("bottom"))!.Title.Should().Be("new");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -176,12 +176,79 @@ public class OfferingBannersServiceTests
|
||||
maxLength.Length.Should().Be(max);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Title")]
|
||||
[InlineData("Text")]
|
||||
public void TitleAndText_AreRequired(string name)
|
||||
[Fact]
|
||||
public async Task GetActive_FiltersOnPosition_SoTopAndBottomCanBeActiveAtTheSameTime()
|
||||
{
|
||||
var parameter = typeof(OfferingBannerRequest).GetConstructors()[0].GetParameters().Single(p => p.Name == name);
|
||||
Switched(Banner("beneden", position: "bottom"), Banner("boven", position: "top"));
|
||||
|
||||
var top = await Sut().GetActiveAsync("top");
|
||||
var bottom = await Sut().GetActiveAsync("bottom");
|
||||
|
||||
top.Should().Be(new PublicOfferingBannerDto("boven", "Tekst", null, null, "top"));
|
||||
bottom!.Title.Should().Be("beneden");
|
||||
bottom.Position.Should().Be("bottom");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActive_MaxOnePerPosition_MostRecentlyChangedWinsWithinThePosition()
|
||||
{
|
||||
Switched(
|
||||
Banner("top-old", updated: Now.AddDays(-5), position: "top"),
|
||||
Banner("top-new", updated: Now.AddDays(-1), position: "top"),
|
||||
Banner("bottom-newest", updated: Now, position: "bottom"));
|
||||
|
||||
(await Sut().GetActiveAsync("top"))!.Title.Should().Be("top-new");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActive_ScheduleAppliesPerPosition()
|
||||
{
|
||||
Switched(Banner("top-future", from: Now.AddHours(1), position: "top"), Banner("bottom-now"));
|
||||
|
||||
(await Sut().GetActiveAsync("top")).Should().BeNull();
|
||||
(await Sut().GetActiveAsync("bottom"))!.Title.Should().Be("bottom-now");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_StoresThePosition_AndAllowsAnEmptyTitleOnTop()
|
||||
{
|
||||
OfferingBanner? saved = null;
|
||||
await _repo.AddAsync(Arg.Do<OfferingBanner>(b => saved = b));
|
||||
|
||||
var dto = await Sut().CreateAsync(new OfferingBannerRequest("", "Korte tekst", Position: "top"), Guid.NewGuid());
|
||||
|
||||
saved!.Position.Should().Be("top");
|
||||
saved.Title.Should().BeEmpty();
|
||||
dto.Position.Should().Be("top");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Position_DefaultsToBottom_AndOnlyTopAndBottomAreValid()
|
||||
{
|
||||
new OfferingBannerRequest("T", "X").Position.Should().Be("bottom");
|
||||
Validate(new OfferingBannerRequest("T", "X", Position: "top")).Should().BeEmpty();
|
||||
Validate(new OfferingBannerRequest("T", "X", Position: "left")).Should().HaveCount(1);
|
||||
Validate(new OfferingBannerRequest("T", "X", Position: "")).Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Top_TextIsLimitedTo120_TitleMayBeEmpty()
|
||||
{
|
||||
Validate(new OfferingBannerRequest("", new string('a', 120), Position: "top")).Should().BeEmpty();
|
||||
Validate(new OfferingBannerRequest("", new string('a', 121), Position: "top")).Should().HaveCount(1);
|
||||
Validate(new OfferingBannerRequest("T", new string('a', 300), Position: "bottom")).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Bottom_NeedsATitle(string title)
|
||||
=> Validate(new OfferingBannerRequest(title, "X", Position: "bottom")).Should().HaveCount(1);
|
||||
|
||||
[Fact]
|
||||
public void Text_IsRequired()
|
||||
{
|
||||
var parameter = typeof(OfferingBannerRequest).GetConstructors()[0].GetParameters().Single(p => p.Name == "Text");
|
||||
var required = (RequiredAttribute)parameter.GetCustomAttributes(typeof(RequiredAttribute), false).Single();
|
||||
|
||||
required.IsValid("").Should().BeFalse();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Generated
+189
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user