feat: can get actual available roots
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
7bdf8393b1
commit
816869e6f9
@ -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 }))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {}
|
||||
|
||||
#[allow(dead_code, unused_variables)]
|
||||
impl Querier {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
pub struct Querier {
|
||||
get_available_roots: GetAvailableRoots,
|
||||
}
|
||||
|
||||
pub fn get_available_roots(&self) -> Option<Vec<String>> {
|
||||
todo!()
|
||||
impl Querier {
|
||||
pub fn new(get_available_roots: GetAvailableRoots) -> Self {
|
||||
Self {
|
||||
get_available_roots,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_available_roots(&self) -> anyhow::Result<Option<Vec<String>>> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
pub mod create_item;
|
||||
pub mod create_root;
|
||||
pub mod create_section;
|
||||
|
||||
pub mod get_available_roots;
|
||||
|
54
crates/hyperlog-server/src/services/get_available_roots.rs
Normal file
54
crates/hyperlog-server/src/services/get_available_roots.rs
Normal 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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user