Actix-Demo/src/actions/users.rs

95 lines
2.8 KiB
Rust
Raw Normal View History

use diesel::prelude::*;
use crate::errors;
use crate::models;
use bcrypt::{hash, verify, DEFAULT_COST};
pub fn find_user_by_uid(
uid: i32,
2021-05-11 06:12:37 +00:00
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
2021-04-21 08:02:03 +00:00
) -> Result<Option<models::UserDto>, errors::DomainError> {
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)
.optional();
Ok(maybe_user?)
}
pub fn _find_user_by_name(
user_name: String,
2021-05-11 06:12:37 +00:00
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
2021-04-21 08:02:03 +00:00
) -> Result<Option<models::UserDto>, errors::DomainError> {
let maybe_user = query::_get_user_by_name(&user_name)
2021-04-21 08:02:03 +00:00
.first::<models::UserDto>(conn)
.optional();
Ok(maybe_user?)
}
pub fn get_all(
2021-05-11 06:12:37 +00:00
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
2021-05-02 16:28:18 +00:00
) -> Result<Vec<models::UserDto>, errors::DomainError> {
use crate::schema::users::dsl::*;
Ok(users
.select((name, created_at))
2021-05-02 16:28:18 +00:00
.load::<models::UserDto>(conn)?)
}
/// Run query using Diesel to insert a new database row and return the result.
pub fn insert_new_user(
nu: models::NewUser,
2021-05-11 06:12:37 +00:00
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
2021-04-21 08:02:03 +00:00
) -> Result<models::UserDto, errors::DomainError> {
// 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::*;
let nu = {
2021-04-22 15:05:06 +00:00
let mut nu2 = nu;
nu2.password = hash(&nu2.password, DEFAULT_COST)?;
nu2
};
diesel::insert_into(users).values(&nu).execute(conn)?;
let user =
2021-04-21 08:02:03 +00:00
query::_get_user_by_name(&nu.name).first::<models::UserDto>(conn)?;
Ok(user)
}
2021-04-22 15:05:06 +00:00
pub fn verify_password(
user_name: &str,
given_password: &str,
2021-05-11 06:12:37 +00:00
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<bool, errors::DomainError> {
use crate::schema::users::dsl::*;
let password_hash = users
.select(password)
.filter(name.eq(user_name))
.first::<String>(conn)?;
Ok(verify(given_password, password_hash.as_str())?)
}
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>;
pub fn _get_user_by_name(
2021-04-22 15:05:06 +00:00
user_name: &str,
) -> Query<Sqlite, (Text, Timestamp)> {
use crate::schema::users::dsl::*;
users
.select((name, created_at))
.filter(name.eq(user_name))
.into_boxed()
}
}