Skip to content

gleam: implement request handlers and router #10

@HeyItWorked

Description

@HeyItWorked

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 parsingint.parse(id_string) returns Result(Int, Nil)

Acceptance criteria

  • All 5 handlers implemented
  • Router pattern-matches all methods + fallback to 404
  • JSON decode validates title + author required, status defaults to "want to read"
  • Invalid status returns 400
  • gleam build passes

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgleamGleam implementation

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions