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

73 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(
"
2024-05-25 16:13:07 +00:00
?[node_id] := *journal_day[day, node_id], day = $day
2024-05-25 10:04:05 +00:00
",
btmap! {
"day".to_owned() => today.clone().into(),
},
ScriptMutability::Immutable,
)?;
println!("Result: {:?}", result);
2024-05-27 04:07:02 +00:00
// TODO: Do this check on the server side
2024-05-25 10:04:05 +00:00
if result.rows.len() == 0 {
// Insert a new one
let uuid = Uuid::now_v7();
let node_id = uuid.to_string();
2024-05-25 16:13:07 +00:00
state.db.run_script(
2024-05-25 10:04:05 +00:00
"
{
2024-05-25 16:13:07 +00:00
?[id, title, type] <- [[$node_id, $title, 'panorama/journal/page']]
:put node { id, title, type }
2024-05-25 10:04:05 +00:00
}
{
2024-05-27 04:07:02 +00:00
?[node_id, content] <- [[$node_id, '']]
2024-05-25 11:45:23 +00:00
:put journal { node_id => content }
2024-05-25 10:04:05 +00:00
}
{
?[day, node_id] <- [[$day, $node_id]]
2024-05-25 16:13:07 +00:00
:put journal_day { day => node_id }
2024-05-25 10:04:05 +00:00
}
",
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(),
2024-05-25 16:13:07 +00:00
"title".to_owned() => today.clone().into(),
2024-05-25 10:04:05 +00:00
},
ScriptMutability::Mutable,
2024-05-25 16:13:07 +00:00
)?;
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!({
2024-05-25 16:13:07 +00:00
"node_id": node_id,
"day": today,
2024-05-25 10:04:05 +00:00
})))
}
fn todays_date() -> String {
let now = Local::now();
let date = now.date_naive();
date.format("%Y-%m-%d").to_string()
}