One delivery at a time

Exercise 2 of 4 in chapter 2 · 100 XP · 2 objectives checked by tests

Topics covered: Route parameterHTTP status code404

In the app, a courier taps a delivery to see its details. Each delivery needs its own URL.

Your mission

  1. Add a Show method to the controller, wired to GET /deliveries/{id}.
  2. It looks through the round for the delivery whose ID is the one in the URL, and sends it back as JSON.
  3. An id that doesn't exist, or that isn't a number, responds 404 with {"erreur": "delivery not found"} (erreur is French for “error”).

Reminder

ctx.Request().RouteInt("id") reads a route parameter and converts it to an int. Goravel also has RouteInt64, and Route for the raw string.

ctx.Response().Json(code, data) picks the HTTP status code. Goravel reuses the constants of Go's net/http package: http.StatusNotFound is 404, http.StatusCreated 201, http.StatusUnprocessableEntity 422. They read better than bare numbers.

In Go, return leaves the function right away, in the middle of a loop as anywhere else: the code after the loop only runs if nothing was found.