Added Ubisync::get_apps()

This commit is contained in:
Philip (a-0) 2024-01-24 19:15:22 +01:00
parent d258060769
commit 4bf897278a
3 changed files with 63 additions and 3 deletions

View file

@ -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<App> {
self.state_handle.get_apps().unwrap_or(vec![])
}
pub fn get_peers(&self) -> Vec<Peer> {
self.state_handle.get_peers().unwrap_or(vec![])
}

View file

@ -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<Vec<App>> {
queries::apps::get_all(&self.db)
}
pub fn set_element_content(
&self,
element_id: &ElementId,

View file

@ -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<bool> {
Err(Error::msg("Could not check whether app is registered"))
}
pub fn get_all(db: &DbInstance) -> anyhow::Result<Vec<App>> {
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<Vec<AppId>> {
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());
}
}