/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin */ use super::{ account::session::Session, error::MyError, layout::DynLayoutPage, node::NodeCard, sort::{filter_and_sort_nodes, NodeFilterSort, NodeFilterSortForm, SortOrder, SortProperty}, }; use crate::{database::DataAcid, uri}; use jellybase::database::{ReadableTable, T_NODE, T_USER_NODE}; use rocket::{get, State}; /// This function is a stub and only useful for use in the uri! macro. #[get("/items")] pub fn r_all_items() {} #[get("/items?&")] pub fn r_all_items_filter( sess: Session, db: &State, page: Option, filter: NodeFilterSort, ) -> Result, MyError> { let mut items = { let txn = db.begin_read()?; let nodes = txn.open_table(T_NODE)?; let node_users = txn.open_table(T_USER_NODE)?; let i = nodes .iter()? .map(|a| { let (x, y) = a.unwrap(); let (x, y) = (x.value().to_owned(), y.value().0); let z = node_users .get(&(sess.user.name.as_str(), x.as_str())) .unwrap() .map(|z| z.value().0) .unwrap_or_default(); let y = y.public; (x, y, z) }) .collect::>(); drop(nodes); i }; filter_and_sort_nodes( &filter, (SortProperty::Title, SortOrder::Ascending), &mut items, ); let page_size = 100; let page = page.unwrap_or(0); let offset = page * page_size; let from = offset.min(items.len()); let to = (offset + page_size).min(items.len()); let max_page = items.len().div_ceil(page_size); Ok(super::layout::LayoutPage { title: "All Items".to_owned(), content: markup::new! { .page.dir { h1 { "All Items" } @NodeFilterSortForm { f: &filter } ul.children { @for (id, node, udata) in &items[from..to] { li {@NodeCard { id, node, udata }} }} p.pagecontrols { span.current { "Page " @{page + 1} " of " @max_page " " } @if page > 0 { a.prev[href=uri!(r_all_items_filter(Some(page - 1), filter.clone()))] { "Previous page" } " " } @if page + 1 < max_page { a.next[href=uri!(r_all_items_filter(Some(page + 1), filter.clone()))] { "Next page" } } } } }, ..Default::default() }) }