Early-bird ticket price on the event page and at checkout
C# · ASP.NET Core · intermediate · modification
Adds early-bird pricing to the event page and checkout. `TicketPricing.For` (new, `Events/TicketPricing.cs`) decides which ticket price applies to an event right now and returns it with whether the early-bird offer is on and when it ends. `EventEndpoints.cs` uses it in `GET /api/events/{slug}`, which now also returns `earlyBird` and `earlyBirdEndsAt` for the page's countdown, and in `POST /api/events/{slug}/orders`, so an order's amount uses the same price rule the page shows. `EventDtos.cs` adds the two fields to `EventDto`. `TicketPricingTests.cs` (new) adds xUnit tests for `TicketPricing.For`.
Ticketing API on ASP.NET Core 10 minimal APIs, .NET 10 (C# 14, nullable reference types enabled, implicit usings), EF Core 10 with Npgsql on Postgres 16, xUnit for tests. It runs on two Linux VMs in Frankfurt whose system time zone is set to Europe/Berlin (the ops team's choice, so host logs read in office time). Existing and unchanged: `Program.cs` registers `TicketsDbContext` and JWT bearer authentication and calls `app.MapEventEndpoints()`; `User.GetUserId()` (`Tickets.Api.Auth`) reads the token's `sub` claim. `TicketsDbContext.Events` is the `Event` entity: `Id`, `Slug` (unique), `Name`, `Venue`, `StartsAt` (`timestamptz`), `Published`, `SalesOpen`, `PriceCents` (long, 100 to 1,000,000), `EarlyBirdPriceCents` (long?, 100 to 999,999 and always below `PriceCents`) and `EarlyBirdEndsAt` (`DateTime?`, a `timestamptz` column). The organizer dashboard (shipped earlier, unchanged) sets `EarlyBirdPriceCents` and `EarlyBirdEndsAt` together or clears both; organizers enter the end in the event's local time and the dashboard stores that instant. Npgsql reads `timestamptz` columns as `DateTime` values with `Kind = Utc`, and the API's JSON writes them with a trailing `Z`. `Order` has `EventId`, `UserId`, `Quantity`, `AmountCents`, `Status` and `CreatedAt`; the payment page (separate, unchanged) charges a pending order's `AmountCents`. All prices are euro cents. The code reads the clock directly, as the order endpoint's `CreatedAt = DateTime.UtcNow` does; no `TimeProvider` is registered. The event page in the web app ships separately and only reads this endpoint.
Requirements
- An event can have an early-bird offer (`EarlyBirdPriceCents` and `EarlyBirdEndsAt` both set). The offer runs until the instant `EarlyBirdEndsAt`, exclusive: before that instant a ticket costs `EarlyBirdPriceCents`, from that instant on it costs `PriceCents`. An event without an offer always costs `PriceCents`.
- `GET /api/events/{slug}` returns in `priceCents` the ticket price that applies at the moment of the request, plus `earlyBird` (`true` while the offer runs) and `earlyBirdEndsAt` (the offer's end while it runs, so the event page can show a countdown; `null` otherwise).
- `POST /api/events/{slug}/orders` stores `AmountCents` as the ticket price that applies when the order is created, times `quantity`, and returns it.
- Unchanged: the event endpoint's other fields; the order endpoint's rules (sign-in required, 401 otherwise; `quantity` from 1 to 10, 400 otherwise; 404 for an unknown or unpublished slug; 409 when sales are closed; 201 with the order).
Files touched
- src/Tickets.Api/Events/TicketPricing.cs
- src/Tickets.Api/Events/EventEndpoints.cs
- src/Tickets.Api/Events/EventDtos.cs
- tests/Tickets.Api.Tests/TicketPricingTests.cs