Aanbiedingen: planning en aan/uit per pakket, admin-lijst /offerings/all, Annuleren op het pakketformulier
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,10 @@ public class OfferingsControllerTests
|
||||
private static OfferingDto Dto(int displayOrder = 0) => new(
|
||||
Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false, displayOrder);
|
||||
|
||||
private static OfferingAdminDto AdminDto(int displayOrder = 0) => new(
|
||||
Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false, displayOrder,
|
||||
null, null, true, null, null);
|
||||
|
||||
private static CreateOfferingRequest CreateRequest() => new(
|
||||
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact");
|
||||
|
||||
@@ -50,7 +54,7 @@ public class OfferingsControllerTests
|
||||
[Fact]
|
||||
public async Task Create_Returns201Created()
|
||||
{
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(Dto());
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
||||
|
||||
var result = await CreateSut().Create(CreateRequest());
|
||||
|
||||
@@ -73,7 +77,7 @@ public class OfferingsControllerTests
|
||||
[Fact]
|
||||
public async Task Update_Returns200_WhenFound()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>()).Returns(Dto());
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
||||
|
||||
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
||||
|
||||
@@ -84,7 +88,7 @@ public class OfferingsControllerTests
|
||||
public async Task Update_Returns404_WhenNotFound()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.Returns((OfferingDto?)null);
|
||||
.Returns((OfferingAdminDto?)null);
|
||||
|
||||
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
||||
|
||||
|
||||
@@ -46,8 +46,9 @@ public class OfferingsRoutesTests
|
||||
foreach (var (action, http) in Actions())
|
||||
{
|
||||
var anonymous = action.GetCustomAttribute<AllowAnonymousAttribute>() is not null;
|
||||
// "GET all" is the administrator list (switched-off/planned/expired included): never anonymous.
|
||||
anonymous.Should().Be(
|
||||
http.HttpMethods.Single() == "GET",
|
||||
http.HttpMethods.Single() == "GET" && http.Template != "all",
|
||||
$"{http.HttpMethods.Single()} {http.Template} must be anonymous only when it is a GET");
|
||||
}
|
||||
}
|
||||
@@ -71,6 +72,7 @@ public class OfferingsRoutesTests
|
||||
.Should().BeEquivalentTo(
|
||||
[
|
||||
"GET",
|
||||
"GET all",
|
||||
"POST",
|
||||
"PUT {id:guid}",
|
||||
"DELETE {id:guid}",
|
||||
@@ -81,7 +83,7 @@ public class OfferingsRoutesTests
|
||||
[Fact]
|
||||
public async Task Create_Returns201_WithLocationOfTheNewResource()
|
||||
{
|
||||
var dto = new OfferingDto(Guid.NewGuid().ToString(), "t", "T", "D", "p", "n", ["f"], "c", false, 0);
|
||||
var dto = new OfferingAdminDto(Guid.NewGuid().ToString(), "t", "T", "D", "p", "n", ["f"], "c", false, 0, null, null, true, null, null);
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(dto);
|
||||
|
||||
var result = await CreateSut().Create(new CreateOfferingRequest("T", "D", "p", "n", ["f"], "c"));
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
||||
|
||||
public class OfferingsScheduleTests
|
||||
{
|
||||
private sealed class FakeTimeProvider(DateTimeOffset now) : TimeProvider
|
||||
{
|
||||
public override DateTimeOffset GetUtcNow() => now;
|
||||
}
|
||||
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private readonly IOfferingRepository _repo = Substitute.For<IOfferingRepository>();
|
||||
|
||||
private OfferingsService Sut() => new(
|
||||
_repo, Substitute.For<IOfferingCategoryRepository>(), Substitute.For<ILogger<OfferingsService>>(),
|
||||
null, new FakeTimeProvider(Now));
|
||||
|
||||
private static Offering Offer(string title, bool active = true, DateTimeOffset? starts = null, DateTimeOffset? ends = null) => new()
|
||||
{
|
||||
Id = Guid.NewGuid(), Slug = title.ToLowerInvariant(), Title = title, Description = "d", Price = "p", PriceNote = "n",
|
||||
Features = ["f"], CtaLabel = "c", IsActive = active, StartsAt = starts, EndsAt = ends,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task Public_list_shows_unscheduled_active_offering_so_existing_packages_stay_visible()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([Offer("Plain")]);
|
||||
|
||||
(await Sut().GetOfferingsAsync()).Select(o => o.Title).Should().Equal("Plain");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Public_list_hides_switched_off_planned_and_expired_but_keeps_order()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([
|
||||
Offer("A"),
|
||||
Offer("Off", active: false),
|
||||
Offer("Planned", starts: Now.AddMinutes(1)),
|
||||
Offer("Expired", ends: Now),
|
||||
Offer("B", starts: Now, ends: Now.AddDays(1)),
|
||||
]);
|
||||
|
||||
(await Sut().GetOfferingsAsync()).Select(o => o.Title).Should().Equal("A", "B");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Public_list_never_exposes_switch_or_schedule()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([Offer("A")]);
|
||||
|
||||
var dto = (await Sut().GetOfferingsAsync()).Single();
|
||||
|
||||
dto.GetType().Should().Be(typeof(OfferingDto));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Admin_list_returns_everything_with_switch_and_schedule()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([Offer("Off", active: false), Offer("Planned", starts: Now.AddDays(1), ends: Now.AddDays(2))]);
|
||||
|
||||
var list = await Sut().ListAllAsync();
|
||||
|
||||
list.Should().HaveCount(2);
|
||||
list[0].IsActive.Should().BeFalse();
|
||||
list[1].StartsAt.Should().Be(Now.AddDays(1));
|
||||
list[1].EndsAt.Should().Be(Now.AddDays(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_without_schedule_fields_defaults_to_active_and_unscheduled()
|
||||
{
|
||||
_repo.GetMaxDisplayOrderAsync().Returns(0);
|
||||
var request = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c");
|
||||
|
||||
var dto = await Sut().CreateAsync(request, Guid.NewGuid());
|
||||
|
||||
dto.IsActive.Should().BeTrue();
|
||||
dto.StartsAt.Should().BeNull();
|
||||
dto.EndsAt.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_and_update_store_switch_and_schedule()
|
||||
{
|
||||
_repo.GetMaxDisplayOrderAsync().Returns(0);
|
||||
var create = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", IsActive: false, StartsAt: Now, EndsAt: Now.AddDays(1));
|
||||
|
||||
var created = await Sut().CreateAsync(create, Guid.NewGuid());
|
||||
|
||||
created.IsActive.Should().BeFalse();
|
||||
await _repo.Received(1).AddAsync(Arg.Is<Offering>(o => !o.IsActive && o.StartsAt == Now && o.EndsAt == Now.AddDays(1)));
|
||||
|
||||
var existing = Offer("E");
|
||||
_repo.GetByIdAsync(existing.Id).Returns(existing);
|
||||
var update = new UpdateOfferingRequest("E", "d", "p", "n", ["f"], "c", IsActive: false, StartsAt: Now, EndsAt: Now.AddHours(2));
|
||||
|
||||
var updated = await Sut().UpdateAsync(existing.Id, update, Guid.NewGuid());
|
||||
|
||||
updated!.IsActive.Should().BeFalse();
|
||||
existing.EndsAt.Should().Be(Now.AddHours(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void End_must_be_after_start_for_create_and_update()
|
||||
{
|
||||
var bad = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now);
|
||||
var badUpdate = new UpdateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now.AddSeconds(-1));
|
||||
var ok = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now.AddSeconds(1));
|
||||
var ctx = new ValidationContext(bad);
|
||||
|
||||
bad.Validate(ctx).Should().ContainSingle();
|
||||
badUpdate.Validate(ctx).Should().ContainSingle();
|
||||
ok.Validate(ctx).Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,10 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
|
||||
return Ok(offerings);
|
||||
}
|
||||
|
||||
/// <summary>Administrator list including switched-off, planned and expired offerings.</summary>
|
||||
[HttpGet("all")]
|
||||
public async Task<IActionResult> ListAll() => Ok(await service.ListAllAsync());
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateOfferingRequest request)
|
||||
{
|
||||
|
||||
@@ -14,6 +14,12 @@ public class Offering
|
||||
public Guid? CategoryId { get; set; }
|
||||
public OfferingCategory? Category { get; set; }
|
||||
public int DisplayOrder { get; set; }
|
||||
/// <summary>Switched on. A switched-off offering is never shown publicly (it stays in the admin list).</summary>
|
||||
public bool IsActive { get; set; } = true;
|
||||
/// <summary>First moment it is shown (inclusive); null = no start limit.</summary>
|
||||
public DateTimeOffset? StartsAt { get; set; }
|
||||
/// <summary>First moment it is no longer shown (exclusive); null = no end limit.</summary>
|
||||
public DateTimeOffset? EndsAt { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
|
||||
@@ -26,6 +26,7 @@ public class OfferingsDbContext(DbContextOptions<OfferingsDbContext> options) :
|
||||
entity.Property(e => e.PriceNote).IsRequired().HasMaxLength(100);
|
||||
entity.Property(e => e.CtaLabel).IsRequired().HasMaxLength(50);
|
||||
entity.Property(e => e.DisplayOrder).IsRequired();
|
||||
entity.Property(e => e.IsActive).IsRequired().HasDefaultValue(true);
|
||||
entity.Property(e => e.IsDeleted).IsRequired();
|
||||
entity.Property(e => e.CreatedAt).IsRequired();
|
||||
entity.Property(e => e.UpdatedAt).IsRequired();
|
||||
|
||||
Generated
+200
@@ -0,0 +1,200 @@
|
||||
// <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("20261115100000_AddOfferingSchedule")]
|
||||
partial class AddOfferingSchedule
|
||||
{
|
||||
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<DateTimeOffset?>("EndsAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<bool>("Featured")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("Features")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("tinyint(1)")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
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<DateTimeOffset?>("StartsAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
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,43 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddOfferingSchedule : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
// Existing offerings stay switched on without a schedule: nothing changes for the websites.
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "IsActive",
|
||||
table: "OfferingsOfferings",
|
||||
type: "tinyint(1)",
|
||||
nullable: false,
|
||||
defaultValue: true);
|
||||
|
||||
migrationBuilder.AddColumn<DateTimeOffset>(
|
||||
name: "StartsAt",
|
||||
table: "OfferingsOfferings",
|
||||
type: "datetime",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<DateTimeOffset>(
|
||||
name: "EndsAt",
|
||||
table: "OfferingsOfferings",
|
||||
type: "datetime",
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(name: "EndsAt", table: "OfferingsOfferings");
|
||||
migrationBuilder.DropColumn(name: "StartsAt", table: "OfferingsOfferings");
|
||||
migrationBuilder.DropColumn(name: "IsActive", table: "OfferingsOfferings");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
@@ -101,6 +101,9 @@ namespace SlpModularCms.Modules.Offerings.Migrations
|
||||
b.Property<int>("DisplayOrder")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("EndsAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<bool>("Featured")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
@@ -108,6 +111,11 @@ namespace SlpModularCms.Modules.Offerings.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("tinyint(1)")
|
||||
.HasDefaultValue(true);
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
@@ -129,6 +137,9 @@ namespace SlpModularCms.Modules.Offerings.Migrations
|
||||
.HasMaxLength(130)
|
||||
.HasColumnType("varchar(130)");
|
||||
|
||||
b.Property<DateTimeOffset?>("StartsAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
|
||||
@@ -11,5 +11,12 @@ public record CreateOfferingRequest(
|
||||
[Required(AllowEmptyStrings = true), MaxLength(50)] string CtaLabel,
|
||||
bool Featured = false,
|
||||
[MaxLength(130)] string? Slug = null,
|
||||
Guid? CategoryId = null
|
||||
);
|
||||
Guid? CategoryId = null,
|
||||
bool IsActive = true,
|
||||
DateTimeOffset? StartsAt = null,
|
||||
DateTimeOffset? EndsAt = null
|
||||
) : IValidatableObject
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt);
|
||||
}
|
||||
|
||||
@@ -21,3 +21,26 @@ public record OfferingDto(
|
||||
string? CategoryId = null,
|
||||
string? Category = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Administrator view: the public representation plus the switch and the schedule. Never returned by the
|
||||
/// public endpoint, whose JSON shape stays as websites expect it.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public record OfferingAdminDto(
|
||||
string Id,
|
||||
string Slug,
|
||||
string Title,
|
||||
string Description,
|
||||
string Price,
|
||||
string PriceNote,
|
||||
IReadOnlyList<string> Features,
|
||||
string CtaLabel,
|
||||
bool Featured,
|
||||
int DisplayOrder,
|
||||
string? CategoryId,
|
||||
string? Category,
|
||||
bool IsActive,
|
||||
DateTimeOffset? StartsAt,
|
||||
DateTimeOffset? EndsAt
|
||||
) : OfferingDto(Id, Slug, Title, Description, Price, PriceNote, Features, CtaLabel, Featured, DisplayOrder, CategoryId, Category);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Models;
|
||||
|
||||
/// <summary>The one rule for "is this offering shown right now", shared by the public list and the SEO injection.</summary>
|
||||
public static class OfferingSchedule
|
||||
{
|
||||
public static IEnumerable<ValidationResult> Validate(DateTimeOffset? startsAt, DateTimeOffset? endsAt)
|
||||
{
|
||||
if (startsAt is not null && endsAt is not null && endsAt <= startsAt)
|
||||
yield return new ValidationResult("EndsAt must be after StartsAt.", [nameof(OfferingBanner.EndsAt)]);
|
||||
}
|
||||
|
||||
/// <summary>Switched on, started (inclusive) and not yet ended (exclusive).</summary>
|
||||
public static bool IsLive(Offering o, DateTimeOffset now)
|
||||
=> o.IsActive && (o.StartsAt is null || o.StartsAt <= now) && (o.EndsAt is null || now < o.EndsAt);
|
||||
}
|
||||
@@ -11,5 +11,12 @@ public record UpdateOfferingRequest(
|
||||
[Required(AllowEmptyStrings = true), MaxLength(50)] string CtaLabel,
|
||||
bool Featured = false,
|
||||
[MaxLength(130)] string? Slug = null,
|
||||
Guid? CategoryId = null
|
||||
);
|
||||
Guid? CategoryId = null,
|
||||
bool IsActive = true,
|
||||
DateTimeOffset? StartsAt = null,
|
||||
DateTimeOffset? EndsAt = null
|
||||
) : IValidatableObject
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,14 @@ namespace SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
public interface IOfferingsService
|
||||
{
|
||||
/// <summary>What visitors get: only offerings switched on and inside their schedule right now.</summary>
|
||||
Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync();
|
||||
Task<OfferingDto> CreateAsync(CreateOfferingRequest request, Guid callerId);
|
||||
Task<OfferingDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId);
|
||||
|
||||
/// <summary>What administrators get: every offering with its switch and schedule.</summary>
|
||||
Task<IReadOnlyList<OfferingAdminDto>> ListAllAsync();
|
||||
|
||||
Task<OfferingAdminDto> CreateAsync(CreateOfferingRequest request, Guid callerId);
|
||||
Task<OfferingAdminDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId);
|
||||
Task<bool> DeleteAsync(Guid id, Guid callerId);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -10,15 +10,24 @@ public class OfferingsService(
|
||||
IOfferingRepository repository,
|
||||
IOfferingCategoryRepository categories,
|
||||
ILogger<OfferingsService> logger,
|
||||
IIndexHtmlCache? indexCache = null) : IOfferingsService
|
||||
IIndexHtmlCache? indexCache = null,
|
||||
TimeProvider? time = null) : IOfferingsService
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
|
||||
/// <summary>Public list: only offerings that are switched on and inside their schedule right now.</summary>
|
||||
public async Task<IReadOnlyList<OfferingDto>> GetOfferingsAsync()
|
||||
{
|
||||
var now = _time.GetUtcNow();
|
||||
var offerings = await repository.GetAllAsync();
|
||||
return offerings.Select(ToDto).ToList();
|
||||
return offerings.Where(o => OfferingSchedule.IsLive(o, now)).Select(ToDto).ToList();
|
||||
}
|
||||
|
||||
public async Task<OfferingDto> CreateAsync(CreateOfferingRequest request, Guid callerId)
|
||||
/// <summary>Administrator list: everything, including switched-off, planned and expired offerings.</summary>
|
||||
public async Task<IReadOnlyList<OfferingAdminDto>> ListAllAsync()
|
||||
=> (await repository.GetAllAsync()).Select(ToAdminDto).ToList();
|
||||
|
||||
public async Task<OfferingAdminDto> CreateAsync(CreateOfferingRequest request, Guid callerId)
|
||||
{
|
||||
var category = await ResolveCategoryAsync(request.CategoryId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
@@ -35,6 +44,9 @@ public class OfferingsService(
|
||||
CategoryId = category?.Id,
|
||||
Category = category,
|
||||
Featured = request.Featured,
|
||||
IsActive = request.IsActive,
|
||||
StartsAt = request.StartsAt,
|
||||
EndsAt = request.EndsAt,
|
||||
DisplayOrder = await repository.GetMaxDisplayOrderAsync() + 1,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
@@ -64,10 +76,10 @@ public class OfferingsService(
|
||||
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
|
||||
offering.Id, "Created", callerId);
|
||||
|
||||
return ToDto(offering);
|
||||
return ToAdminDto(offering);
|
||||
}
|
||||
|
||||
public async Task<OfferingDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId)
|
||||
public async Task<OfferingAdminDto?> UpdateAsync(Guid id, UpdateOfferingRequest request, Guid callerId)
|
||||
{
|
||||
var offering = await repository.GetByIdAsync(id);
|
||||
if (offering is null)
|
||||
@@ -93,6 +105,9 @@ public class OfferingsService(
|
||||
offering.CategoryId = category?.Id;
|
||||
offering.Category = category;
|
||||
offering.Featured = request.Featured;
|
||||
offering.IsActive = request.IsActive;
|
||||
offering.StartsAt = request.StartsAt;
|
||||
offering.EndsAt = request.EndsAt;
|
||||
offering.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
offering.LastModifiedByUserId = callerId;
|
||||
|
||||
@@ -115,7 +130,7 @@ public class OfferingsService(
|
||||
"Offering {OfferingId} {Action} by user {LastModifiedByUserId}",
|
||||
offering.Id, "Updated", callerId);
|
||||
|
||||
return ToDto(offering);
|
||||
return ToAdminDto(offering);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id, Guid callerId)
|
||||
@@ -245,4 +260,22 @@ public class OfferingsService(
|
||||
o.CategoryId?.ToString(),
|
||||
o.Category?.Name
|
||||
);
|
||||
|
||||
private static OfferingAdminDto ToAdminDto(Offering o) => new(
|
||||
o.Id.ToString(),
|
||||
o.Slug,
|
||||
o.Title,
|
||||
o.Description,
|
||||
o.Price,
|
||||
o.PriceNote,
|
||||
o.Features,
|
||||
o.CtaLabel,
|
||||
o.Featured,
|
||||
o.DisplayOrder,
|
||||
o.CategoryId?.ToString(),
|
||||
o.Category?.Name,
|
||||
o.IsActive,
|
||||
o.StartsAt,
|
||||
o.EndsAt
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user