Writing to the logbook

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

Topics covered: Query builderInsertType conversion

The logbook can be read; now it needs to be written to: an order taken in the app must go into the table, not just into memory.

Your mission

  1. In Store, after the validation, insert the order into the livraisons table with facades.DB().
  2. Respond 201 with the delivery, and the number the database gave it.
  3. An invalid order still responds 422, and writes nothing.
  4. The in-memory round, the livraisons slice, has no reason to exist anymore: remove it.

Reminder

InsertGetID(data) inserts a row and returns the number SQLite assigned to it, 6 here, since the logbook already holds 5. The data is a map[string]any, from columns to their values. For several rows at once, Insert takes a slice of maps.

Go never converts one numeric type into another unless you ask: an int64 doesn't fit into an int. int(id) does the conversion. It's one of the rules that surprise you coming from PHP, and that save you from many surprises later on.