2020-08-23 01:55:15 +00:00
|
|
|
use crate::modules::member_management::model::filter_form::FilterForm;
|
2021-01-02 12:19:17 +00:00
|
|
|
use rocket::request::{FormItems, FromForm, FormItem};
|
|
|
|
use rocket::data::FromDataSimple;
|
|
|
|
use rocket::{Request, Data, data, Outcome};
|
|
|
|
use std::io::{Read, Error};
|
|
|
|
use rocket::outcome::Outcome::{Failure, Success};
|
|
|
|
use rocket::http::Status;
|
2020-08-23 01:55:15 +00:00
|
|
|
|
|
|
|
/// FromForm implementation for FilterForms. Unfortunately rocket 0.45 has no support for multi-value elements yet.
|
|
|
|
/// See: https://github.com/SergioBenitez/Rocket/issues/205
|
|
|
|
/// It will be added in rocket 0.5, future coders may refactor this and replace it with the rocket implementation.
|
|
|
|
/// For now I need to use this crap.
|
|
|
|
/// TODO: change error type
|
2021-01-02 12:19:17 +00:00
|
|
|
///
|
|
|
|
const LIMIT: u64 = 2000;
|
|
|
|
impl FromDataSimple for FilterForm {
|
|
|
|
type Error = String;
|
2020-08-23 01:55:15 +00:00
|
|
|
|
2021-01-02 12:19:17 +00:00
|
|
|
fn from_data(req: &Request, data: Data) -> data::Outcome<Self, String> {
|
|
|
|
let mut selected : Vec<String> = vec![];
|
|
|
|
|
|
|
|
match req.raw_query_items(){
|
|
|
|
None => {Outcome::Forward(data)}
|
|
|
|
Some(qi) => {
|
|
|
|
for item in qi {
|
|
|
|
if item.key.as_str() == "selected_groups" {
|
|
|
|
selected.push(String::from(item.value.as_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if selected.len() > 0 {
|
|
|
|
let form = FilterForm {
|
|
|
|
selected_groups: selected
|
|
|
|
};
|
|
|
|
Success(form)
|
|
|
|
} else {
|
|
|
|
Outcome::Forward(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*fn from_data(items: &mut FormItems, strict: bool) -> Result<FilterForm, ()> {
|
2020-08-23 01:55:15 +00:00
|
|
|
let mut selected_groups: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for item in items {
|
|
|
|
match item.key.as_str() {
|
|
|
|
"selected_groups" => {
|
|
|
|
let value = item.value.url_decode().map_err(|_| ())?;
|
|
|
|
selected_groups.push(value);
|
|
|
|
}
|
|
|
|
_ if strict => return Err(()),
|
|
|
|
_ => { /* allow extra value when not strict */ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(FilterForm { selected_groups })
|
|
|
|
}
|
2021-01-02 12:19:17 +00:00
|
|
|
*/
|
2020-08-23 01:55:15 +00:00
|
|
|
}
|