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:
Kasper Juul Hermansen 2024-05-14 20:58:38 +02:00
parent 7bdf8393b1
commit 816869e6f9
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 95 additions and 11 deletions

View File

@ -225,9 +225,22 @@ impl Graph for Server {
let req = request.into_inner(); let req = request.into_inner();
tracing::trace!("get available roots: req({:?})", req); tracing::trace!("get available roots: req({:?})", req);
Ok(Response::new(GetAvailableRootsResponse { let roots = match self
roots: vec!["kjuulh".into()], .querier
})) .get_available_roots()
.await
.map_err(to_tonic_err)?
{
Some(roots) => roots,
None => {
return Err(tonic::Status::new(
tonic::Code::NotFound,
"failed to find any valid roots",
))
}
};
Ok(Response::new(GetAvailableRootsResponse { roots }))
} }
} }

View File

@ -1,17 +1,32 @@
use hyperlog_core::log::GraphItem; use hyperlog_core::log::GraphItem;
use crate::state::SharedState; use crate::{
services::get_available_roots::{self, GetAvailableRoots, GetAvailableRootsExt},
state::SharedState,
};
pub struct Querier {} pub struct Querier {
get_available_roots: GetAvailableRoots,
}
#[allow(dead_code, unused_variables)]
impl Querier { impl Querier {
pub fn new() -> Self { pub fn new(get_available_roots: GetAvailableRoots) -> Self {
Self {} Self {
get_available_roots,
}
} }
pub fn get_available_roots(&self) -> Option<Vec<String>> { pub async fn get_available_roots(&self) -> anyhow::Result<Option<Vec<String>>> {
todo!() let res = self
.get_available_roots
.execute(get_available_roots::Request {})
.await?;
if res.roots.is_empty() {
return Ok(None);
}
Ok(Some(res.roots))
} }
pub fn get( pub fn get(
@ -29,6 +44,6 @@ pub trait QuerierExt {
impl QuerierExt for SharedState { impl QuerierExt for SharedState {
fn querier(&self) -> Querier { fn querier(&self) -> Querier {
Querier::new() Querier::new(self.get_available_roots_service())
} }
} }

View File

@ -1,3 +1,5 @@
pub mod create_item; pub mod create_item;
pub mod create_root; pub mod create_root;
pub mod create_section; pub mod create_section;
pub mod get_available_roots;

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())
}
}