summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-08-21 12:38:20 +0200
committerSanto Cariotti <santo@dcariotti.me>2024-08-21 12:38:20 +0200
commit24388ba81515c57e812994fdb9147e6de7f3a5b6 (patch)
treeb14a9ba122b419f1c8ebbff9327ad3c7005fd7fa
parent91bfbd1abeb37ced029afba966a7134d92838baa (diff)
Reformat types mod
-rw-r--r--src/graphql/query.rs24
-rw-r--r--src/graphql/types.rs29
-rw-r--r--src/graphql/types/mod.rs1
-rw-r--r--src/graphql/types/user.rs52
4 files changed, 56 insertions, 50 deletions
diff --git a/src/graphql/query.rs b/src/graphql/query.rs
index 26f9a6e..ed83a9f 100644
--- a/src/graphql/query.rs
+++ b/src/graphql/query.rs
@@ -1,4 +1,4 @@
-use crate::{graphql::types::User, state::AppState};
+use crate::graphql::types::user;
use async_graphql::{Context, Object};
pub struct Query;
@@ -10,25 +10,7 @@ impl Query {
}
/// 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;
-
- 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))
+ async fn users<'ctx>(&self, ctx: &Context<'ctx>) -> Result<Option<Vec<user::User>>, String> {
+ user::get_users(ctx).await
}
}
diff --git a/src/graphql/types.rs b/src/graphql/types.rs
deleted file mode 100644
index 79241df..0000000
--- a/src/graphql/types.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-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
- }
-}
diff --git a/src/graphql/types/mod.rs b/src/graphql/types/mod.rs
new file mode 100644
index 0000000..22d12a3
--- /dev/null
+++ b/src/graphql/types/mod.rs
@@ -0,0 +1 @@
+pub mod user;
diff --git a/src/graphql/types/user.rs b/src/graphql/types/user.rs
new file mode 100644
index 0000000..bf9080f
--- /dev/null
+++ b/src/graphql/types/user.rs
@@ -0,0 +1,52 @@
+use crate::state::AppState;
+use async_graphql::{Context, 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
+ }
+}
+
+pub async fn get_users<'ctx>(ctx: &Context<'ctx>) -> Result<Option<Vec<User>>, String> {
+ let state = ctx.data::<AppState>().expect("Can't connect to db");
+ let client = &*state.client;
+
+ 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))
+}