Task
Create gleam-bookshelf/src/handlers.gleam and gleam-bookshelf/src/router.gleam.
Router pattern
Gleam uses pattern matching on method + path segments instead of registering routes:
```gleam
case req.method, wisp.path_segments(req) {
Post, ["books"] -> create_book(req, ctx)
Get, ["books"] -> get_books(ctx)
Get, ["books", id] -> get_book(id, ctx)
Put, ["books", id] -> update_book(req, id, ctx)
Delete, ["books", id] -> delete_book(id, ctx)
_, _ -> wisp.not_found()
}
```
Handlers to implement
| Handler |
Input |
DB call |
Response |
create_book |
JSON body |
insert_book |
201 + book JSON |
get_books |
nothing |
get_all_books |
200 + list JSON |
get_book |
id from path |
get_book_by_id |
200 + book JSON |
update_book |
id + JSON body |
update_book |
200 + book JSON |
delete_book |
id from path |
delete_book |
204 no content |
Key differences from Go/TS
- JSON decoding requires building a decoder (like Go's Scan, but for JSON)
- JSON encoding requires an explicit
encode_book(book) -> Json function
- No early returns — use
case expressions or use for control flow
- Path param parsing —
int.parse(id_string) returns Result(Int, Nil)
Acceptance criteria
Task
Create
gleam-bookshelf/src/handlers.gleamandgleam-bookshelf/src/router.gleam.Router pattern
Gleam uses pattern matching on method + path segments instead of registering routes:
```gleam
case req.method, wisp.path_segments(req) {
Post, ["books"] -> create_book(req, ctx)
Get, ["books"] -> get_books(ctx)
Get, ["books", id] -> get_book(id, ctx)
Put, ["books", id] -> update_book(req, id, ctx)
Delete, ["books", id] -> delete_book(id, ctx)
_, _ -> wisp.not_found()
}
```
Handlers to implement
create_bookinsert_bookget_booksget_all_booksget_bookget_book_by_idupdate_bookupdate_bookdelete_bookdelete_bookKey differences from Go/TS
encode_book(book) -> Jsonfunctioncaseexpressions orusefor control flowint.parse(id_string)returnsResult(Int, Nil)Acceptance criteria
gleam buildpasses