diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-09-03 12:27:07 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-09-03 12:27:07 +0200 |
commit | 1aab73450987ebdaa813265b6911b4ce8134a788 (patch) | |
tree | f24789e456e369e8f3c5582f232ac74a40390c42 /src/graphql | |
parent | 518eb39eb4c63d20a1ff71930e016937666e55e5 (diff) |
Add name and address fields for users
Diffstat (limited to 'src/graphql')
-rw-r--r-- | src/graphql/query.rs | 2 | ||||
-rw-r--r-- | src/graphql/types/user.rs | 18 |
2 files changed, 17 insertions, 3 deletions
diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 27ef11f..d2b7968 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -18,7 +18,7 @@ impl Query { /// curl http://localhost:8000/graphql /// -H 'authorization: Bearer ***' /// -H 'content-type: application/json' - /// -d '{"query":"{users(limit: 2) { id, email, password, isAdmin }}"}' + /// -d '{"query":"{users(limit: 2) { id, email, password, name, address, isAdmin }}"}' /// ``` async fn users<'ctx>( &self, diff --git a/src/graphql/types/user.rs b/src/graphql/types/user.rs index 96fcf40..85f559e 100644 --- a/src/graphql/types/user.rs +++ b/src/graphql/types/user.rs @@ -11,6 +11,8 @@ pub struct User { pub id: i32, pub email: String, pub password: String, + pub name: Option<String>, + pub address: Option<String>, pub is_admin: bool, } @@ -28,6 +30,14 @@ impl User { String::from("******") } + async fn name(&self) -> String { + self.name.clone().unwrap_or(String::default()) + } + + async fn address(&self) -> String { + self.address.clone().unwrap_or(String::default()) + } + async fn is_admin(&self) -> bool { self.is_admin } @@ -50,7 +60,7 @@ pub async fn get_users<'ctx>( Authentication::Logged(_claims) => { let rows = client .query( - "SELECT id, email, password, is_admin FROM users LIMIT $1 OFFSET $2", + "SELECT id, email, password, name, address, is_admin FROM users LIMIT $1 OFFSET $2", &[&limit.unwrap_or(20), &offset.unwrap_or(0)], ) .await @@ -62,6 +72,8 @@ pub async fn get_users<'ctx>( id: row.get("id"), email: row.get("email"), password: row.get("password"), + name: row.get("name"), + address: row.get("address"), is_admin: row.get("is_admin"), }) .collect(); @@ -75,7 +87,7 @@ pub async fn get_users<'ctx>( pub async fn find_user(client: &Client, id: i32) -> Result<User, AppError> { let rows = client .query( - "SELECT id, email, password, is_admin FROM users WHERE id = $1", + "SELECT id, email, password, name, address, is_admin FROM users WHERE id = $1", &[&id], ) .await @@ -87,6 +99,8 @@ pub async fn find_user(client: &Client, id: i32) -> Result<User, AppError> { id: row.get("id"), email: row.get("email"), password: row.get("password"), + name: row.get("name"), + address: row.get("address"), is_admin: row.get("is_admin"), }) .collect(); |