Skip to content

Commit

Permalink
feat: try to query user and put inside auth hook
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsam480 committed Oct 20, 2024
1 parent 8994c03 commit 7406c5a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
5 changes: 2 additions & 3 deletions .lasso-marks-tracker
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
41 changes: 38 additions & 3 deletions src/app/db/models/user.gleam
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) })
}
15 changes: 14 additions & 1 deletion src/app/hooks/auth.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import app/config
import app/db/models/user
import app/lib/response_helpers
import gleam/http/request
import gleam/int
import gleam/iterator
import gleam/result
import wisp

pub fn hook_auth(
Expand All @@ -11,7 +15,16 @@ pub fn hook_auth(

case header {
Ok(res) -> {
Ok(wisp.ok())
let user =
res |> int.parse |> result.try(fn(id) { user.find_by_id(id, ctx.db) })

case user {
Ok(u) -> {
let user = u |> iterator.from_list |> iterator.at(0)
Ok(wisp.ok() |> wisp.string_body("Hello ?" <> user.name))
}
_ -> Ok(wisp.not_found() |> wisp.string_body("Not found !!!"))
}
}
_ -> {
Error(
Expand Down
3 changes: 2 additions & 1 deletion test/okane_test.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import app/config
import app/router
import gleam/option
import gleeunit
import gleeunit/should
import sqlight
Expand All @@ -12,7 +13,7 @@ pub fn main() {
fn get_connection() -> config.Context {
use conn <- sqlight.with_connection(":memory:")

config.Context(conn)
config.Context(conn, option.None)
}

pub fn get_home_page_test() {
Expand Down

0 comments on commit 7406c5a

Please sign in to comment.