diff options
| author | Fuwn <[email protected]> | 2024-05-30 17:03:28 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-30 17:03:28 -0700 |
| commit | 5f1f7b43c172c0cc458c60e055f18be821663dff (patch) | |
| tree | 06134d7b9736aed16064e571f9b64ef923057716 /src | |
| parent | 9ee076493b06fc95c8539f4aff20a2ebfb710de9 (diff) | |
| download | mayu-5f1f7b43c172c0cc458c60e055f18be821663dff.tar.xz mayu-5f1f7b43c172c0cc458c60e055f18be821663dff.zip | |
refactor(request): straightforward pattern matching
Diffstat (limited to 'src')
| -rw-r--r-- | src/database.gleam | 8 | ||||
| -rw-r--r-- | src/request.gleam | 55 |
2 files changed, 34 insertions, 29 deletions
diff --git a/src/database.gleam b/src/database.gleam index 875231d..c893b90 100644 --- a/src/database.gleam +++ b/src/database.gleam @@ -67,7 +67,7 @@ fn sqlite_now() { pub fn get_counter(connection, name) { case name { - "demo" -> Counter("demo", 0_123_456_789, "", "") + "demo" -> Ok(Counter("demo", 0_123_456_789, "", "")) _ -> { check_error( sqlight.query( @@ -102,14 +102,14 @@ pub fn get_counter(connection, name) { ) { Ok([first_element]) -> { - Counter( + Ok(Counter( first_element.0, first_element.1, first_element.2, first_element.3, - ) + )) } - _ -> Counter(name, 0, "", "") + _ -> Error("Unreachable entity") } } } 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() } } |