-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: try to query user and put inside auth hook
- Loading branch information
1 parent
8994c03
commit 7406c5a
Showing
4 changed files
with
56 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
src/app/router.gleam | ||
src/app/hooks/hook.gleam | ||
src/app/config.gleam | ||
src/app/db/models/user.gleam | ||
src/app/db/connection.gleam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
import app/db/connection | ||
import cake | ||
import cake/select | ||
import cake/where | ||
import decode/zero | ||
import gleam/dynamic | ||
import gleam/iterator | ||
import gleam/result | ||
import sqlight | ||
|
||
pub opaque type User { | ||
User( | ||
id: String, | ||
id: Int, | ||
name: String, | ||
email: String, | ||
password: String, | ||
created_at: String, | ||
) | ||
} | ||
|
||
pub fn decode_user(row: dynamic.Dynamic) { | ||
todo | ||
fn decode_user(row: dynamic.Dynamic) { | ||
let decoder = { | ||
use id <- zero.field("id", zero.int) | ||
use name <- zero.field("name", zero.string) | ||
use email <- zero.field("email", zero.string) | ||
use password <- zero.field("password", zero.string) | ||
use created_at <- zero.field("created_at", zero.string) | ||
zero.success(User(id:, name:, email:, password:, created_at:)) | ||
} | ||
|
||
zero.run(row, decoder) | ||
} | ||
|
||
pub fn find_by_id(id: Int, conn: sqlight.Connection) { | ||
select.new() | ||
|> select.selects([ | ||
select.col("users.id"), | ||
select.col("users.name"), | ||
select.col("users.email"), | ||
select.col("users.password"), | ||
select.col("users.created_at"), | ||
]) | ||
|> select.from_table("users") | ||
|> select.where(where.col("users.id") |> where.eq(where.int(id))) | ||
|> select.comment("Find user by id") | ||
|> select.to_query | ||
|> cake.to_read_query | ||
|> connection.run_query_with(conn, decode_user) | ||
// |> result.try(fn(res) { res |> iterator.from_list |> iterator.at(0) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters