summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-13 13:23:57 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-13 13:23:57 +0000
commitbcff6540a98056c4b1f90e0efd784eb2b41f347f (patch)
tree1c851ee3679e8c5fb958866619dac943079009b1
parent412494edf618b8eda71656855656f9b8b21c3eae (diff)
Add avatar field
-rw-r--r--Cargo.lock78
-rw-r--r--Cargo.toml1
-rw-r--r--migrations/20220903093057_add-users-table.sql1
-rw-r--r--src/models/user.rs14
4 files changed, 90 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0900b7a..abbbc59 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -321,6 +321,41 @@ dependencies = [
]
[[package]]
+name = "darling"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "649c91bc01e8b1eac09fb91e8dbc7d517684ca6be8ebc75bb9cafc894f9fdb6f"
+dependencies = [
+ "fnv",
+ "ident_case",
+ "proc-macro2",
+ "quote",
+ "strsim",
+ "syn",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
+dependencies = [
+ "darling_core",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -647,6 +682,12 @@ dependencies = [
]
[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
+[[package]]
name = "idna"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -671,6 +712,7 @@ checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",
+ "serde 1.0.144",
]
[[package]]
@@ -1310,6 +1352,34 @@ dependencies = [
]
[[package]]
+name = "serde_with"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "368f2d60d049ea019a84dcd6687b0d1e0030fe663ae105039bdf967ed5e6a9a7"
+dependencies = [
+ "base64",
+ "chrono",
+ "hex",
+ "indexmap",
+ "serde 1.0.144",
+ "serde_json",
+ "serde_with_macros",
+ "time 0.3.13",
+]
+
+[[package]]
+name = "serde_with_macros"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1ccadfacf6cf10faad22bbadf55986bdd0856edfb5d9210aa1dcf1f516e84e93"
+dependencies = [
+ "darling",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "sha-1"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1535,6 +1605,12 @@ dependencies = [
]
[[package]]
+name = "strsim"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
+[[package]]
name = "subtle"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1606,6 +1682,7 @@ dependencies = [
"itoa",
"libc",
"num_threads",
+ "serde 1.0.144",
"time-macros",
]
@@ -1947,6 +2024,7 @@ dependencies = [
"rand",
"serde 1.0.144",
"serde_json",
+ "serde_with",
"sha256",
"sqlx",
"tokio",
diff --git a/Cargo.toml b/Cargo.toml
index 0cf6a32..1bca769 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,6 +11,7 @@ http-body = "0.4.3"
jsonwebtoken = "8.1"
once_cell = "1.13"
serde = { version = "1.0", features = ["derive"] }
+serde_with = "2.0.1"
serde_json = "1.0"
tokio = { version = "1.20", features = ["full"] }
tracing = "0.1"
diff --git a/migrations/20220903093057_add-users-table.sql b/migrations/20220903093057_add-users-table.sql
index 73c2396..58be825 100644
--- a/migrations/20220903093057_add-users-table.sql
+++ b/migrations/20220903093057_add-users-table.sql
@@ -3,5 +3,6 @@ CREATE TABLE users (
email VARCHAR(100) UNIQUE NOT NULL,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
+ avatar VARCHAR,
is_staff BOOLEAN DEFAULT false
);
diff --git a/src/models/user.rs b/src/models/user.rs
index 56ae307..b1427f6 100644
--- a/src/models/user.rs
+++ b/src/models/user.rs
@@ -3,6 +3,7 @@ use crate::db::get_client;
use crate::errors::AppError;
use serde::{Deserialize, Serialize};
+use serde_with::{serde_as, NoneAsEmptyString};
use validator::Validate;
/// User model
@@ -16,9 +17,11 @@ pub struct User {
#[validate(length(min = 8, message = "Must be min 8 chars length"))]
password: String,
is_staff: Option<bool>,
+ avatar: Option<String>,
}
/// Response used to print a user (or a users list)
+#[serde_as]
#[derive(Deserialize, Serialize)]
pub struct UserList {
// It is public because it used by `Claims` creation
@@ -26,6 +29,8 @@ pub struct UserList {
email: String,
username: String,
is_staff: Option<bool>,
+ #[serde_as(as = "NoneAsEmptyString")]
+ avatar: Option<String>,
}
impl User {
@@ -37,6 +42,7 @@ impl User {
username,
password,
is_staff: Some(false),
+ avatar: None,
}
}
@@ -54,7 +60,7 @@ impl User {
r#"
INSERT INTO users (email, username, password)
VALUES ( $1, $2, $3)
- RETURNING id, email, username, is_staff
+ RETURNING id, email, username, is_staff, avatar
"#,
user.email,
user.username,
@@ -75,7 +81,7 @@ impl User {
let rec = sqlx::query_as!(
UserList,
r#"
- SELECT id, email, username, is_staff FROM "users"
+ SELECT id, email, username, is_staff, avatar FROM "users"
WHERE username = $1 AND password = $2
"#,
user.username,
@@ -94,7 +100,7 @@ impl User {
let rec = sqlx::query_as!(
UserList,
r#"
- SELECT id, email, username, is_staff FROM "users"
+ SELECT id, email, username, is_staff, avatar FROM "users"
WHERE id = $1
"#,
user_id
@@ -110,7 +116,7 @@ impl User {
let pool = unsafe { get_client() };
let rows = sqlx::query_as!(
UserList,
- r#"SELECT id, email, username, is_staff FROM users
+ r#"SELECT id, email, username, is_staff, avatar FROM users
LIMIT $1 OFFSET $2
"#,
CONFIG.page_limit,