···22import gleam/dynamic.{type Dynamic}
33import gleam/http.{Get, Post}
44import gleam/json
55-import gleam/map
55+import gleam/dict
66import gleam/result.{try}
77import tiny_database
88import wisp.{type Request, type Response}
···4141 use ids <- try(tiny_database.list(ctx.db))
42424343 // Convert the ids into a JSON array of objects.
4444- Ok(json.to_string_builder(json.object([
4545- #(
4646- "people",
4747- json.array(ids, fn(id) { json.object([#("id", json.string(id))]) }),
4444+ Ok(
4545+ json.to_string_builder(
4646+ json.object([
4747+ #(
4848+ "people",
4949+ json.array(ids, fn(id) { json.object([#("id", json.string(id))]) }),
5050+ ),
5151+ ]),
4852 ),
4949- ])))
5353+ )
5054 }
51555256 case result {
···8892 use person <- try(read_from_database(ctx.db, id))
89939094 // Construct a JSON payload with the person's details.
9191- Ok(json.to_string_builder(json.object([
9292- #("id", json.string(id)),
9393- #("name", json.string(person.name)),
9494- #("favourite-colour", json.string(person.favourite_colour)),
9595- ])))
9595+ Ok(
9696+ json.to_string_builder(
9797+ json.object([
9898+ #("id", json.string(id)),
9999+ #("name", json.string(person.name)),
100100+ #("favourite-colour", json.string(person.favourite_colour)),
101101+ ]),
102102+ ),
103103+ )
96104 }
9710598106 // Return an appropriate response.
···123131 person: Person,
124132) -> Result(String, Nil) {
125133 // In a real application you might use a database client with some SQL here.
126126- // Instead we create a simple map and save that.
134134+ // Instead we create a simple dict and save that.
127135 let data =
128128- map.from_list([
136136+ dict.from_list([
129137 #("name", person.name),
130138 #("favourite-colour", person.favourite_colour),
131139 ])
···138146) -> Result(Person, Nil) {
139147 // In a real application you might use a database client with some SQL here.
140148 use data <- try(tiny_database.read(db, id))
141141- use name <- try(map.get(data, "name"))
142142- use favourite_colour <- try(map.get(data, "favourite-colour"))
149149+ use name <- try(dict.get(data, "name"))
150150+ use favourite_colour <- try(dict.get(data, "favourite-colour"))
143151 Ok(Person(name, favourite_colour))
144152}
+2-2
examples/5-using-a-database/test/app_test.gleam
···1212 gleeunit.main()
1313}
14141515-fn with_context(test: fn(Context) -> t) -> t {
1515+fn with_context(testcase: fn(Context) -> t) -> t {
1616 // Create a new database connection for this test
1717 use db <- tiny_database.with_connection(app.data_directory)
1818···2121 let context = Context(db: db)
22222323 // Run the test with the context
2424- test(context)
2424+ testcase(context)
2525}
26262727pub fn get_unknown_test() {
···99 gleeunit.main()
1010}
11111212-fn with_context(test: fn(Context) -> t) -> t {
1212+fn with_context(testcase: fn(Context) -> t) -> t {
1313 // Create the context to use in tests
1414 let context = Context(static_directory: app.static_directory())
15151616 // Run the test with the context
1717- test(context)
1717+ testcase(context)
1818}
19192020pub fn get_home_page_test() {