@@ -2,7 +2,7 @@ import app/web.{type Context}
22import gleam/dynamic . { type Dynamic }
33import gleam/http . { Get , Post }
44import gleam/json
5- import gleam/map
5+ import gleam/dict
66import gleam/result . { try }
77import tiny_database
88import wisp . { type Request , type Response }
@@ -41,12 +41,16 @@ pub fn list_people(ctx: Context) -> Response {
4141 use ids <- try ( tiny_database . list ( ctx . db ) )
4242
4343 // Convert the ids into a JSON array of objects.
44- Ok ( json . to_string_builder ( json . object ( [
45- # (
46- "people" ,
47- json . array ( ids , fn ( id ) { json . object ( [ # ( "id" , json . string ( id ) ) ] ) } ) ,
44+ Ok (
45+ json . to_string_builder (
46+ json . object ( [
47+ # (
48+ "people" ,
49+ json . array ( ids , fn ( id ) { json . object ( [ # ( "id" , json . string ( id ) ) ] ) } ) ,
50+ ) ,
51+ ] ) ,
4852 ) ,
49- ] ) ) )
53+ )
5054 }
5155
5256 case result {
@@ -88,11 +92,15 @@ pub fn read_person(ctx: Context, id: String) -> Response {
8892 use person <- try ( read_from_database ( ctx . db , id ) )
8993
9094 // Construct a JSON payload with the person's details.
91- Ok ( json . to_string_builder ( json . object ( [
92- # ( "id" , json . string ( id ) ) ,
93- # ( "name" , json . string ( person . name ) ) ,
94- # ( "favourite-colour" , json . string ( person . favourite_colour ) ) ,
95- ] ) ) )
95+ Ok (
96+ json . to_string_builder (
97+ json . object ( [
98+ # ( "id" , json . string ( id ) ) ,
99+ # ( "name" , json . string ( person . name ) ) ,
100+ # ( "favourite-colour" , json . string ( person . favourite_colour ) ) ,
101+ ] ) ,
102+ ) ,
103+ )
96104 }
97105
98106 // Return an appropriate response.
@@ -123,9 +131,9 @@ pub fn save_to_database(
123131 person : Person ,
124132) -> Result ( String , Nil ) {
125133 // In a real application you might use a database client with some SQL here.
126- // Instead we create a simple map and save that.
134+ // Instead we create a simple dict and save that.
127135 let data =
128- map . from_list ( [
136+ dict . from_list ( [
129137 # ( "name" , person . name ) ,
130138 # ( "favourite-colour" , person . favourite_colour ) ,
131139 ] )
@@ -138,7 +146,7 @@ pub fn read_from_database(
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 ) )
141- use name <- try ( map . get ( data , "name" ) )
142- use favourite_colour <- try ( map . get ( data , "favourite-colour" ) )
149+ use name <- try ( dict . get ( data , "name" ) )
150+ use favourite_colour <- try ( dict . get ( data , "favourite-colour" ) )
143151 Ok ( Person ( name , favourite_colour ) )
144152}
0 commit comments