this repo has no description
at main 53 lines 1.2 kB view raw
1use anyhow::Context; 2use atrium_api::types::string::Did; 3use sqlx::PgPool; 4 5pub struct Admin { 6 actor_did: Did, 7} 8 9impl Admin { 10 pub fn new(did_string: String) -> Result<Self, anyhow::Error> { 11 let actor_did = match Did::new(did_string) { 12 Ok(did) => did, 13 Err(err) => anyhow::bail!("{err}"), 14 }; 15 16 Ok(Self { actor_did }) 17 } 18 19 pub async fn store(&self, pool: PgPool) -> Result<(), anyhow::Error> { 20 sqlx::query!( 21 r#" 22 INSERT INTO admins (actor_did) VALUES ($1) 23 "#, 24 self.actor_did.as_str() 25 ) 26 .execute(&pool) 27 .await 28 .context("Failed to store admin")?; 29 30 Ok(()) 31 } 32 33 pub async fn find(did_string: String, pool: &PgPool) -> Result<bool, anyhow::Error> { 34 let result = sqlx::query_scalar!( 35 r#" 36 SELECT EXISTS( 37 SELECT * 38 FROM admins 39 WHERE actor_did = $1 40 LIMIT 1 41 ) 42 "#, 43 did_string 44 ) 45 .fetch_one(pool) 46 .await?; 47 48 match result { 49 Some(result) => Ok(result), 50 None => Ok(false), 51 } 52 } 53}