add even more fields to user schema

This commit is contained in:
anna 2022-12-19 15:45:03 +01:00
parent eccf4810a9
commit 776cbe6d52
Signed by: fef
GPG key ID: EC22E476DC2D3D84
4 changed files with 56 additions and 29 deletions

View file

@ -3,7 +3,9 @@ CREATE TABLE users (
account_id BIGINT REFERENCES accounts (id),
email VARCHAR NOT NULL,
password VARCHAR NOT NULL,
activated BOOLEAN NOT NULL DEFAULT FALSE
reason VARCHAR DEFAULT NULL,
activated BOOLEAN NOT NULL DEFAULT FALSE,
locale VARCHAR NOT NULL
);
CREATE UNIQUE INDEX index_users_on_email ON users (email);

View file

@ -13,11 +13,7 @@ impl PgUserDataSource {
PgUserDataSource { pool }
}
pub async fn by_id<I>(&self, id: I) -> Result<User>
where
I: Into<Id> + Send,
{
let id: Id = id.into();
pub async fn by_id(&self, id: Id) -> Result<User> {
let user: User = sqlx::query_as("SELECT * FROM users WHERE id = $1")
.bind(id)
.fetch_one(&self.pool)
@ -25,30 +21,35 @@ impl PgUserDataSource {
Ok(user)
}
pub async fn by_account<I>(&self, account_id: I) -> Result<User>
where
I: Into<Id> + Send,
{
let id: Id = account_id.into();
pub async fn by_account(&self, account_id: Id) -> Result<User> {
let user: User = sqlx::query_as("SELECT * FROM users WHERE account_id = $1")
.bind(id)
.bind(account_id)
.fetch_one(&self.pool)
.await?;
Ok(user)
}
pub async fn create<T>(&self, new: T) -> Result<User>
where
T: Into<NewUser> + Send,
{
let new = new.into();
let user: User =
sqlx::query_as("INSERT INTO users (account_id, email, password) VALUES ($1, $2, $3)")
.bind(new.account_id)
.bind(new.email)
.bind(new.password)
.fetch_one(&self.pool)
.await?;
pub async fn by_email(&self, email: &str) -> Result<User> {
let user: User = sqlx::query_as("SELECT * FROM users WHERE email = $1")
.bind(email)
.fetch_one(&self.pool)
.await?;
Ok(user)
}
pub async fn create(&self, new: NewUser) -> Result<User> {
let user: User = sqlx::query_as(
"INSERT INTO users (account_id, email, password, reason, locale) \
VALUES ($1, $2, $3, $4, $5) \
RETURNING *",
)
.bind(new.account_id)
.bind(new.email)
.bind(new.password)
.bind(new.reason)
.bind(new.locale)
.fetch_one(&self.pool)
.await?;
Ok(user)
}
}

View file

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use crate::core::*;
use crate::util::validate::{ResultBuilder, Validate};
#[derive(Deserialize, Serialize, FromRow)]
pub struct User {
@ -9,12 +10,17 @@ pub struct User {
pub account_id: Id,
pub email: String,
pub password: String,
pub reason: Option<String>,
pub locale: String,
pub activated: bool,
}
pub struct NewUser {
pub account_id: Id,
pub email: String,
pub password: String,
pub reason: Option<String>,
pub locale: String,
}
impl From<User> for Id {
@ -22,3 +28,11 @@ impl From<User> for Id {
user.id
}
}
impl Validate for NewUser {
fn validate(&self, builder: ResultBuilder) -> ResultBuilder {
builder.field(&self.email, "email", |f| {
f.check("Email must not be empty", |v| v.len() > 0)
})
}
}

View file

@ -19,20 +19,30 @@ impl UserRepo {
where
I: Into<Id> + Send,
{
self.db.by_id(id).await
self.db.by_id(id.into()).await
}
pub async fn by_account<I>(&self, account_id: I) -> Result<User>
where
I: Into<Id> + Send,
{
self.db.by_account(account_id).await
self.db.by_account(account_id.into()).await
}
pub async fn create<T>(&self, new: T) -> Result<User>
pub async fn by_email<T>(&self, email: T) -> Result<User>
where
T: Into<NewUser> + Send,
T: AsRef<str>,
{
self.db.create(new).await
self.db.by_email(email.as_ref()).await
}
pub async fn create<T, E>(&self, new: T) -> Result<User>
where
T: TryInto<Sane<NewUser>, Error = E> + Send,
E: Into<Error>,
{
self.db
.create(new.try_into().map_err(|e| e.into())?.inner())
.await
}
}