feat: can get actual available roots
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-14 20:58:38 +02:00
parent 7bdf8393b1
commit 816869e6f9
4 changed files with 95 additions and 11 deletions

View File

@@ -0,0 +1,54 @@
use hyperlog_core::log::{GraphItem, ItemState};
use sqlx::types::Json;
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())
}
}