Skip to content

Commit

Permalink
Implemented FromRow for PgRow for User. Changed id type to Ulid.
Browse files Browse the repository at this point in the history
  • Loading branch information
agaviria committed Mar 30, 2024
1 parent 95debf3 commit a05e849
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/domain/user/entities.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
use sqlx;
use sqlx::Row;

use frunk::LabelledGeneric;

use crate::{
relay::Base64Cursor,
scalar::{Id, Time},
scalar::{self, Time, Ulid},

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for linux-gnu

unresolved import `crate::scalar::Ulid`

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Minimum supported rust version

unresolved import `crate::scalar::Ulid`

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for macos

unresolved import `crate::scalar::Ulid`

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win-gnu

unresolved import `crate::scalar::Ulid`

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win32-msvc

unresolved import `crate::scalar::Ulid`

Check failure on line 7 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win-msvc

unresolved import `crate::scalar::Ulid`
};

#[derive(Debug, Clone, sqlx::FromRow, LabelledGeneric)]
#[derive(Debug, Clone, LabelledGeneric)]
pub struct User {
pub id: Id,
pub id: Ulid,
pub created_at: Time,
pub updated_at: Time,
pub name: String,
pub email: String,
pub full_name: Option<String>,
}

impl<'r> sqlx::FromRow<'r, sqlx::postgres::PgRow> for User {
fn from_row(row: &'r sqlx::postgres::PgRow) -> Result<Self, sqlx::Error> {
let id: String = row.try_get("id")?;
let created_at: Time = row.try_get("created_at")?;
let updated_at: Time = row.try_get("updated_at")?;
let name: String = row.try_get("name")?;
let email: String = row.try_get("email")?;
let full_name: Option<String> = row.try_get("full_name")?;

Ok(Self {
id: id.parse().expect("failed to parse Id"),
created_at,
updated_at,
name,
email,
full_name,
})
}
}

#[derive(Debug, LabelledGeneric)]
pub struct UserEdge {
pub node: User,
Expand All @@ -25,7 +45,7 @@ pub struct UserEdge {

impl From<User> for UserEdge {
fn from(user: User) -> Self {
let cursor = Base64Cursor::new(user.id).encode();
let cursor = Base64Cursor::new(scalar::uid::Ulid(*user.id)).encode();

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for linux-gnu

failed to resolve: could not find `uid` in `scalar`

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Minimum supported rust version

failed to resolve: could not find `uid` in `scalar`

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for macos

failed to resolve: could not find `uid` in `scalar`

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win-gnu

failed to resolve: could not find `uid` in `scalar`

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win32-msvc

failed to resolve: could not find `uid` in `scalar`

Check failure on line 48 in src/domain/user/entities.rs

View workflow job for this annotation

GitHub Actions / Build for win-msvc

failed to resolve: could not find `uid` in `scalar`
Self { node: user, cursor }
}
}
Expand Down

0 comments on commit a05e849

Please sign in to comment.