summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/Cargo.lock62
-rw-r--r--server/Cargo.toml1
-rw-r--r--server/src/models/user.rs29
-rw-r--r--server/src/routes/user.rs16
4 files changed, 93 insertions, 15 deletions
diff --git a/server/Cargo.lock b/server/Cargo.lock
index b9f62cc..fcc90ba 100644
--- a/server/Cargo.lock
+++ b/server/Cargo.lock
@@ -136,6 +136,15 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "block-buffer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "block-buffer"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
@@ -250,11 +259,20 @@ dependencies = [
[[package]]
name = "digest"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "digest"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
- "block-buffer",
+ "block-buffer 0.10.2",
"crypto-common",
"subtle",
]
@@ -471,7 +489,7 @@ version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
- "digest",
+ "digest 0.10.3",
]
[[package]]
@@ -645,6 +663,7 @@ dependencies = [
"once_cell",
"serde",
"serde_json",
+ "sha256",
"sqlx",
"tokio",
"tower-http",
@@ -679,7 +698,7 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582"
dependencies = [
- "digest",
+ "digest 0.10.3",
]
[[package]]
@@ -778,6 +797,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e"
[[package]]
+name = "opaque-debug"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
+
+[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1085,7 +1110,20 @@ checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f"
dependencies = [
"cfg-if",
"cpufeatures",
- "digest",
+ "digest 0.10.3",
+]
+
+[[package]]
+name = "sha2"
+version = "0.9.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800"
+dependencies = [
+ "block-buffer 0.9.0",
+ "cfg-if",
+ "cpufeatures",
+ "digest 0.9.0",
+ "opaque-debug",
]
[[package]]
@@ -1096,7 +1134,17 @@ checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676"
dependencies = [
"cfg-if",
"cpufeatures",
- "digest",
+ "digest 0.10.3",
+]
+
+[[package]]
+name = "sha256"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4e84a7f596c081d359de5e06a83877138bc3c4483591e1af1916e1472e6e146e"
+dependencies = [
+ "hex",
+ "sha2 0.9.9",
]
[[package]]
@@ -1213,7 +1261,7 @@ dependencies = [
"serde",
"serde_json",
"sha-1",
- "sha2",
+ "sha2 0.10.2",
"smallvec",
"sqlformat",
"sqlx-rt",
@@ -1237,7 +1285,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
- "sha2",
+ "sha2 0.10.2",
"sqlx-core",
"sqlx-rt",
"syn",
diff --git a/server/Cargo.toml b/server/Cargo.toml
index 08a3b07..83ea0d9 100644
--- a/server/Cargo.toml
+++ b/server/Cargo.toml
@@ -15,3 +15,4 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tower-http = { version = "0.3.4", features = ["trace", "compression-br", "propagate-header", "sensitive-headers"] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres" ] }
+sha256 = "1.0.3"
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?;
diff --git a/server/src/routes/user.rs b/server/src/routes/user.rs
index 1b43e01..7aecccb 100644
--- a/server/src/routes/user.rs
+++ b/server/src/routes/user.rs
@@ -1,9 +1,12 @@
use crate::errors::AppError;
-use crate::models::user::{User, UserList};
-use axum::{routing::get, Json, Router};
+use crate::models::user::{User, UserCreate, UserList};
+use axum::{
+ routing::{get},
+ Json, Router,
+};
pub fn create_route() -> Router {
- Router::new().route("/", get(list_users))
+ Router::new().route("/", get(list_users).post(create_user))
}
async fn list_users() -> Result<Json<Vec<UserList>>, AppError> {
@@ -11,3 +14,10 @@ async fn list_users() -> Result<Json<Vec<UserList>>, AppError> {
Ok(Json(users))
}
+
+async fn create_user(Json(payload): Json<UserCreate>) -> Result<Json<UserList>, AppError> {
+ let user = User::new(payload.email, payload.password);
+ let user_new = User::create(user).await?;
+
+ Ok(Json(user_new))
+}