diff options
| author | Fuwn <[email protected]> | 2024-05-31 00:03:28 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-31 00:03:28 +0000 |
| commit | d475dde9db7820c42e6de5c9a75e012f3b2e693a (patch) | |
| tree | 06134d7b9736aed16064e571f9b64ef923057716 /src/request.gleam | |
| parent | refactor(image): use purely bit array syntax (diff) | |
| download | mayu-d475dde9db7820c42e6de5c9a75e012f3b2e693a.tar.xz mayu-d475dde9db7820c42e6de5c9a75e012f3b2e693a.zip | |
refactor(request): straightforward pattern matching
Diffstat (limited to 'src/request.gleam')
| -rw-r--r-- | src/request.gleam | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/src/request.gleam b/src/request.gleam index b6bd969..08dbf8d 100644 --- a/src/request.gleam +++ b/src/request.gleam @@ -20,32 +20,37 @@ pub fn handle(request, connection) { case wisp.path_segments(request) { ["heart-beat"] -> wisp.html_response(string_builder.from_string("alive"), 200) - ["get", "@" <> name] -> - wisp.ok() - |> wisp.set_header("Content-Type", "image/svg+xml") - |> wisp.string_body(svg.xml( - case wisp.get_query(request) { - [#("theme", theme)] -> theme - _ -> "asoul" - }, - database.get_counter(connection, name).num, - )) + ["get", "@" <> name] -> { + case database.get_counter(connection, name) { + Ok(counter) -> { + wisp.ok() + |> wisp.set_header("Content-Type", "image/svg+xml") + |> wisp.string_body(svg.xml( + case wisp.get_query(request) { + [#("theme", theme)] -> theme + _ -> "asoul" + }, + counter.num, + )) + } + Error(_) -> wisp.unprocessable_entity() + } + } ["record", "@" <> name] -> { - let counter = database.get_counter(connection, name) - - case - Ok( - json.to_string_builder( - json.object([ - #("name", json.string(counter.name)), - #("num", json.int(counter.num)), - #("updated_at", json.string(counter.updated_at)), - #("created_at", json.string(counter.created_at)), - ]), - ), - ) - { - Ok(builder) -> wisp.json_response(builder, 200) + case database.get_counter(connection, name) { + Ok(counter) -> { + wisp.json_response( + json.to_string_builder( + json.object([ + #("name", json.string(counter.name)), + #("num", json.int(counter.num)), + #("updated_at", json.string(counter.updated_at)), + #("created_at", json.string(counter.created_at)), + ]), + ), + 200, + ) + } Error(_) -> wisp.unprocessable_entity() } } |