feat(db): cleaned up SQL statements
This commit is contained in:
parent
9f0280c204
commit
ccb3b3fd36
|
@ -52,66 +52,45 @@ impl Account {
|
|||
}
|
||||
|
||||
pub async fn from_username(pool: &MySqlPool, username: &String) -> Result<Option<Self>> {
|
||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE username = ?;"#, username)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
match sqlx::query_as!(
|
||||
Self,
|
||||
r#"SELECT id, username, email, salt, password, joined, verified as `verified: bool` FROM Accounts WHERE username = ?;"#,
|
||||
username
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let account = Account {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
salt: row.salt,
|
||||
password: row.password,
|
||||
joined: row.joined,
|
||||
verified: row.verified != 0,
|
||||
};
|
||||
Ok(Some(account))
|
||||
}
|
||||
Ok(account) => Ok(Some(account)),
|
||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||
Err(e) => Err(Error::new(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_id(pool: &MySqlPool, id: u64) -> Result<Option<Self>> {
|
||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE id = ?;"#, id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
match sqlx::query_as!(
|
||||
Self,
|
||||
r#"SELECT id, username, email, salt, password, joined, verified as `verified: bool` FROM Accounts WHERE id = ?;"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let account = Account {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
salt: row.salt,
|
||||
password: row.password,
|
||||
joined: row.joined,
|
||||
verified: row.verified != 0,
|
||||
};
|
||||
Ok(Some(account))
|
||||
}
|
||||
Ok(account) => Ok(Some(account)),
|
||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||
Err(e) => Err(Error::new(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_email(pool: &MySqlPool, email: &String) -> Result<Option<Self>> {
|
||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE email = ?;"#, email)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
match sqlx::query_as!(
|
||||
Self,
|
||||
r#"SELECT id, username, email, salt, password, joined, verified as `verified: bool` FROM Accounts WHERE email = ?;"#,
|
||||
email
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let account = Account {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
salt: row.salt,
|
||||
password: row.password,
|
||||
joined: row.joined,
|
||||
verified: row.verified != 0,
|
||||
};
|
||||
Ok(Some(account))
|
||||
}
|
||||
Ok(account) => Ok(Some(account)),
|
||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||
Err(e) => Err(Error::new(e)),
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ impl AuthToken {
|
|||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO AuthTokens (token, account, expire) VALUES (?, ?, ?);
|
||||
"#,
|
||||
"#,
|
||||
token.token,
|
||||
token.account,
|
||||
token.expire,
|
||||
|
@ -51,7 +51,8 @@ impl AuthToken {
|
|||
}
|
||||
|
||||
pub async fn check(pool: &MySqlPool, alphanumeric_token: &String) -> Result<Option<Self>> {
|
||||
let query_result = sqlx::query!(
|
||||
let query_result = sqlx::query_as!(
|
||||
Self,
|
||||
r#"SELECT * FROM AuthTokens WHERE token = ?;"#,
|
||||
alphanumeric_token
|
||||
)
|
||||
|
@ -59,23 +60,11 @@ impl AuthToken {
|
|||
.await;
|
||||
|
||||
match query_result {
|
||||
Ok(row) => {
|
||||
let token = Self {
|
||||
token: row.token,
|
||||
account: row.account,
|
||||
expire: row.expire,
|
||||
};
|
||||
|
||||
Ok(token) => {
|
||||
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
||||
Ok(Some(token))
|
||||
} else {
|
||||
// The token expired
|
||||
sqlx::query!(
|
||||
r#"DELETE FROM AuthTokens WHERE token = ?;"#,
|
||||
alphanumeric_token
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
token.delete(pool).await?;
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
@ -83,6 +72,13 @@ impl AuthToken {
|
|||
Err(e) => Err(Error::new(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(&self, pool: &MySqlPool) -> Result<()> {
|
||||
sqlx::query!(r#"DELETE FROM AuthTokens WHERE token = ?;"#, self.token)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -138,7 +134,8 @@ impl VerificationToken {
|
|||
}
|
||||
|
||||
pub async fn check(pool: &MySqlPool, alphanumeric_token: &String) -> Result<Option<Self>> {
|
||||
let query_result = sqlx::query!(
|
||||
let query_result = sqlx::query_as!(
|
||||
Self,
|
||||
r#"SELECT * FROM VerificationTokens WHERE token = ?;"#,
|
||||
alphanumeric_token
|
||||
)
|
||||
|
@ -146,23 +143,11 @@ impl VerificationToken {
|
|||
.await;
|
||||
|
||||
match query_result {
|
||||
Ok(row) => {
|
||||
let token = Self {
|
||||
token: row.token,
|
||||
account: row.account,
|
||||
expire: row.expire,
|
||||
};
|
||||
|
||||
Ok(token) => {
|
||||
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
||||
Ok(Some(token))
|
||||
} else {
|
||||
// The token expired
|
||||
sqlx::query!(
|
||||
r#"DELETE FROM VerificationTokens WHERE token = ?;"#,
|
||||
alphanumeric_token
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
token.delete(pool).await?;
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
@ -171,13 +156,18 @@ impl VerificationToken {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn apply(&self, pool: &MySqlPool) -> Result<()> {
|
||||
pub async fn delete(&self, pool: &MySqlPool) -> Result<()> {
|
||||
sqlx::query!(
|
||||
r#"DELETE FROM VerificationTokens WHERE token = ?;"#,
|
||||
self.token
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn apply(&self, pool: &MySqlPool) -> Result<()> {
|
||||
self.delete(pool).await?;
|
||||
|
||||
sqlx::query!(
|
||||
r#"UPDATE Accounts SET verified=true WHERE id = ?;"#,
|
||||
|
|
Loading…
Reference in New Issue