panorama/crates/panorama-daemon/src/node.rs

107 lines
2.5 KiB
Rust
Raw Normal View History

2024-05-25 15:11:34 +00:00
use std::collections::HashMap;
2024-05-25 10:04:05 +00:00
use axum::{
extract::{Path, State},
2024-05-25 11:45:23 +00:00
http::StatusCode,
2024-05-25 10:04:05 +00:00
Json,
};
2024-05-27 02:56:12 +00:00
use cozo::{DataValue, ScriptMutability};
2024-05-25 10:04:05 +00:00
use serde_json::Value;
use crate::{error::AppResult, AppState};
pub async fn get_node(
State(state): State<AppState>,
Path(node_id): Path<String>,
2024-05-25 11:45:23 +00:00
) -> AppResult<(StatusCode, Json<Value>)> {
2024-05-25 10:04:05 +00:00
let result = state.db.run_script(
"
2024-05-25 11:45:23 +00:00
j[content] := *journal{ node_id, content }, node_id = $node_id
j[content] := not *journal{ node_id }, node_id = $node_id, content = null
2024-05-25 10:37:25 +00:00
2024-05-25 16:13:07 +00:00
jd[day] := *journal_day{ node_id, day }, node_id = $node_id
jd[day] := not *journal_day{ node_id }, node_id = $node_id, day = null
2024-05-25 10:37:25 +00:00
2024-05-25 11:45:23 +00:00
?[
2024-05-25 16:13:07 +00:00
extra_data, content, day, created_at, updated_at, type, title
] := *node{ id, type, title, created_at, updated_at, extra_data },
2024-05-25 11:45:23 +00:00
j[content],
2024-05-25 10:37:25 +00:00
jd[day],
id = $node_id
:limit 1
2024-05-25 10:04:05 +00:00
",
btmap! {"node_id".to_owned() => node_id.clone().into()},
ScriptMutability::Immutable,
)?;
2024-05-25 11:45:23 +00:00
if result.rows.len() == 0 {
return Ok((StatusCode::NOT_FOUND, Json(json!(null))));
}
2024-05-25 10:37:25 +00:00
let row = &result.rows[0];
let extra_data = row[0].get_str();
let day = row[2].get_str();
2024-05-25 10:04:05 +00:00
2024-05-25 11:45:23 +00:00
Ok((
StatusCode::OK,
Json(json!({
"node": node_id,
"extra_data": extra_data,
"content": row[1].get_str(),
"day": day,
"created_at": row[3].get_float(),
"updated_at": row[4].get_float(),
"type": row[5].get_str(),
2024-05-25 16:13:07 +00:00
"title": row[6].get_str(),
2024-05-25 11:45:23 +00:00
})),
))
}
2024-05-25 15:11:34 +00:00
#[derive(Deserialize, Debug)]
pub struct UpdateData {
title: Option<String>,
extra_data: Option<HashMap<String, Value>>,
}
2024-05-25 11:45:23 +00:00
pub async fn update_node(
State(state): State<AppState>,
Path(node_id): Path<String>,
2024-05-25 15:11:34 +00:00
Json(update_data): Json<UpdateData>,
2024-05-25 11:45:23 +00:00
) -> AppResult<Json<Value>> {
2024-05-25 15:11:34 +00:00
println!("Update data: {:?}", update_data);
2024-05-27 02:56:12 +00:00
let tx = state.db.multi_transaction(true);
if let Some(extra_data) = update_data.extra_data {
let result = tx.run_script(
"
?[relation, field_name, type] :=
*fqkey_to_dbkey{key, relation, field_name, type},
key = $key
",
btmap! {
"key".to_owned() => DataValue::List(
extra_data
.keys()
.map(|s| DataValue::from(s.as_str()))
.collect::<Vec<_>>()
),
},
)?;
println!("Result: {result:?}");
}
tx.commit()?;
2024-05-25 11:45:23 +00:00
Ok(Json(json!({})))
2024-05-25 10:04:05 +00:00
}
2024-05-25 15:11:34 +00:00
pub async fn node_types() -> AppResult<Json<Value>> {
Ok(Json(json!({
"types": [
{ "id": "panorama/journal/page", "display": "Journal Entry" },
]
})))
}