The delivery logbook

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

Topics covered: Query builderSQLitedb tagError handling

The in-memory round is wiped at every restart, and Margot loses her orders. Her nephew opened a logbook: a SQLite table livraisons, with today's round and the Épicerie de la Lune, a grocery store that just signed up. In a real project, the table would keep everything; here, in the browser, database/carnet.go resets it at every start. He also added an erreurServeur (“server error”) function to the controller, which responds 500.

Your mission

  1. In Index, read the deliveries from the livraisons table, sorted by number, with facades.DB().
  2. Keep the ?quartier= filter: it becomes a condition of the query. Without deliveries, the response stays [].
  3. In Show, read the delivery from the table. An unknown number still responds 404.
  4. Store still writes to the in-memory round: that's the next exercise.

Reminder

facades.DB() is Goravel's query builder, Laravel's DB::table(): Table("livraisons").Where("quartier", "confluence").OrderBy("id").Get(&resultat). Get fills a slice, First a single struct. You pass them the address of the variable, &resultat, so that they can write into it.

The db:"client" tag links a column to a field, the way json:"client" does with a JSON key (without a tag, Goravel looks for the column with the same name in snake_case). A struct can carry both.

Every query can fail, and returns an error: if err := query.Get(&resultat); err != nil { … } checks it on the same line. The file's erreurServeur function then responds 500.

In a real project, the table would come from a migration (go run . artisan make:migration) and its data from a seeder; here, without a console, database/carnet.go creates them at startup.