summaryrefslogtreecommitdiff
path: root/server/src/models
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-11-20 22:03:02 +0100
committerSanto Cariotti <santo@dcariotti.me>2022-11-20 22:03:02 +0100
commit745841f0a64fe122a93b0426e3c249b089fb7052 (patch)
treed8cd883b4ce5c6eb99e59eda865d87a22262402c /server/src/models
parent8d47b483a06a0848a605374ddf12e25e0c2e359f (diff)
Fixs for build
Diffstat (limited to 'server/src/models')
-rw-r--r--server/src/models/user.rs27
1 files changed, 12 insertions, 15 deletions
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<UserList, AppError> {
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<Vec<UserList>, AppError> {
let pool = unsafe { get_client() };
- let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#)
+ let rows: Vec<UserList> = sqlx::query_as(r#"SELECT id, email, is_staff FROM users"#)
.fetch_all(pool)
.await?;