2020-05-09 06:37:08 +00:00
|
|
|
use diesel::prelude::*;
|
|
|
|
|
|
|
|
use crate::errors;
|
|
|
|
use crate::models;
|
2020-05-11 19:33:11 +00:00
|
|
|
use bcrypt::{hash, verify, DEFAULT_COST};
|
2020-05-09 06:37:08 +00:00
|
|
|
|
|
|
|
pub fn find_user_by_uid(
|
|
|
|
uid: i32,
|
|
|
|
conn: &SqliteConnection,
|
2021-04-21 08:02:03 +00:00
|
|
|
) -> Result<Option<models::UserDto>, errors::DomainError> {
|
2020-05-09 06:37:08 +00:00
|
|
|
use crate::schema::users::dsl::*;
|
|
|
|
|
|
|
|
let maybe_user = users
|
|
|
|
.select((name, created_at))
|
|
|
|
.find(uid)
|
2021-04-21 08:02:03 +00:00
|
|
|
.first::<models::UserDto>(conn)
|
2020-05-09 06:37:08 +00:00
|
|
|
.optional();
|
|
|
|
|
|
|
|
Ok(maybe_user?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn _find_user_by_name(
|
|
|
|
user_name: String,
|
|
|
|
conn: &SqliteConnection,
|
2021-04-21 08:02:03 +00:00
|
|
|
) -> Result<Option<models::UserDto>, errors::DomainError> {
|
2020-05-12 12:07:53 +00:00
|
|
|
let maybe_user = query::_get_user_by_name(&user_name)
|
2021-04-21 08:02:03 +00:00
|
|
|
.first::<models::UserDto>(conn)
|
2020-05-09 06:37:08 +00:00
|
|
|
.optional();
|
|
|
|
|
|
|
|
Ok(maybe_user?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_all(
|
|
|
|
conn: &SqliteConnection,
|
2021-05-02 16:28:18 +00:00
|
|
|
) -> Result<Vec<models::UserDto>, errors::DomainError> {
|
2020-05-09 06:37:08 +00:00
|
|
|
use crate::schema::users::dsl::*;
|
|
|
|
Ok(users
|
|
|
|
.select((name, created_at))
|
2021-05-02 16:28:18 +00:00
|
|
|
.load::<models::UserDto>(conn)?)
|
2020-05-09 06:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run query using Diesel to insert a new database row and return the result.
|
|
|
|
pub fn insert_new_user(
|
2021-04-21 05:41:48 +00:00
|
|
|
nu: models::NewUser,
|
2020-05-09 06:37:08 +00:00
|
|
|
conn: &SqliteConnection,
|
2021-04-21 08:02:03 +00:00
|
|
|
) -> Result<models::UserDto, errors::DomainError> {
|
2020-05-09 06:37:08 +00:00
|
|
|
// It is common when using Diesel with Actix web to import schema-related
|
|
|
|
// modules inside a function's scope (rather than the normal module's scope)
|
|
|
|
// to prevent import collisions and namespace pollution.
|
|
|
|
use crate::schema::users::dsl::*;
|
2021-04-21 05:41:48 +00:00
|
|
|
let nu = {
|
2021-04-22 15:05:06 +00:00
|
|
|
let mut nu2 = nu;
|
2021-04-21 05:41:48 +00:00
|
|
|
nu2.password = hash(&nu2.password, DEFAULT_COST)?;
|
|
|
|
nu2
|
|
|
|
};
|
2020-05-09 06:37:08 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
diesel::insert_into(users).values(&nu).execute(conn)?;
|
2020-05-11 19:33:11 +00:00
|
|
|
let user =
|
2021-04-21 08:02:03 +00:00
|
|
|
query::_get_user_by_name(&nu.name).first::<models::UserDto>(conn)?;
|
2020-05-11 19:33:11 +00:00
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:05:06 +00:00
|
|
|
pub fn verify_password(
|
|
|
|
user_name: &str,
|
|
|
|
given_password: &str,
|
2020-05-11 19:33:11 +00:00
|
|
|
conn: &SqliteConnection,
|
|
|
|
) -> Result<bool, errors::DomainError> {
|
|
|
|
use crate::schema::users::dsl::*;
|
|
|
|
let password_hash = users
|
|
|
|
.select(password)
|
|
|
|
.filter(name.eq(user_name))
|
|
|
|
.first::<String>(conn)?;
|
2020-05-12 12:07:53 +00:00
|
|
|
Ok(verify(given_password, password_hash.as_str())?)
|
2020-05-11 19:33:11 +00:00
|
|
|
}
|
2020-05-12 12:07:53 +00:00
|
|
|
|
|
|
|
mod query {
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use diesel::sql_types::Text;
|
|
|
|
use diesel::sql_types::Timestamp;
|
|
|
|
use diesel::sqlite::Sqlite;
|
|
|
|
|
|
|
|
/// <'a, B, T> where a = lifetime, B = Backend, T = SQL data types
|
|
|
|
type Query<'a, B, T> = crate::schema::users::BoxedQuery<'a, B, T>;
|
|
|
|
|
2020-08-11 19:51:20 +00:00
|
|
|
pub fn _get_user_by_name(
|
2021-04-22 15:05:06 +00:00
|
|
|
user_name: &str,
|
2020-08-11 19:51:20 +00:00
|
|
|
) -> Query<Sqlite, (Text, Timestamp)> {
|
2020-05-12 12:07:53 +00:00
|
|
|
use crate::schema::users::dsl::*;
|
|
|
|
users
|
|
|
|
.select((name, created_at))
|
|
|
|
.filter(name.eq(user_name))
|
|
|
|
.into_boxed()
|
|
|
|
}
|
2020-05-09 06:37:08 +00:00
|
|
|
}
|