Files
hyperlog/crates/hyperlog-server/src/services/archive.rs
kjuulh 9587c60e72
Some checks failed
continuous-integration/drone/push Build is failing
feat: add archive sub command
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-06-01 13:13:18 +02:00

71 lines
1.3 KiB
Rust

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