users: get user by id, email, username
This commit is contained in:
parent
b8f6cae85e
commit
d790d2ff29
8 changed files with 254 additions and 6 deletions
|
|
@ -2,8 +2,10 @@ use crate::error::Error;
|
|||
use crate::User;
|
||||
use database::sqlx::SqliteExecutor;
|
||||
use database::Users as DatabaseUsers;
|
||||
use email_address::EmailAddress;
|
||||
use hash::Password;
|
||||
use id::UserID;
|
||||
use std::str::FromStr;
|
||||
|
||||
impl From<DatabaseUsers> for User {
|
||||
fn from(db: DatabaseUsers) -> Self {
|
||||
|
|
@ -43,12 +45,46 @@ impl User {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn get_by_id(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
id: &UserID,
|
||||
) -> Result<Option<Self>, Error> {
|
||||
async fn get_by_id(conn: impl SqliteExecutor<'_>, id: &UserID) -> Result<Option<Self>, Error> {
|
||||
Ok(DatabaseUsers::get_one_by_id(conn, &id.0)
|
||||
.await?
|
||||
.map(Self::from))
|
||||
}
|
||||
|
||||
async fn get_by_email(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
email: &EmailAddress,
|
||||
) -> Result<Option<Self>, Error> {
|
||||
Ok(DatabaseUsers::get_one_by_email(conn, email.as_str())
|
||||
.await?
|
||||
.map(Self::from))
|
||||
}
|
||||
|
||||
async fn get_by_username(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
username: &str,
|
||||
) -> Result<Option<Self>, Error> {
|
||||
Ok(DatabaseUsers::get_one_by_username(conn, username)
|
||||
.await?
|
||||
.map(Self::from))
|
||||
}
|
||||
|
||||
/// Get by ID, Email, Username (in that order)
|
||||
pub async fn get_by_login(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
login: &str,
|
||||
) -> Result<Option<Self>, Error> {
|
||||
// Parse login as ID
|
||||
if let Ok(id) = UserID::from_str(login) {
|
||||
return Self::get_by_id(conn, &id).await;
|
||||
}
|
||||
|
||||
// Parse login as email
|
||||
if let Ok(email) = EmailAddress::from_str(login) {
|
||||
return Self::get_by_email(conn, &email).await;
|
||||
}
|
||||
|
||||
// Get user from username
|
||||
Self::get_by_username(conn, login).await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue