hyperlog/crates/hyperlog-server/src/services/get_available_roots.rs
kjuulh 91ad8eda01
All checks were successful
continuous-integration/drone/push Build is passing
chore: remove unused functions and fix warnings
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-05-15 15:10:35 +02:00

52 lines
999 B
Rust

use crate::state::SharedState;
#[derive(Clone)]
pub struct GetAvailableRoots {
db: sqlx::PgPool,
}
pub struct Request {}
pub struct Response {
pub roots: Vec<String>,
}
#[derive(sqlx::FromRow)]
pub struct Root {
root_name: String,
}
impl GetAvailableRoots {
pub fn new(db: sqlx::PgPool) -> Self {
Self { db }
}
pub async fn execute(&self, _req: Request) -> anyhow::Result<Response> {
let roots: Vec<Root> = sqlx::query_as(
r#"
SELECT
*
FROM
roots
LIMIT
100
"#,
)
.fetch_all(&self.db)
.await?;
Ok(Response {
roots: roots.into_iter().map(|i| i.root_name).collect(),
})
}
}
pub trait GetAvailableRootsExt {
fn get_available_roots_service(&self) -> GetAvailableRoots;
}
impl GetAvailableRootsExt for SharedState {
fn get_available_roots_service(&self) -> GetAvailableRoots {
GetAvailableRoots::new(self.db.clone())
}
}