···11import gleam/dynamic/decode
22import gleam/json
33-import gleam/list
44-import gleam/result
55-import pog
66-import shared/user/sql
73import youid/uuid
88-99-pub type UserError {
1010- Database(pog.QueryError)
1111- NotFound
1212-}
134145pub type User {
156 User(uuid: uuid.Uuid, full_name: String, email: String, phone: String)
···3021 use email <- decode.field("email", decode.string)
3122 use phone <- decode.field("phone", decode.string)
3223 decode.success(User(uuid:, full_name:, email:, phone:))
3333-}
3434-3535-/// Query an user by Uuid
3636-pub fn find(db: pog.Connection, uuid: uuid.Uuid) -> Result(User, UserError) {
3737- use returned <- result.try(
3838- sql.find_by_id(db, uuid)
3939- |> result.map_error(Database),
4040- )
4141-4242- use row <- result.map(
4343- list.first(returned.rows)
4444- |> result.replace_error(NotFound),
4545- )
4646-4747- User(uuid:, full_name: row.full_name, email: row.email, phone: row.phone)
4824}
49255026// helpers
-64
shared/src/shared/user/sql.gleam
···11-//// This module contains the code to run the sql queries defined in
22-//// `./src/shared/user/sql`.
33-//// > ๐ฟ๏ธ This module was generated automatically using v4.6.0 of
44-//// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
55-////
66-77-import gleam/dynamic/decode
88-import pog
99-import youid/uuid.{type Uuid}
1010-1111-/// A row you get from running the `find_by_id` query
1212-/// defined in `./src/shared/user/sql/find_by_id.sql`.
1313-///
1414-/// > ๐ฟ๏ธ This type definition was generated automatically using v4.6.0 of the
1515-/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
1616-///
1717-pub type FindByIdRow {
1818- FindByIdRow(id: Uuid, full_name: String, email: String, phone: String)
1919-}
2020-2121-/// Runs the `find_by_id` query
2222-/// defined in `./src/shared/user/sql/find_by_id.sql`.
2323-///
2424-/// > ๐ฟ๏ธ This function was generated automatically using v4.6.0 of
2525-/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
2626-///
2727-pub fn find_by_id(
2828- db: pog.Connection,
2929- arg_1: Uuid,
3030-) -> Result(pog.Returned(FindByIdRow), pog.QueryError) {
3131- let decoder = {
3232- use id <- decode.field(0, uuid_decoder())
3333- use full_name <- decode.field(1, decode.string)
3434- use email <- decode.field(2, decode.string)
3535- use phone <- decode.field(3, decode.string)
3636- decode.success(FindByIdRow(id:, full_name:, email:, phone:))
3737- }
3838-3939- "select
4040- u.id,
4141- u.full_name,
4242- u.email,
4343- u.phone
4444-from public.user_account as u
4545-where u.id =
4646-$1;
4747-"
4848- |> pog.query
4949- |> pog.parameter(pog.text(uuid.to_string(arg_1)))
5050- |> pog.returning(decoder)
5151- |> pog.execute(db)
5252-}
5353-5454-// --- Encoding/decoding utils -------------------------------------------------
5555-5656-/// A decoder to decode `Uuid`s coming from a Postgres query.
5757-///
5858-fn uuid_decoder() {
5959- use bit_array <- decode.then(decode.bit_array)
6060- case uuid.from_bit_array(bit_array) {
6161- Ok(uuid) -> decode.success(uuid)
6262- Error(_) -> decode.failure(uuid.v7(), "Uuid")
6363- }
6464-}