From 745841f0a64fe122a93b0426e3c249b089fb7052 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 20 Nov 2022 22:03:02 +0100 Subject: Fixs for build --- server/src/models/user.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'server/src/models/user.rs') diff --git a/server/src/models/user.rs b/server/src/models/user.rs index b94e114..ace4266 100644 --- a/server/src/models/user.rs +++ b/server/src/models/user.rs @@ -2,11 +2,11 @@ use crate::db::get_client; use crate::errors::AppError; use serde::{Deserialize, Serialize}; -use validator::Validate; use sqlx::Row; +use validator::Validate; /// User model -#[derive(Deserialize, Serialize, Validate)] +#[derive(Deserialize, Serialize, Validate, sqlx::FromRow)] pub struct User { id: i32, #[validate(length(min = 1, message = "Can not be empty"))] @@ -17,7 +17,7 @@ pub struct User { } /// Response used to print a user (or a users list) -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, sqlx::FromRow)] pub struct UserList { // It is public because it used by `Claims` creation pub id: i32, @@ -52,16 +52,15 @@ impl User { let crypted_password = sha256::digest(user.password); - let rec = sqlx::query_as!( - UserList, + let rec: UserList = sqlx::query_as( r#" INSERT INTO users (email, password) VALUES ( $1, $2 ) RETURNING id, email, is_staff "#, - user.email, - crypted_password ) + .bind(user.email) + .bind(crypted_password) .fetch_one(pool) .await?; @@ -74,15 +73,14 @@ impl User { let crypted_password = sha256::digest(user.password); - let rec = sqlx::query_as!( - UserList, + let rec: UserList = sqlx::query_as( r#" SELECT id, email, is_staff FROM "users" WHERE email = $1 AND password = $2 "#, - user.email, - crypted_password ) + .bind(user.email) + .bind(crypted_password) .fetch_one(pool) .await?; @@ -93,14 +91,13 @@ impl User { pub async fn find_by_id(user_id: i32) -> Result { let pool = unsafe { get_client() }; - let rec = sqlx::query_as!( - UserList, + let rec: UserList = sqlx::query_as( r#" SELECT id, email, is_staff FROM "users" WHERE id = $1 "#, - user_id ) + .bind(user_id) .fetch_one(pool) .await?; @@ -110,7 +107,7 @@ impl User { /// 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"#) + let rows: Vec = sqlx::query_as(r#"SELECT id, email, is_staff FROM users"#) .fetch_all(pool) .await?; -- cgit v1.2.3-18-g5258