summaryrefslogtreecommitdiffstats
path: root/src/graphql/types
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-08-23 20:19:32 +0000
committerSanto Cariotti <santo@dcariotti.me>2024-08-23 20:19:32 +0000
commit7b9e9d2da5d85f23d43724fe2ca5012918ea54be (patch)
tree801ccae65d27fcf69bfca5db18265fbb7b5d7f21 /src/graphql/types
parentcb1aa9668a967fe875e716469ccf936ed8693ad3 (diff)
Add doc
Diffstat (limited to 'src/graphql/types')
-rw-r--r--src/graphql/types/position.rs10
-rw-r--r--src/graphql/types/user.rs6
2 files changed, 16 insertions, 0 deletions
diff --git a/src/graphql/types/position.rs b/src/graphql/types/position.rs
index 8b2d658..86e219c 100644
--- a/src/graphql/types/position.rs
+++ b/src/graphql/types/position.rs
@@ -8,6 +8,7 @@ use tokio_postgres::types::{to_sql_checked, FromSql, IsNull, ToSql, Type};
use super::user::find_user;
#[derive(Enum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
+/// Enumeration which refers to the kind of moving activity
pub enum MovingActivity {
// "Car" of the doc
InVehicle,
@@ -67,6 +68,7 @@ impl ToSql for MovingActivity {
}
#[derive(Clone, Debug, Serialize, Deserialize)]
+/// Position struct
pub struct Position {
pub id: i32,
pub user_id: i32,
@@ -103,10 +105,18 @@ impl Position {
}
}
+/// Get positions from the database
pub async fn get_positions<'ctx>(
ctx: &Context<'ctx>,
+
+ // Optional filter by user id. If not defined returns only available positions:
+ // If claimed user is admin returns everything, otherwise only positions linked to that user.
user_id: Option<i32>,
+
+ // Optional limit results
limit: Option<i64>,
+
+ // Optional offset results. It should be used with limit field.
offset: Option<i64>,
) -> Result<Option<Vec<Position>>, String> {
let state = ctx.data::<AppState>().expect("Can't connect to db");
diff --git a/src/graphql/types/user.rs b/src/graphql/types/user.rs
index 4ecf086..96fcf40 100644
--- a/src/graphql/types/user.rs
+++ b/src/graphql/types/user.rs
@@ -6,6 +6,7 @@ use tokio_postgres::Client;
use super::jwt::Authentication;
#[derive(Clone, Debug, Serialize, Deserialize)]
+/// User struct
pub struct User {
pub id: i32,
pub email: String,
@@ -32,9 +33,13 @@ impl User {
}
}
+/// Get users from the database
pub async fn get_users<'ctx>(
ctx: &Context<'ctx>,
+
+ // Optional limit results
limit: Option<i64>,
+ // Optional offset results. It should be used with limit field.
offset: Option<i64>,
) -> Result<Option<Vec<User>>, String> {
let state = ctx.data::<AppState>().expect("Can't connect to db");
@@ -66,6 +71,7 @@ pub async fn get_users<'ctx>(
}
}
+/// Find an user with id = `id` using the PostgreSQL `client`
pub async fn find_user(client: &Client, id: i32) -> Result<User, AppError> {
let rows = client
.query(