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/main.rs | 2 +- server/src/models/user.rs | 27 ++++++++++++--------------- server/src/routes/user.rs | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) (limited to 'server') diff --git a/server/src/main.rs b/server/src/main.rs index 508d6cd..4a211fd 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -18,7 +18,7 @@ use tracing::Span; async fn main() { let app = create_app().await; - /// By default the server is bind at "127.0.0.1:3000" + // By default the server is bind at "127.0.0.1:3000" let addr = std::env::var("ALLOWED_HOST").unwrap_or_else(|_| "127.0.0.1:3000".to_string()); tracing::info!("Listening on {}", addr); 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?; diff --git a/server/src/routes/user.rs b/server/src/routes/user.rs index a3d7a5e..5733871 100644 --- a/server/src/routes/user.rs +++ b/server/src/routes/user.rs @@ -3,7 +3,7 @@ use crate::models::{ auth::Claims, user::{User, UserCreate, UserList}, }; -use axum::{extract::Path, routing::get, Json, Router}; +use axum::{routing::get, Json, Router}; /// Create routes for `/v1/users/` namespace pub fn create_route() -> Router { -- cgit v1.2.3-18-g5258