diff options
| author | Fuwn <[email protected]> | 2024-05-14 02:34:55 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-14 02:34:55 -0700 |
| commit | 91186ca65bf2335e16cc05fd331704f3628b8d53 (patch) | |
| tree | d85486a84c73c43f973aae210c623d7848934c53 /src | |
| parent | f3f73ed3b35a90959af1d545921ac31227920d0c (diff) | |
| download | mayu-91186ca65bf2335e16cc05fd331704f3628b8d53.tar.xz mayu-91186ca65bf2335e16cc05fd331704f3628b8d53.zip | |
feat(database): demo counterv0.1.2
Diffstat (limited to 'src')
| -rw-r--r-- | src/database.gleam | 78 |
1 files changed, 43 insertions, 35 deletions
diff --git a/src/database.gleam b/src/database.gleam index 8e8d320..34400ce 100644 --- a/src/database.gleam +++ b/src/database.gleam @@ -49,42 +49,50 @@ pub fn add_counter(connection, name) { } pub fn get_counter(connection, name) { - let assert Ok(_) = - sqlight.query( - "insert or ignore into tb_count (name) values (?);", - with: [sqlight.text(name)], - on: connection, - expecting: dynamic.optional(dynamic.int), - ) - let assert Ok(_) = - sqlight.query( - "update tb_count set num = num + 1, updated_at = ? where name = ?;", - with: [sqlight.text(birl.to_iso8601(birl.utc_now())), sqlight.text(name)], - on: connection, - expecting: dynamic.int, - ) + case name { + "demo" -> Counter("demo", 0_123_456_789, "", "") + _ -> { + let assert Ok(_) = + sqlight.query( + "insert or ignore into tb_count (name) values (?);", + with: [sqlight.text(name)], + on: connection, + expecting: dynamic.optional(dynamic.int), + ) + let assert Ok(_) = + sqlight.query( + "update tb_count set num = num + 1, updated_at = ? where name = ?;", + with: [ + sqlight.text(birl.to_iso8601(birl.utc_now())), + sqlight.text(name), + ], + on: connection, + expecting: dynamic.int, + ) - case - sqlight.query( - "select name, num, created_at, updated_at from tb_count where name = ?;", - with: [sqlight.text(name)], - on: connection, - expecting: dynamic.tuple4( - dynamic.string, - dynamic.int, - dynamic.string, - dynamic.string, - ), - ) - { - Ok([first_element]) -> { - Counter( - first_element.0, - first_element.1, - first_element.2, - first_element.3, - ) + case + sqlight.query( + "select name, num, created_at, updated_at from tb_count where name = ?;", + with: [sqlight.text(name)], + on: connection, + expecting: dynamic.tuple4( + dynamic.string, + dynamic.int, + dynamic.string, + dynamic.string, + ), + ) + { + Ok([first_element]) -> { + Counter( + first_element.0, + first_element.1, + first_element.2, + first_element.3, + ) + } + _ -> Counter(name, 0, "", "") + } } - _ -> Counter(name, 0, "", "") } } |