use anyhow::Context; use atrium_api::types::string::Did; use sqlx::PgPool; pub struct Admin { actor_did: Did, } impl Admin { pub fn new(did_string: String) -> Result { let actor_did = match Did::new(did_string) { Ok(did) => did, Err(err) => anyhow::bail!("{err}"), }; Ok(Self { actor_did }) } pub async fn store(&self, pool: PgPool) -> Result<(), anyhow::Error> { sqlx::query!( r#" INSERT INTO admins (actor_did) VALUES ($1) "#, self.actor_did.as_str() ) .execute(&pool) .await .context("Failed to store admin")?; Ok(()) } pub async fn find(did_string: String, pool: &PgPool) -> Result { let result = sqlx::query_scalar!( r#" SELECT EXISTS( SELECT * FROM admins WHERE actor_did = $1 LIMIT 1 ) "#, did_string ) .fetch_one(pool) .await?; match result { Some(result) => Ok(result), None => Ok(false), } } }