Booking by pigeon

Exercise 3 of 6 in chapter 5 · 240 XP · 5 objectives checked by tests

Topics covered: POST JSONDTO#[MapRequestPayload]201 / 400 / 422

Big news: scrolls can now send messages. No need to cross the forest to book a table any more: a magic pigeon takes care of it. It brings a JSON like this one:

{ "nom": "Gimli", "personnes": 3, "jour": "2026-09-18" }

(nom = name, personnes = number of adventurers, jour = day.)

Your mission

  1. Complete the ReservationDemande DTO (reservation request), which describes and validates a request:
    • nom: required (“Enter your name.”);
    • personnes: from 1 to 12 (“The number of adventurers must be between 1 and 12.”);
    • jour: required (“Enter the day of your visit.”), and not in the past (“You cannot book in the past, not even with a magic scroll.”). Today is allowed.
  2. In POST /api/reservations, receive the request with #[MapRequestPayload], save a Reservation, then respond 201 Created with the reservation (reservation group).

Try the three ready-made requests in the Requests tab.

What #[MapRequestPayload] does

Symfony reads the JSON body, fills the DTO, then validates it. Your controller only receives valid requests. Otherwise, Symfony responds for you:

Problem Response
Unreadable JSON 400 Bad Request
Rule broken, missing field, wrong type ("personnes": "three") 422 Unprocessable Content, with the list of violations

Why a DTO, and not the entity?

The request is not the reservation. The DTO only contains what the client is allowed to send: no way to slip in an id or a creation date. And the API's validation doesn't depend on the database.