Book a drop-off slot at the bike workshop

C# · ASP.NET Core · intermediate · modification

Customers can now book a drop-off for a bike repair straight from the slot list instead of phoning the workshop. `SlotsController` gets `POST /api/slots/{slotId}/bookings`: it validates the bike description, answers 404 for an unknown slot and 409 for a slot that has started or has no place left, and otherwise stores the booking for the signed-in customer and answers 201 with it. The request and response records sit next to `SlotDto` in `SlotDtos.cs`. `SlotBookingTests.cs` adds endpoint tests on the existing `WorkshopApiFactory`.

ASP.NET Core 10 Web API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), controllers with `[ApiController]` (so a body that is missing or has no `bike` string answers 400 before the action runs). EF Core 10 with Npgsql on Postgres 16 at the default READ COMMITTED isolation; no retrying execution strategy is configured. `WorkshopDbContext` is unchanged: `Slots` holds `Slot` (`Id` int identity, `StartsAt` `DateTimeOffset` stored as `timestamptz`, `Capacity` int from 1 to 20, `Bookings`) and `Bookings` holds `Booking` (`Id`, `SlotId`, `CustomerId` string, `Bike` text, `CreatedAt`), in tables `"Slots"` and `"Bookings"` with EF's default names. Both tables come from an earlier migration of this feature. Staff create slots in the staff tool, and a slot's capacity does not change once it is published; staff can delete a booking there, and this endpoint is the only code that inserts bookings. The API runs as two instances behind a load balancer. The workshop publishes next week's slots every Monday at 08:00, and the Saturday morning ones are usually booked within minutes. Authentication is unchanged in `Program.cs`: cookie sign-in shared with the customer site, and every customer has a `customer_id` claim read by `User.GetCustomerId()` (`Workshop.Api.Auth`, existing). `Program.cs` also registers `TimeProvider.System`. JSON uses the ASP.NET Core web defaults (camelCase names). Tests use xUnit and the existing `WorkshopApiFactory` (`WebApplicationFactory<Program>` on a throwaway Postgres 16 database per test class): `CreateClientFor(customerId)` signs requests in as that customer, `SeedSlotAsync(startsAt, capacity, existingBookings)` inserts a slot with that many bookings, and `CountBookingsAsync(slotId)` counts a slot's bookings. The booking page ships separately in the web app.

Requirements

Files touched

Review this PR

C# practice