Merge pull request '[ring] Simplify types' (#3) from simplify_types into main

Reviewed-on: #3
This commit is contained in:
Erika Rowland 2024-03-31 19:04:17 +00:00
commit 802c2f99af

View file

@ -24,10 +24,6 @@ pub fn handle_request(req: Request) -> Response {
} }
} }
type Row {
Row(String)
}
fn handle_previous(hash) { fn handle_previous(hash) {
let db_path = "members.db" let db_path = "members.db"
use conn <- sqlight.with_connection(db_path) use conn <- sqlight.with_connection(db_path)
@ -36,10 +32,10 @@ fn handle_previous(hash) {
"select previous from ring where hash = ?", "select previous from ring where hash = ?",
on: conn, on: conn,
with: [sqlight.text(hash)], with: [sqlight.text(hash)],
expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), expecting: dynamic.element(0, dynamic.string),
) )
case previous { case previous {
Ok([Row(previous_link)]) -> wisp.redirect(previous_link) Ok([previous_link]) -> wisp.redirect(previous_link)
_ -> wisp.not_found() _ -> wisp.not_found()
} }
} }
@ -52,10 +48,10 @@ fn handle_next(hash) {
"select next from ring where hash = ?", "select next from ring where hash = ?",
on: conn, on: conn,
with: [sqlight.text(hash)], with: [sqlight.text(hash)],
expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), expecting: dynamic.element(0, dynamic.string),
) )
case next { case next {
Ok([Row(next_link)]) -> wisp.redirect(next_link) Ok([next_link]) -> wisp.redirect(next_link)
_ -> wisp.not_found() _ -> wisp.not_found()
} }
} }
@ -68,10 +64,10 @@ fn handle_random() {
"select next from ring order by random() limit 1", "select next from ring order by random() limit 1",
on: conn, on: conn,
with: [], with: [],
expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), expecting: dynamic.element(0, dynamic.string),
) )
case random { case random {
Ok([Row(random_link)]) -> wisp.redirect(random_link) Ok([random_link]) -> wisp.redirect(random_link)
_ -> wisp.not_found() _ -> wisp.not_found()
} }
} }
@ -87,12 +83,12 @@ fn home_page(req: Request) -> Response {
"select member from ring order by member asc", "select member from ring order by member asc",
on: conn, on: conn,
with: [], with: [],
expecting: dynamic.decode1(Row, dynamic.element(0, dynamic.string)), expecting: dynamic.element(0, dynamic.string),
) )
let members = let members =
result.unwrap(members, []) result.unwrap(members, [])
|> list.map(fn(row) { |> list.map(fn(row) {
let Row(member) = row let member = row
html.li([], [html.a([attribute.href(member)], [html.text(member)])]) html.li([], [html.a([attribute.href(member)], [html.text(member)])])
}) })
let html = let html =