ezidam/crates/apps/src/database.rs

110 lines
2.9 KiB
Rust

use crate::error::Error;
use crate::App;
use database::sqlx::SqliteExecutor;
use database::Apps as DatabaseApps;
use hash::Secret;
use id::AppID;
use url::Url;
impl From<DatabaseApps> for App {
fn from(db: DatabaseApps) -> Self {
Self {
id: AppID(db.id),
created_at: db.created_at,
updated_at: db.updated_at,
is_archived: db.is_archived,
label: db.label,
redirect_uri: db.redirect_uri,
secret: db.secret,
is_confidential: db.is_confidential,
}
}
}
impl App {
pub async fn get_all(
conn: impl SqliteExecutor<'_>,
filter_get_archived: Option<bool>,
) -> Result<Vec<Self>, Error> {
Ok(DatabaseApps::get_all(conn, filter_get_archived)
.await?
.into_iter()
.map(Self::from)
.collect::<Vec<_>>())
}
pub async fn insert(
conn: impl SqliteExecutor<'_>,
id: &AppID,
label: &str,
redirect_uri: &Url,
secret: &Secret,
is_confidential: bool,
) -> Result<Option<()>, Error> {
Ok(DatabaseApps::insert(
conn,
id.as_ref(),
label,
redirect_uri.as_str(),
secret.hash(),
is_confidential,
)
.await?)
}
/// App needs to be not archived
pub(crate) async fn get_one(
conn: impl SqliteExecutor<'_>,
id: &str,
redirect: &str,
) -> Result<Option<Self>, database::Error> {
Ok(DatabaseApps::get_one(conn, id, redirect)
.await?
.map(Self::from))
}
pub async fn get_one_by_id(
conn: impl SqliteExecutor<'_>,
id: &str,
) -> Result<Option<Self>, Error> {
Ok(DatabaseApps::get_one_by_id(conn, id).await?.map(Self::from))
}
pub async fn get_one_from_authorization_code(
conn: impl SqliteExecutor<'_>,
code: &str,
) -> Result<Option<Self>, Error> {
Ok(DatabaseApps::get_one_from_authorization_code(conn, code)
.await?
.map(Self::from))
}
pub async fn update(
conn: impl SqliteExecutor<'_>,
id: &AppID,
label: &str,
redirect_uri: &Url,
is_confidential: bool,
) -> Result<Option<()>, Error> {
Ok(DatabaseApps::update(
conn,
id.as_ref(),
label,
redirect_uri.as_str(),
is_confidential,
)
.await?)
}
pub async fn new_secret(
&self,
conn: impl SqliteExecutor<'_>,
secret: &Secret,
) -> Result<Option<()>, Error> {
Ok(DatabaseApps::new_secret(conn, self.id.as_ref(), secret.hash()).await?)
}
pub async fn archive(&self, conn: impl SqliteExecutor<'_>) -> Result<Option<()>, Error> {
Ok(DatabaseApps::archive(conn, self.id.as_ref()).await?)
}
}