feat: add archive sub command
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-06-01 13:13:18 +02:00
parent 710fb431f7
commit 9587c60e72
17 changed files with 232 additions and 43 deletions

View File

@@ -2,6 +2,7 @@ use hyperlog_core::log::ItemState;
use crate::{
services::{
archive::{self, Archive, ArchiveExt},
create_item::{self, CreateItem, CreateItemExt},
create_root::{self, CreateRoot, CreateRootExt},
create_section::{self, CreateSection, CreateSectionExt},
@@ -43,6 +44,10 @@ pub enum Command {
src: Vec<String>,
dest: Vec<String>,
},
Archive {
root: String,
path: Vec<String>,
},
}
#[allow(dead_code)]
@@ -52,6 +57,7 @@ pub struct Commander {
create_item: CreateItem,
update_item: UpdateItem,
toggle_item: ToggleItem,
archive: Archive,
}
impl Commander {
@@ -61,6 +67,7 @@ impl Commander {
create_item: CreateItem,
update_item: UpdateItem,
toggle_item: ToggleItem,
archive: Archive,
) -> Self {
Self {
create_root,
@@ -68,6 +75,7 @@ impl Commander {
create_item,
update_item,
toggle_item,
archive,
}
}
@@ -133,6 +141,13 @@ impl Commander {
Ok(())
}
Command::Move { .. } => todo!(),
Command::Archive { root, path } => {
self.archive
.execute(archive::Request { root, path })
.await?;
Ok(())
}
}
}
}
@@ -149,6 +164,7 @@ impl CommanderExt for SharedState {
self.create_item_service(),
self.update_item_service(),
self.toggle_item_service(),
self.archive_service(),
)
}
}

View File

@@ -380,6 +380,38 @@ impl Graph for Server {
Ok(Response::new(ToggleItemResponse {}))
}
async fn archive(
&self,
request: tonic::Request<ArchiveRequest>,
) -> std::result::Result<tonic::Response<ArchiveResponse>, tonic::Status> {
let req = request.into_inner();
tracing::trace!("update item: req({:?})", req);
if req.root.is_empty() {
return Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"root cannot be empty".to_string(),
));
}
if req.path.is_empty() {
return Err(tonic::Status::new(
tonic::Code::InvalidArgument,
"path cannot be empty".to_string(),
));
}
self.commander
.execute(Command::Archive {
root: req.root,
path: req.path,
})
.await
.map_err(to_tonic_err)?;
Ok(Response::new(ArchiveResponse {}))
}
}
fn to_native(from: &hyperlog_core::log::GraphItem) -> anyhow::Result<GraphItem> {

View File

@@ -1,3 +1,4 @@
pub mod archive;
pub mod create_item;
pub mod create_root;
pub mod create_section;

View File

@@ -0,0 +1,70 @@
use crate::state::SharedState;
#[derive(Clone)]
pub struct Archive {
db: sqlx::PgPool,
}
pub struct Request {
pub root: String,
pub path: Vec<String>,
}
pub struct Response {}
#[derive(sqlx::FromRow)]
struct Root {
id: uuid::Uuid,
}
impl Archive {
pub fn new(db: sqlx::PgPool) -> Self {
Self { db }
}
pub async fn execute(&self, req: Request) -> anyhow::Result<Response> {
let Root { id: root_id, .. } =
sqlx::query_as(r#"SELECT * FROM roots WHERE root_name = $1"#)
.bind(req.root)
.fetch_one(&self.db)
.await?;
sqlx::query(
r#"
UPDATE nodes
SET status = 'archive'
WHERE
root_id = $1
AND path = $2;
"#,
)
.bind(root_id)
.bind(req.path.join("."))
.execute(&self.db)
.await?;
sqlx::query(
r#"
UPDATE nodes
SET status = 'archive'
WHERE root_id = $1
AND path LIKE $2;
"#,
)
.bind(root_id)
.bind(format!("{}.%", req.path.join(".")))
.execute(&self.db)
.await?;
Ok(Response {})
}
}
pub trait ArchiveExt {
fn archive_service(&self) -> Archive;
}
impl ArchiveExt for SharedState {
fn archive_service(&self) -> Archive {
Archive::new(self.db.clone())
}
}

View File

@@ -60,6 +60,7 @@ impl GetGraph {
nodes
WHERE
root_id = $1
AND status = 'active'
LIMIT
1000
"#,