|
|
|
@ -14,13 +14,11 @@ use rocket_contrib::templates::Template; |
|
|
|
|
|
|
|
|
|
mod store; |
|
|
|
|
|
|
|
|
|
use crate::store::form::{render_card_fields, render_empty_fields, RenderedCard, RenderedField}; |
|
|
|
|
use crate::store::model::FieldKind; |
|
|
|
|
use crate::store::form::{render_card_fields, render_empty_fields, RenderedCard, RenderedField, MapFromForm, collect_card_form}; |
|
|
|
|
use crate::store::Store; |
|
|
|
|
use indexmap::map::IndexMap; |
|
|
|
|
use parking_lot::RwLock; |
|
|
|
|
|
|
|
|
|
use rocket::request::{Form, FormItems, FromForm}; |
|
|
|
|
use rocket::request::Form; |
|
|
|
|
use rocket::response::Redirect; |
|
|
|
|
use rocket::State; |
|
|
|
|
use std::env; |
|
|
|
@ -33,7 +31,7 @@ pub struct ListContext<'a> { |
|
|
|
|
pub pages: usize, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const per_page: usize = 20; |
|
|
|
|
const PER_PAGE: usize = 20; // TODO configurable
|
|
|
|
|
|
|
|
|
|
fn find_page_with_card(store: &Store, card_id: usize) -> Option<usize> { |
|
|
|
|
if let Some((n, _)) = store |
|
|
|
@ -43,22 +41,18 @@ fn find_page_with_card(store: &Store, card_id: usize) -> Option<usize> { |
|
|
|
|
.enumerate() |
|
|
|
|
.find(|(_n, (id, _card))| **id == card_id) |
|
|
|
|
{ |
|
|
|
|
Some(n / per_page) |
|
|
|
|
Some(n / PER_PAGE) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[get("/?<page>&<card>")] |
|
|
|
|
fn route_index(store: State<RwLock<Store>>, page: Option<usize>, card: Option<usize>) -> Template { |
|
|
|
|
#[get("/?<page>")] |
|
|
|
|
fn route_index(store: State<RwLock<Store>>, page: Option<usize>) -> Template { |
|
|
|
|
let rg = store.read(); |
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
page = find_page_with_card(&rg, card_id).unwrap_or(page); |
|
|
|
|
} |
|
|
|
|
let n_pages = (rg.data.cards.len() as f64 / PER_PAGE as f64).ceil() as usize; |
|
|
|
|
|
|
|
|
|
if page >= n_pages { |
|
|
|
|
page = n_pages - 1; |
|
|
|
@ -72,8 +66,8 @@ fn route_index(store: State<RwLock<Store>>, page: Option<usize>, card: Option<us |
|
|
|
|
.data |
|
|
|
|
.cards |
|
|
|
|
.iter() |
|
|
|
|
.skip(page * per_page) |
|
|
|
|
.take(per_page) |
|
|
|
|
.skip(page * PER_PAGE) |
|
|
|
|
.take(PER_PAGE) |
|
|
|
|
.filter_map(|(id, card)| { |
|
|
|
|
if let Value::Object(ref map) = card { |
|
|
|
|
Some(RenderedCard { |
|
|
|
@ -90,123 +84,6 @@ fn route_index(store: State<RwLock<Store>>, page: Option<usize>, card: Option<us |
|
|
|
|
Template::render("index", context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Default)] |
|
|
|
|
struct MapFromForm { |
|
|
|
|
pub data: IndexMap<String, String>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> FromForm<'a> for MapFromForm { |
|
|
|
|
type Error = (); |
|
|
|
|
|
|
|
|
|
fn from_form(items: &mut FormItems, _strict: bool) -> Result<Self, Self::Error> { |
|
|
|
|
let mut new = MapFromForm::default(); |
|
|
|
|
items.for_each(|item| { |
|
|
|
|
let (k, v) = item.key_value_decoded(); |
|
|
|
|
new.data.insert(k, v); |
|
|
|
|
}); |
|
|
|
|
Ok(new) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap<String, Value> { |
|
|
|
|
let mut card = IndexMap::new(); |
|
|
|
|
|
|
|
|
|
for (k, field) in &store.model.fields { |
|
|
|
|
let mut value: Option<Value> = None; |
|
|
|
|
if let Some(input) = form.data.remove(k) { |
|
|
|
|
value = Some(match &field.kind { |
|
|
|
|
FieldKind::Text | FieldKind::String | FieldKind::FreeEnum { .. } => { |
|
|
|
|
Value::String(input) |
|
|
|
|
} |
|
|
|
|
FieldKind::Bool { .. } => serde_json::to_value(true).unwrap(), |
|
|
|
|
FieldKind::Int { min, max, default } => { |
|
|
|
|
let mut val: i64 = if input.is_empty() { |
|
|
|
|
*default |
|
|
|
|
} else { |
|
|
|
|
input.parse().expect("Error parse number") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if let Some(min) = min { |
|
|
|
|
val = val.max(*min); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let Some(max) = max { |
|
|
|
|
val = val.min(*max); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
serde_json::to_value(val).unwrap() |
|
|
|
|
} |
|
|
|
|
FieldKind::Float { min, max, default } => { |
|
|
|
|
let mut val: f64 = if input.is_empty() { |
|
|
|
|
*default |
|
|
|
|
} else { |
|
|
|
|
input.parse().expect("Error parse number") |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if let Some(min) = min { |
|
|
|
|
val = val.min(*min); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let Some(max) = max { |
|
|
|
|
val = val.max(*max); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
serde_json::to_value(val).unwrap() |
|
|
|
|
} |
|
|
|
|
FieldKind::Enum { options, default } => { |
|
|
|
|
if options.contains(&input) { |
|
|
|
|
Value::String(input) |
|
|
|
|
} else { |
|
|
|
|
let val = default.as_ref().map(ToOwned::to_owned).unwrap_or_else(|| { |
|
|
|
|
options |
|
|
|
|
.first() |
|
|
|
|
.expect("fixed enum must have values") |
|
|
|
|
.to_owned() |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
Value::String(val) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
FieldKind::Tags { options } => { |
|
|
|
|
let tags: Vec<String> = input |
|
|
|
|
.split(' ') |
|
|
|
|
.map(ToOwned::to_owned) |
|
|
|
|
.filter_map(|tag| { |
|
|
|
|
if options.contains(&tag) { |
|
|
|
|
Some(tag) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
serde_json::to_value(tags).unwrap() |
|
|
|
|
} |
|
|
|
|
FieldKind::FreeTags { .. } => { |
|
|
|
|
let tags: Vec<String> = input |
|
|
|
|
.split(' ') |
|
|
|
|
.map(str::trim) |
|
|
|
|
.filter(|s| !s.is_empty()) |
|
|
|
|
.map(ToOwned::to_owned) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
serde_json::to_value(tags).unwrap() |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
if let FieldKind::Bool { .. } = field.kind { |
|
|
|
|
value = Some(Value::Bool(false)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let Some(v) = value { |
|
|
|
|
card.insert(k.to_owned(), v); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
card |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Serialize)] |
|
|
|
|
struct AddCardContext<'a> { |
|
|
|
|
pub fields: Vec<RenderedField<'a>>, |
|
|
|
@ -231,12 +108,13 @@ fn route_add(store: State<RwLock<Store>>) -> Template { |
|
|
|
|
|
|
|
|
|
#[post("/add", data = "<form>")] |
|
|
|
|
fn route_add_save(form: Form<MapFromForm>, store: State<RwLock<Store>>) -> Redirect { |
|
|
|
|
let mut rg = store.write(); |
|
|
|
|
let mut wg = store.write(); |
|
|
|
|
|
|
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
|
let id = rg.add_card(card); |
|
|
|
|
let card = collect_card_form(&wg, form.into_inner()); |
|
|
|
|
let id = wg.add_card(card); |
|
|
|
|
|
|
|
|
|
Redirect::found(uri!(route_index: page=_, card=id)) |
|
|
|
|
let page = find_page_with_card(&wg, id).unwrap_or(0); |
|
|
|
|
Redirect::found(uri!(route_index: page)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[get("/edit/<id>")] |
|
|
|
@ -261,23 +139,32 @@ fn route_edit_save( |
|
|
|
|
form: Form<MapFromForm>, |
|
|
|
|
store: State<RwLock<Store>>, |
|
|
|
|
) -> Option<Redirect> { |
|
|
|
|
let mut rg = store.write(); |
|
|
|
|
let mut wg = store.write(); |
|
|
|
|
|
|
|
|
|
let card = collect_card_form(&rg, form.into_inner()); |
|
|
|
|
rg.update_card(id, card); |
|
|
|
|
let card = collect_card_form(&wg, form.into_inner()); |
|
|
|
|
wg.update_card(id, card); |
|
|
|
|
|
|
|
|
|
Some(Redirect::found(uri!(route_index: page=_, card=id))) |
|
|
|
|
let page = find_page_with_card(&wg, id).unwrap_or(0); |
|
|
|
|
Some(Redirect::found(uri!(route_index: page))) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[get("/delete/<id>")] |
|
|
|
|
fn route_delete(id: usize, store: State<RwLock<Store>>) -> Redirect { |
|
|
|
|
let mut rg = store.write(); |
|
|
|
|
let mut wg = store.write(); |
|
|
|
|
|
|
|
|
|
let page = find_page_with_card(&rg, id).unwrap_or(0); |
|
|
|
|
// must find page before deleting
|
|
|
|
|
let page = find_page_with_card(&wg, id).unwrap_or(0); |
|
|
|
|
wg.delete_card(id); |
|
|
|
|
|
|
|
|
|
rg.delete_card(id); |
|
|
|
|
Redirect::found(uri!(route_index: page)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Redirect::found(uri!(route_index: page=page, card=_)) |
|
|
|
|
#[get("/maintenance/reindex")] |
|
|
|
|
fn route_reindex(store: State<RwLock<Store>>) -> Redirect { |
|
|
|
|
let mut wg = store.write(); |
|
|
|
|
wg.rebuild_indexes(); |
|
|
|
|
wg.persist(); |
|
|
|
|
Redirect::found(uri!(route_index: _)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn main() { |
|
|
|
@ -298,6 +185,7 @@ fn main() { |
|
|
|
|
route_edit, |
|
|
|
|
route_edit_save, |
|
|
|
|
route_delete, |
|
|
|
|
route_reindex, |
|
|
|
|
], |
|
|
|
|
) |
|
|
|
|
.launch(); |
|
|
|
|