diff --git a/crates/hyperlog-server/src/external_grpc.rs b/crates/hyperlog-server/src/external_grpc.rs index d04c595..0f34042 100644 --- a/crates/hyperlog-server/src/external_grpc.rs +++ b/crates/hyperlog-server/src/external_grpc.rs @@ -225,9 +225,22 @@ impl Graph for Server { let req = request.into_inner(); tracing::trace!("get available roots: req({:?})", req); - Ok(Response::new(GetAvailableRootsResponse { - roots: vec!["kjuulh".into()], - })) + let roots = match self + .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 })) } } diff --git a/crates/hyperlog-server/src/querier.rs b/crates/hyperlog-server/src/querier.rs index 50b4714..4bf4a20 100644 --- a/crates/hyperlog-server/src/querier.rs +++ b/crates/hyperlog-server/src/querier.rs @@ -1,17 +1,32 @@ 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 { - pub fn new() -> Self { - Self {} + pub fn new(get_available_roots: GetAvailableRoots) -> Self { + Self { + get_available_roots, + } } - pub fn get_available_roots(&self) -> Option> { - todo!() + pub async fn get_available_roots(&self) -> anyhow::Result>> { + 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( @@ -29,6 +44,6 @@ pub trait QuerierExt { impl QuerierExt for SharedState { fn querier(&self) -> Querier { - Querier::new() + Querier::new(self.get_available_roots_service()) } } diff --git a/crates/hyperlog-server/src/services.rs b/crates/hyperlog-server/src/services.rs index 2e24ae2..8af67a5 100644 --- a/crates/hyperlog-server/src/services.rs +++ b/crates/hyperlog-server/src/services.rs @@ -1,3 +1,5 @@ pub mod create_item; pub mod create_root; pub mod create_section; + +pub mod get_available_roots; diff --git a/crates/hyperlog-server/src/services/get_available_roots.rs b/crates/hyperlog-server/src/services/get_available_roots.rs new file mode 100644 index 0000000..df352ed --- /dev/null +++ b/crates/hyperlog-server/src/services/get_available_roots.rs @@ -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, +} + +#[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 { + let roots: Vec = 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()) + } +}