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
- New endpoint `POST /api/slots/{slotId}/bookings` with the body `{ "bike": "..." }` books a drop-off in that slot for the signed-in customer and answers 201 with `{ "id", "slotId", "startsAt", "bike" }`. `bike` (the bike and what is wrong with it) is stored trimmed; a `bike` that is missing, blank, longer than 200 UTF-16 code units (`string.Length`) after trimming, or contains a control character (Unicode category Cc, `char.IsControl`) answers 400 and books nothing.
- A slot takes at most `Capacity` bookings, the number of bikes the workshop can check in at that time. A booking that would go over it answers 409 `{ "error": "slot full" }`, and a slot never holds more bookings than its capacity.
- A slot that has already started (`StartsAt` at or before the current time, read from the injected `TimeProvider`) answers 409 `{ "error": "slot closed" }`. An unknown slot id answers 404.
- A customer may book several drop-offs, one per bike, in the same slot or in different ones.
- Unchanged: the controller requires a signed-in customer (`[Authorize]`, 401 otherwise), and `GET /api/slots` keeps listing the upcoming slots (at most 200, soonest first) with `remaining` = capacity minus bookings.
Files touched
- src/Workshop.Api/Slots/SlotsController.cs
- src/Workshop.Api/Slots/SlotDtos.cs
- tests/Workshop.Api.Tests/SlotBookingTests.cs