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>> {
|
pub async fn from_username(pool: &MySqlPool, username: &String) -> Result<Option<Self>> {
|
||||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE username = ?;"#, username)
|
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)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(row) => {
|
Ok(account) => Ok(Some(account)),
|
||||||
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))
|
|
||||||
}
|
|
||||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||||
Err(e) => Err(Error::new(e)),
|
Err(e) => Err(Error::new(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn from_id(pool: &MySqlPool, id: u64) -> Result<Option<Self>> {
|
pub async fn from_id(pool: &MySqlPool, id: u64) -> Result<Option<Self>> {
|
||||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE id = ?;"#, id)
|
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)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(row) => {
|
Ok(account) => Ok(Some(account)),
|
||||||
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))
|
|
||||||
}
|
|
||||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||||
Err(e) => Err(Error::new(e)),
|
Err(e) => Err(Error::new(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn from_email(pool: &MySqlPool, email: &String) -> Result<Option<Self>> {
|
pub async fn from_email(pool: &MySqlPool, email: &String) -> Result<Option<Self>> {
|
||||||
match sqlx::query!(r#"SELECT * FROM Accounts WHERE email = ?;"#, email)
|
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)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(row) => {
|
Ok(account) => Ok(Some(account)),
|
||||||
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))
|
|
||||||
}
|
|
||||||
Err(sqlx::Error::RowNotFound) => Ok(None),
|
Err(sqlx::Error::RowNotFound) => Ok(None),
|
||||||
Err(e) => Err(Error::new(e)),
|
Err(e) => Err(Error::new(e)),
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,8 @@ impl AuthToken {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check(pool: &MySqlPool, alphanumeric_token: &String) -> Result<Option<Self>> {
|
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 = ?;"#,
|
r#"SELECT * FROM AuthTokens WHERE token = ?;"#,
|
||||||
alphanumeric_token
|
alphanumeric_token
|
||||||
)
|
)
|
||||||
|
@ -59,23 +60,11 @@ impl AuthToken {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match query_result {
|
match query_result {
|
||||||
Ok(row) => {
|
Ok(token) => {
|
||||||
let token = Self {
|
|
||||||
token: row.token,
|
|
||||||
account: row.account,
|
|
||||||
expire: row.expire,
|
|
||||||
};
|
|
||||||
|
|
||||||
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
||||||
Ok(Some(token))
|
Ok(Some(token))
|
||||||
} else {
|
} else {
|
||||||
// The token expired
|
token.delete(pool).await?;
|
||||||
sqlx::query!(
|
|
||||||
r#"DELETE FROM AuthTokens WHERE token = ?;"#,
|
|
||||||
alphanumeric_token
|
|
||||||
)
|
|
||||||
.execute(pool)
|
|
||||||
.await?;
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,6 +72,13 @@ impl AuthToken {
|
||||||
Err(e) => Err(Error::new(e)),
|
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)]
|
#[derive(Debug)]
|
||||||
|
@ -138,7 +134,8 @@ impl VerificationToken {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check(pool: &MySqlPool, alphanumeric_token: &String) -> Result<Option<Self>> {
|
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 = ?;"#,
|
r#"SELECT * FROM VerificationTokens WHERE token = ?;"#,
|
||||||
alphanumeric_token
|
alphanumeric_token
|
||||||
)
|
)
|
||||||
|
@ -146,23 +143,11 @@ impl VerificationToken {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match query_result {
|
match query_result {
|
||||||
Ok(row) => {
|
Ok(token) => {
|
||||||
let token = Self {
|
|
||||||
token: row.token,
|
|
||||||
account: row.account,
|
|
||||||
expire: row.expire,
|
|
||||||
};
|
|
||||||
|
|
||||||
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
if token.expire.timestamp() > chrono::Utc::now().timestamp() {
|
||||||
Ok(Some(token))
|
Ok(Some(token))
|
||||||
} else {
|
} else {
|
||||||
// The token expired
|
token.delete(pool).await?;
|
||||||
sqlx::query!(
|
|
||||||
r#"DELETE FROM VerificationTokens WHERE token = ?;"#,
|
|
||||||
alphanumeric_token
|
|
||||||
)
|
|
||||||
.execute(pool)
|
|
||||||
.await?;
|
|
||||||
Ok(None)
|
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!(
|
sqlx::query!(
|
||||||
r#"DELETE FROM VerificationTokens WHERE token = ?;"#,
|
r#"DELETE FROM VerificationTokens WHERE token = ?;"#,
|
||||||
self.token
|
self.token
|
||||||
)
|
)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn apply(&self, pool: &MySqlPool) -> Result<()> {
|
||||||
|
self.delete(pool).await?;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"UPDATE Accounts SET verified=true WHERE id = ?;"#,
|
r#"UPDATE Accounts SET verified=true WHERE id = ?;"#,
|
||||||
|
|
Loading…
Reference in New Issue