tallyweb_frontend/countable/
server.rs

1use super::CountableStore;
2use leptos::{server, ServerFnError};
3
4#[server(GetCountableStore, "/api/session")]
5pub async fn get_countable_store(user: uuid::Uuid) -> Result<CountableStore, ServerFnError> {
6    use super::{super::api, Countable, CountableId};
7    use std::collections::{HashMap, VecDeque};
8
9    let mut conn = api::extract_pool().await?.begin().await?;
10
11    let mut store: HashMap<CountableId, Countable> = HashMap::new();
12    let mut counters: VecDeque<backend::DbCounter> =
13        backend::counter::all_by_user(&mut conn, user).await?.into();
14    let phases = backend::phase::all_by_user(&mut conn, user).await?;
15
16    while let Some(c) = counters.pop_front() {
17        // TODO: allow parent field on counters
18        store.insert(c.uuid.into(), c.into());
19    }
20
21    for phase in phases {
22        if let Some(parent) = store.get(&phase.parent_uuid.into()) {
23            let uuid = phase.uuid;
24            parent.add_child_checked(uuid.into())?;
25            store.insert(uuid.into(), phase.into());
26        }
27    }
28
29    conn.commit().await?;
30
31    Ok(CountableStore::new(user, store))
32}