2023-05-24 14:19:46 +00:00
|
|
|
//! # Users service
|
|
|
|
|
|
|
|
use crate::db_connection;
|
|
|
|
use crate::models::{NewUser, User};
|
|
|
|
use crate::schema::users;
|
|
|
|
use crate::utils::time_utils::time;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
|
|
|
/// Create a new account
|
|
|
|
pub async fn create_account(name: &str, email: &str) -> anyhow::Result<User> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
let res = diesel::insert_into(users::table)
|
|
|
|
.values(&NewUser {
|
|
|
|
name: name.trim(),
|
|
|
|
email: email.trim(),
|
|
|
|
time_create: time() as i64,
|
|
|
|
})
|
|
|
|
.get_result(conn)?;
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
2023-05-25 07:42:43 +00:00
|
|
|
|
|
|
|
/// Check if an email address is already associated with an account
|
|
|
|
pub async fn exists_email(email: &str) -> anyhow::Result<bool> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
let count: i64 = users::table
|
|
|
|
.filter(users::email.eq(email))
|
|
|
|
.count()
|
|
|
|
.get_result(conn)?;
|
|
|
|
|
|
|
|
Ok(count != 0)
|
|
|
|
})
|
|
|
|
}
|