From 2231ab6730552d3b58b00ae42d490743a29d7c8b Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 23 Aug 2022 18:57:16 +0200 Subject: Add docs --- server/src/models/user.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'server/src/models/user.rs') diff --git a/server/src/models/user.rs b/server/src/models/user.rs index dd96f90..06cde0a 100644 --- a/server/src/models/user.rs +++ b/server/src/models/user.rs @@ -4,6 +4,7 @@ use crate::errors::AppError; use serde::{Deserialize, Serialize}; use validator::Validate; +/// User model #[derive(Deserialize, Serialize, Validate)] pub struct User { id: i32, @@ -14,13 +15,16 @@ pub struct User { is_staff: Option, } +/// Response used to print a user (or a users list) #[derive(Deserialize, Serialize)] pub struct UserList { + // It is public because it used by `Claims` creation pub id: i32, email: String, is_staff: Option, } +/// Payload used for user creation #[derive(Deserialize)] pub struct UserCreate { pub email: String, @@ -28,6 +32,7 @@ pub struct UserCreate { } impl User { + /// By default an user has id = 0. It is not created yet pub fn new(email: String, password: String) -> Self { Self { id: 0, @@ -37,6 +42,7 @@ impl User { } } + /// Create a new user from the model using a SHA256 crypted password pub async fn create(user: User) -> Result { let pool = unsafe { get_client() }; @@ -61,6 +67,7 @@ impl User { Ok(rec) } + /// Find a user using the model. It used for login pub async fn find(user: User) -> Result { let pool = unsafe { get_client() }; @@ -81,6 +88,7 @@ impl User { Ok(rec) } + /// Returns the user with id = `user_id` pub async fn find_by_id(user_id: i32) -> Result { let pool = unsafe { get_client() }; @@ -98,6 +106,7 @@ impl User { Ok(rec) } + /// List all users pub async fn list() -> Result, AppError> { let pool = unsafe { get_client() }; let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#) -- cgit v1.2.3-18-g5258