diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-08-21 12:29:23 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-08-21 12:29:23 +0200 |
commit | 91bfbd1abeb37ced029afba966a7134d92838baa (patch) | |
tree | dffcbeae78b89a76120ba476aeae4dda95effd61 | |
parent | 185ba5270aaf94de9b91e4455be27db5198ec21e (diff) |
Add users
Query must be something like '{users { id, email, password, isAdmin }}'
-rw-r--r-- | schema/init.sql | 7 | ||||
-rw-r--r-- | src/graphql/mod.rs | 1 | ||||
-rw-r--r-- | src/graphql/query.rs | 44 | ||||
-rw-r--r-- | src/graphql/types.rs | 29 |
4 files changed, 58 insertions, 23 deletions
diff --git a/schema/init.sql b/schema/init.sql new file mode 100644 index 0000000..38d1e80 --- /dev/null +++ b/schema/init.sql @@ -0,0 +1,7 @@ +CREATE TABLE users( + id SERIAL NOT NULL, + email text NOT NULL, + password text NOT NULL, + is_admin boolean default false, + PRIMARY KEY (id) +); diff --git a/src/graphql/mod.rs b/src/graphql/mod.rs index 305f0d3..b394fc1 100644 --- a/src/graphql/mod.rs +++ b/src/graphql/mod.rs @@ -1,2 +1,3 @@ pub mod query; pub mod routes; +pub mod types; diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 683885e..26f9a6e 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -1,3 +1,4 @@ +use crate::{graphql::types::User, state::AppState}; use async_graphql::{Context, Object}; pub struct Query; @@ -8,29 +9,26 @@ impl Query { "1.0" } - /// Returns the sum of a and b - async fn add<'ctx>( - &self, - _ctx: &Context<'ctx>, - #[graphql(desc = "First value")] a: i32, - #[graphql(desc = "Second value")] b: Option<i32>, - ) -> i32 { - // let state = ctx.data::<AppState>().unwrap(); - // let client = &*state.client; - // - // // Perform a database query - // let rows = client - // .query("SELECT owner FROM payment", &[]) - // .await - // .unwrap(); - // for row in rows { - // let owner: String = row.get(0); - // println!("{owner}"); - // } + /// Returns all the users + async fn users<'ctx>(&self, ctx: &Context<'ctx>) -> Result<Option<Vec<User>>, String> { + let state = ctx.data::<AppState>().expect("Can't connect to db"); + let client = &*state.client; - match b { - Some(x) => a + x, - None => a, - } + let rows = client + .query("SELECT id, email, password, is_admin FROM users", &[]) + .await + .unwrap(); + + let users: Vec<User> = rows + .iter() + .map(|row| User { + id: row.get("id"), + email: row.get("email"), + password: row.get("password"), + is_admin: row.get("is_admin"), + }) + .collect(); + + Ok(Some(users)) } } diff --git a/src/graphql/types.rs b/src/graphql/types.rs new file mode 100644 index 0000000..79241df --- /dev/null +++ b/src/graphql/types.rs @@ -0,0 +1,29 @@ +use async_graphql::Object; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct User { + pub id: i32, + pub email: String, + pub password: String, + pub is_admin: bool, +} + +#[Object] +impl User { + async fn id(&self) -> i32 { + self.id + } + + async fn email(&self) -> String { + self.email.clone() + } + + async fn password(&self) -> String { + String::from("******") + } + + async fn is_admin(&self) -> bool { + self.is_admin + } +} |