summaryrefslogtreecommitdiff
path: root/server/src/models
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-22 22:32:54 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-22 22:32:54 +0200
commit0c656f0fe6b61c35bd40d43829a6a63fe353c589 (patch)
treec2bbbe08b62d15317fc45beb3da8290c9b93f251 /server/src/models
parent800ee06b1a59bf7bb6bb73016021bd9f28477aa4 (diff)
Create user
Diffstat (limited to 'server/src/models')
-rw-r--r--server/src/models/user.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/server/src/models/user.rs b/server/src/models/user.rs
index 08514a8..9545fac 100644
--- a/server/src/models/user.rs
+++ b/server/src/models/user.rs
@@ -8,22 +8,41 @@ pub struct User {
id: i32,
email: String,
password: String,
+ is_staff: Option<bool>,
}
#[derive(Deserialize, Serialize)]
pub struct UserList {
id: i32,
email: String,
+ is_staff: Option<bool>,
+}
+
+#[derive(Deserialize)]
+pub struct UserCreate {
+ pub email: String,
+ pub password: String,
}
impl User {
- pub async fn create(user: User) -> Result<i32, AppError> {
+ pub fn new(email: String, password: String) -> Self {
+ let crypted_password = sha256::digest(password);
+ Self {
+ id: 0,
+ email,
+ password: crypted_password,
+ is_staff: Some(false),
+ }
+ }
+
+ pub async fn create(user: User) -> Result<UserList, AppError> {
let pool = unsafe { get_client() };
- let rec = sqlx::query!(
+ let rec = sqlx::query_as!(
+ UserList,
r#"
INSERT INTO users (email, password)
VALUES ( $1, $2 )
- RETURNING id
+ RETURNING id, email, is_staff
"#,
user.email,
user.password
@@ -31,12 +50,12 @@ impl User {
.fetch_one(pool)
.await?;
- Ok(rec.id)
+ Ok(rec)
}
pub async fn list() -> Result<Vec<UserList>, AppError> {
let pool = unsafe { get_client() };
- let rows = sqlx::query_as!(UserList, r#"SELECT id, email FROM users"#)
+ let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#)
.fetch_all(pool)
.await?;