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

70 lines
1.6 KiB
Rust
Raw Normal View History

2024-05-25 10:04:05 +00:00
use axum::{extract::State, Json};
use chrono::Local;
2024-05-25 11:45:23 +00:00
use cozo::ScriptMutability;
2024-05-25 10:04:05 +00:00
use serde_json::Value;
use uuid::Uuid;
2024-05-25 11:45:23 +00:00
use crate::{error::AppResult, AppState};
2024-05-25 10:04:05 +00:00
pub async fn get_todays_journal_id(
State(state): State<AppState>,
) -> AppResult<Json<Value>> {
let today = todays_date();
println!("Getting journal id for {today}!");
let result = state.db.run_script(
"
?[node_id] := *journal_days[day, node_id], day = $day
",
btmap! {
"day".to_owned() => today.clone().into(),
},
ScriptMutability::Immutable,
)?;
println!("Result: {:?}", result);
if result.rows.len() == 0 {
// Insert a new one
let uuid = Uuid::now_v7();
let node_id = uuid.to_string();
2024-05-25 11:45:23 +00:00
let _result = state.db.run_script_fold_err(
2024-05-25 10:04:05 +00:00
"
{
2024-05-25 11:45:23 +00:00
?[id, type] <- [[$node_id, 'panorama/journal/page']]
:put node { id, type }
2024-05-25 10:04:05 +00:00
}
{
2024-05-25 11:45:23 +00:00
?[node_id, content] <- [[$node_id, 'Default **content**']]
:put journal { node_id => content }
2024-05-25 10:04:05 +00:00
}
{
?[day, node_id] <- [[$day, $node_id]]
:put journal_days { day => node_id }
}
",
btmap! {
2024-05-25 11:45:23 +00:00
"node_id".to_owned() => node_id.clone().into(),
2024-05-25 10:04:05 +00:00
"day".to_owned() => today.clone().into(),
},
ScriptMutability::Mutable,
);
2024-05-25 11:45:23 +00:00
return Ok(Json(json!({
"node_id": node_id
})));
2024-05-25 10:04:05 +00:00
}
let node_id = result.rows[0][0].get_str().unwrap();
Ok(Json(json!({
"node_id": node_id
})))
}
fn todays_date() -> String {
let now = Local::now();
let date = now.date_naive();
date.format("%Y-%m-%d").to_string()
}