|
|
@ -30,16 +30,33 @@ use indexmap::map::IndexMap; |
|
|
|
pub struct ListContext<'a> { |
|
|
|
pub struct ListContext<'a> { |
|
|
|
pub fields: Vec<RenderedField<'a>>, |
|
|
|
pub fields: Vec<RenderedField<'a>>, |
|
|
|
pub cards: Vec<RenderedCard<'a>>, |
|
|
|
pub cards: Vec<RenderedCard<'a>>, |
|
|
|
|
|
|
|
pub page: usize, |
|
|
|
|
|
|
|
pub pages: usize, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[get("/")] |
|
|
|
#[get("/?<page>&<card>")] |
|
|
|
fn route_index(store: State<RwLock<Store>>) -> Template { |
|
|
|
fn route_index(store: State<RwLock<Store>>, page: Option<usize>, card: Option<usize>) -> Template { |
|
|
|
let rg = store.read(); |
|
|
|
let rg = store.read(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let per_page: usize = 20; |
|
|
|
|
|
|
|
let mut page = page.unwrap_or_default(); |
|
|
|
|
|
|
|
let n_pages = (rg.data.cards.len() as f64 / per_page as f64).ceil() as usize; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(card_id) = card { |
|
|
|
|
|
|
|
if let Some((n, _)) = rg.data.cards.iter().enumerate().find(|(_n, (id, _card))| **id == card_id) { |
|
|
|
|
|
|
|
page = n / per_page; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let context = ListContext { |
|
|
|
let context = ListContext { |
|
|
|
fields: render_empty_fields(&rg), |
|
|
|
fields: render_empty_fields(&rg), |
|
|
|
cards: rg.data.cards.iter().filter_map(|(id, card)| { |
|
|
|
pages: n_pages, |
|
|
|
if let Value::Object(map) = card { |
|
|
|
page, |
|
|
|
|
|
|
|
cards: rg.data.cards.iter() |
|
|
|
|
|
|
|
.skip(page * per_page) |
|
|
|
|
|
|
|
.take(per_page) |
|
|
|
|
|
|
|
.filter_map(|(id, card)| { |
|
|
|
|
|
|
|
if let Value::Object(ref map) = card { |
|
|
|
Some(RenderedCard { |
|
|
|
Some(RenderedCard { |
|
|
|
fields: render_card_fields(&rg, map), |
|
|
|
fields: render_card_fields(&rg, map), |
|
|
|
id: *id, |
|
|
|
id: *id, |
|
|
@ -198,16 +215,16 @@ fn route_add_save(form: Form<MapFromForm>, store: State<RwLock<Store>>) -> Redir |
|
|
|
let mut rg = store.write(); |
|
|
|
let mut rg = store.write(); |
|
|
|
|
|
|
|
|
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
rg.add_card(card); |
|
|
|
let id = rg.add_card(card); |
|
|
|
|
|
|
|
|
|
|
|
Redirect::found(uri!(route_index)) |
|
|
|
Redirect::found(uri!(route_index: page=_, card=id)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[get("/edit/<id>")] |
|
|
|
#[get("/edit/<id>")] |
|
|
|
fn route_edit(id: usize, store: State<RwLock<Store>>) -> Option<Template> { |
|
|
|
fn route_edit(id: usize, store: State<RwLock<Store>>) -> Option<Template> { |
|
|
|
let rg = store.read(); |
|
|
|
let rg = store.read(); |
|
|
|
|
|
|
|
|
|
|
|
if let Some(Value::Object(map)) = rg.data.cards.get(&id) { |
|
|
|
if let Some(Value::Object(ref map)) = rg.data.cards.get(&id) { |
|
|
|
let context = EditCardContext { |
|
|
|
let context = EditCardContext { |
|
|
|
fields: render_card_fields(&rg, map), |
|
|
|
fields: render_card_fields(&rg, map), |
|
|
|
id, |
|
|
|
id, |
|
|
@ -226,7 +243,16 @@ fn route_edit_save(id : usize, form: Form<MapFromForm>, store: State<RwLock<Stor |
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
rg.update_card(id, card); |
|
|
|
rg.update_card(id, card); |
|
|
|
|
|
|
|
|
|
|
|
Some(Redirect::found(uri!(route_index))) |
|
|
|
Some(Redirect::found(uri!(route_index: page=_, card=id))) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/delete/<id>")] |
|
|
|
|
|
|
|
fn route_delete(id: usize, store: State<RwLock<Store>>) -> Redirect { |
|
|
|
|
|
|
|
let mut rg = store.write(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rg.delete_card(id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Redirect::found(uri!(route_index: page=_, card=_)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn main() { |
|
|
|
fn main() { |
|
|
@ -244,5 +270,6 @@ fn main() { |
|
|
|
route_add_save, |
|
|
|
route_add_save, |
|
|
|
route_edit, |
|
|
|
route_edit, |
|
|
|
route_edit_save, |
|
|
|
route_edit_save, |
|
|
|
|
|
|
|
route_delete, |
|
|
|
]).launch(); |
|
|
|
]).launch(); |
|
|
|
} |
|
|
|
} |
|
|
|