From 4bf897278a78d9a09d42d291dd21881dd430e452 Mon Sep 17 00:00:00 2001 From: "Philip (a-0)" <@ph:a-0.me> Date: Wed, 24 Jan 2024 19:15:22 +0100 Subject: [PATCH] Added `Ubisync::get_apps()` --- ubisync/src/lib.rs | 6 +++- ubisync/src/state/mod.rs | 6 +++- ubisync/src/state/queries/apps.rs | 54 ++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ubisync/src/lib.rs b/ubisync/src/lib.rs index e83691f..b8d46e5 100644 --- a/ubisync/src/lib.rs +++ b/ubisync/src/lib.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use anyhow::bail; -use api::{Api, ApiBuilder}; +use api::{v0::app::App, Api, ApiBuilder}; use comm::CommHandle; use config::Config; use node_events::UbisyncNodeEvent; @@ -67,6 +67,10 @@ impl Ubisync { self.state_handle.set_peer(&Peer::new(id, "".to_string())) } + pub fn get_apps(&self) -> Vec { + self.state_handle.get_apps().unwrap_or(vec![]) + } + pub fn get_peers(&self) -> Vec { self.state_handle.get_peers().unwrap_or(vec![]) } diff --git a/ubisync/src/state/mod.rs b/ubisync/src/state/mod.rs index 8b77bf4..b8dfefb 100644 --- a/ubisync/src/state/mod.rs +++ b/ubisync/src/state/mod.rs @@ -24,7 +24,7 @@ use ubisync_lib::{ types::{AppId, Element, ElementContent, ElementId, PotId, Tag}, }; -use crate::{comm::CommHandle, node_events::UbisyncNodeEvent, Ubisync}; +use crate::{api::v0::app::App, comm::CommHandle, node_events::UbisyncNodeEvent, Ubisync}; pub struct State { db: DbInstance, @@ -77,6 +77,10 @@ impl State { } } + pub fn get_apps(&self) -> anyhow::Result> { + queries::apps::get_all(&self.db) + } + pub fn set_element_content( &self, element_id: &ElementId, diff --git a/ubisync/src/state/queries/apps.rs b/ubisync/src/state/queries/apps.rs index 0d72660..081a7b3 100644 --- a/ubisync/src/state/queries/apps.rs +++ b/ubisync/src/state/queries/apps.rs @@ -2,8 +2,9 @@ use std::collections::BTreeMap; use anyhow::{bail, Error}; use cozo::{DataValue, DbInstance, Num, ScriptMutability}; +use itertools::Itertools; use serde_with::chrono::{DateTime, Utc}; -use tracing::warn; +use tracing::{debug, warn}; use ubisync_lib::types::{AppId, Pot, PotId}; use crate::{api::v0::app::App, run_query}; @@ -62,6 +63,39 @@ pub fn exists(db: &DbInstance, id: &AppId) -> anyhow::Result { Err(Error::msg("Could not check whether app is registered")) } +pub fn get_all(db: &DbInstance) -> anyhow::Result> { + match db.run_script( + "?[id, last_access, app_type, name, description, default_pot] := *apps[id, last_access, app_type, name, description, default_pot]", + BTreeMap::default(), + ScriptMutability::Immutable) { + Ok(named_rows) => { + Ok(named_rows.rows.iter().filter_map(|row| { + if let [ + DataValue::Str(id), + DataValue::Num(Num::Int(last_access)), + DataValue::Str(app_type), + DataValue::Str(name), + DataValue::Str(description), + default_pot, + ] = row.as_slice() { + Some(App { + id: serde_json::from_str(&id).unwrap(), + app_type: app_type.to_string(), + last_access: DateTime::from_timestamp(last_access.to_owned(), 0).unwrap(), + name: name.to_string(), + description: description.to_string(), + default_pot: default_pot.get_str().map(|str| serde_json::from_str(str).unwrap())} + ) + } + else { + None + } + }).collect_vec()) + }, + Err(e) => bail!(e), + } +} + pub fn get_all_ids(db: &DbInstance) -> anyhow::Result> { let result = db.run_script( "?[id] := *apps{id}", @@ -234,6 +268,7 @@ pub fn create_pot( #[cfg(test)] mod tests { use cozo::DbInstance; + use itertools::Itertools; use serde_with::chrono::Utc; use tracing::{debug, Level}; use ubisync_lib::types::{AppId, PotId}; @@ -257,4 +292,21 @@ mod tests { debug!("Result: {:?}", super::get_default_pot(&db, &app_id)); } + + #[test] + pub fn add_and_get_all() { + tracing_subscriber::fmt() + .pretty() + .with_max_level(Level::DEBUG) + .init(); + + let db = DbInstance::new("mem", "", Default::default()).unwrap(); + schema::add_schema(&db).unwrap(); + super::add(&db, &AppId::new(), &Utc::now(), "app1", "description", "app_type").unwrap(); + super::add(&db, &AppId::new(), &Utc::now(), "app2", "description", "app_type").unwrap(); + let all_ids = super::get_all_ids(&db).unwrap(); + let all_apps = super::get_all(&db).unwrap(); + assert_eq!(all_ids.len(), 2); + assert_eq!(all_ids, all_apps.iter().map(|app| app.id.clone()).collect_vec()); + } }