Lots of refactoring. More, smaller files.

This commit is contained in:
Johannes Randerath
2024-08-29 16:30:19 +02:00
parent 36e0ec3ad1
commit 9dafd7504b
15 changed files with 1052 additions and 760 deletions

View File

@@ -68,6 +68,11 @@ async fn login_home() -> Redirect {
Redirect::to(uri!(auth::oauth_login()))
}
#[get("/favicon.ico")]
async fn favicon() -> Redirect {
Redirect::to(uri!("/img/favicon.ico"))
}
#[launch]
async fn rocket() -> _ {
dotenv().ok();
@@ -80,12 +85,14 @@ async fn rocket() -> _ {
.attach(Template::fairing())
.attach(Db::fairing())
.attach(OAuth2::<auth::RanderathIdentity>::fairing("oauth"))
.mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home])
.mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create, table::import_table, table::delete_table])
.mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon])
.mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create_table, table::import_table, table::delete_table])
.mount("/row", routes![table::new_entry, table::edit_entry, table::delete_entry])
.mount("/column", routes![table::delete_column, table::edit_column])
.register("/", catchers![auth::redirect_to_login])
.mount("/static", FileServer::from(relative!("static")))
.mount("/img", FileServer::from(relative!("static/img")))
.mount("/css", FileServer::from(relative!("static/css")))
.mount("/js", FileServer::from(relative!("static/js")))
}

View File

@@ -1,4 +1,5 @@
/* Simple web app using rocket to help maintain inventory data.
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
@@ -15,290 +16,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::auth;
pub mod table_view;
pub mod table_manipulate_table;
pub mod table_manipulate_column;
pub mod table_manipulate_entry;
pub mod forms;
use auth::AuthUser;
use crate::Db;
pub use self::table_view::*;
pub use self::table_manipulate_table::*;
pub use self::table_manipulate_entry::*;
pub use self::table_manipulate_column::*;
pub use self::forms::*;
use rocket_dyn_templates::{Template, context};
use rocket::form::Form;
use rocket::response::Redirect;
use inventur_db;
use rocket::serde::{Serialize, Deserialize, json::Json};
use rocket::http::{Status, Cookie, CookieJar, SameSite};
use inventur_db::FIELDTYPE;
#[derive(FromForm)]
struct DeleteColumn {
tblid: i32,
id_in_table: i32,
}
#[derive(FromForm)]
struct EditColumn {
tblid: i32,
id_in_table: i32,
name: String,
column_type: isize,
}
#[derive(FromForm)]
struct NewColumn {
tblid: i32,
name: String,
column_type: isize,
}
#[derive(FromForm)]
struct NewEntry {
tblid: i32,
cells: Vec<String>,
}
#[derive(FromForm)]
struct EditEntry {
tblid: i32,
cells: Vec<String>,
row_pos: i32,
}
#[derive(FromForm)]
struct DeleteEntry {
tblid: i32,
row_pos: i32,
}
#[derive(FromForm)]
struct EditTname {
tblid: i32,
new_name: String,
}
#[derive(FromForm)]
struct NewTable {
name: String,
field_names: Vec<String>,
field_types: Vec<isize>,
}
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct ImportTable {
name: String,
columns: Vec<String>,
rows: Vec<Vec<String>>,
}
#[derive(FromForm)]
struct DeleteTable {
tblid: i32,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct JRows {
rows: Vec<Vec<String>>,
}
pub async fn get_tids(conn: &Db, uid: i32) -> (Vec<i32>, Vec<String>) {
let mut tids = conn.run(move |c| inventur_db::get_user_tblids(c, uid)).await;
let tnames;
if tids.is_none() {
tids = Some(Vec::new());
tnames = Some(Vec::new());
}else {
let tids = tids.clone().unwrap();
tnames = conn.run(move |c| inventur_db::get_tblnames(c, tids)).await;
}
(tids.unwrap(), tnames.unwrap())
}
#[get("/<tid>", rank=2)]
pub async fn table_sec(conn: Db, tid: i32, user: AuthUser) -> Redirect {
let nus : Option<usize> = None;
let nu8 : Option<u8> = None;
let nvi32 : Option<Vec<i32>> = None;
let ns : Option<String> = None;
Redirect::to(uri!("/table", table(tid, nu8, nus, nvi32, ns)))
}
#[get("/<tid>?<sort_dir>&<sort_field>&<search_fields>&<search_value>")]
pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<usize>, search_fields: Option<Vec<i32>>, search_value: Option<String>, user: AuthUser) -> Option<Template> {
let uid = user.uid;
let table = conn.run(move |c| inventur_db::get_table(c, tid, uid)).await;
if table.is_none() {
return None;
}
let mut table = table.unwrap();
let searchvalue;
let searchfields;
if search_value.is_some() && !search_value.clone().unwrap().eq_ignore_ascii_case("") && search_fields.is_some() {
searchvalue = search_value.unwrap();
searchfields = search_fields.unwrap();
table = inventur_db::search_table(table, searchfields.clone(), searchvalue.clone());
}else {
if search_value.is_none() {
searchvalue = String::new();
} else {
searchvalue = search_value.unwrap();
}
if search_fields.is_none() {
searchfields = (0i32..(table.column_names.len() as i32)).collect();
} else {
searchfields = search_fields.unwrap();
}
}
let sortdir;
if sort_dir.is_none() || sort_dir.unwrap() > 1 {
sortdir = 0;
}else {
sortdir = sort_dir.unwrap();
}
let sortfield;
if sort_field.is_none() {
sortfield = 0;
}else {
sortfield = sort_field.unwrap();
}
let table = inventur_db::sort_table(table, sortfield, sortdir);
let tname = table.name;
let column_names = table.column_names;
let column_types = table.column_types.iter().map(|x: &inventur_db::FIELDTYPE| *x as i32).collect::<Vec<i32>>();
let rows : Vec<Vec<String>>= table.rows.iter().map(|v| {let mut r = v.cells.clone(); r.insert(0, v.row_pos.to_string()); r}).collect();
let (tids, tnames) = get_tids(&conn, uid).await;
Some(
Template::render("table",
context!{
search_value: searchvalue,
search_fields: searchfields,
sort_field: sortfield,
sort_dir: sortdir,
tblid: tid,
tblname: tname,
tids: tids,
tnames: tnames,
column_names: column_names,
column_types: column_types,
rows: rows,
}
)
)
}
#[post("/delete", data="<data>")]
pub async fn delete_table(conn: Db, data: Form<DeleteTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
if conn.run(move |c| inventur_db::delete_table(c, tblid, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/")))
}
#[post("/new", data="<data>")]
pub async fn new_entry(conn: Db, data: Form<NewEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let cells = data.cells.clone();
let tblid = data.tblid;
if conn.run(move |c| inventur_db::add_row(c, data.tblid, cells, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/edit", data="<data>")]
pub async fn edit_entry(conn: Db, data: Form<EditEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let cells = data.cells.clone();
let tblid = data.tblid;
let row_pos = data.row_pos;
if conn.run(move |c| inventur_db::edit_row(c, tblid, cells, row_pos, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/delete", data="<data>")]
pub async fn delete_entry(conn: Db, data: Form<DeleteEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let row_pos = data.row_pos;
if conn.run(move |c| inventur_db::delete_row(c, tblid, row_pos, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/create", data="<data>")]
pub async fn create_column(conn: Db, data: Form<NewColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let coltype = FIELDTYPE::from(data.column_type);
if conn.run(move |c| inventur_db::add_column(c, tblid, data.name.clone(), coltype, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/edit", data="<data>")]
pub async fn edit_column(conn: Db, data: Form<EditColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let idintbl = data.id_in_table;
let clmtype = FIELDTYPE::from(data.column_type);
if conn.run(move |c| inventur_db::edit_column(c, tblid, idintbl, &data.name, clmtype, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/delete", data="<data>")]
pub async fn delete_column(conn: Db, data: Form<DeleteColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let idintbl = data.id_in_table;
if conn.run(move |c| inventur_db::delete_column(c, tblid, idintbl, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/name/edit", data="<data>")]
pub async fn edit_tname(conn: Db, data: Form<EditTname>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
if conn.run(move |c| inventur_db::rename_table(c, data.tblid, data.new_name.clone(), uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/create", data="<data>")]
pub async fn create(conn: Db, data: Form<NewTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = conn.run(move |c| inventur_db::create_table(c, data.name.clone(), data.field_names.clone(), data.field_types.iter().map(|x: &isize| FIELDTYPE::from(*x)).collect::<Vec<FIELDTYPE>>(), uid)).await;
if tblid.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid.unwrap()))))
}
#[post("/import", data="<data>")]
pub async fn import_table(conn: Db, data: Json<ImportTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let columns = data.columns.clone();
let rows = data.rows.clone();
let tblid = conn.run(move |c| inventur_db::create_table(c, data.name.clone(), columns.clone(), vec![FIELDTYPE::TEXT; columns.len()], uid)).await;
if tblid.is_none() {
return Err(Status::UnprocessableEntity);
}
let tblid = tblid.unwrap();
for row in rows {
conn.run(move |c| inventur_db::add_row(c, tblid, row, uid)).await;
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}

92
src/table/forms.rs Normal file
View File

@@ -0,0 +1,92 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use rocket::form::Form;
use rocket::serde::{Serialize, Deserialize};
#[derive(FromForm)]
pub struct DeleteColumn {
pub tblid: i32,
pub id_in_table: i32,
}
#[derive(FromForm)]
pub struct EditColumn {
pub tblid: i32,
pub id_in_table: i32,
pub name: String,
pub column_type: isize,
}
#[derive(FromForm)]
pub struct NewColumn {
pub tblid: i32,
pub name: String,
pub column_type: isize,
}
#[derive(FromForm)]
pub struct NewEntry {
pub tblid: i32,
pub cells: Vec<String>,
}
#[derive(FromForm)]
pub struct EditEntry {
pub tblid: i32,
pub cells: Vec<String>,
pub row_pos: i32,
}
#[derive(FromForm)]
pub struct DeleteEntry {
pub tblid: i32,
pub row_pos: i32,
}
#[derive(FromForm)]
pub struct EditTname {
pub tblid: i32,
pub new_name: String,
}
#[derive(FromForm)]
pub struct NewTable {
pub name: String,
pub field_names: Vec<String>,
pub field_types: Vec<isize>,
}
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct ImportTable {
pub name: String,
pub columns: Vec<String>,
pub rows: Vec<Vec<String>>,
}
#[derive(FromForm)]
pub struct DeleteTable {
pub tblid: i32,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct JRows {
pub rows: Vec<Vec<String>>,
}

View File

@@ -0,0 +1,69 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::auth;
use crate::Db;
use crate::table::forms;
use crate::table::table_view;
use inventur_db;
use rocket::form::Form;
use rocket::response::Redirect;
use rocket::http::Status;
use inventur_db::FIELDTYPE;
use auth::AuthUser;
use self::forms::{NewColumn, EditColumn, DeleteColumn};
use self::table_view::rocket_uri_macro_table_sec;
#[post("/create", data="<data>")]
pub async fn create_column(conn: Db, data: Form<NewColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let coltype = FIELDTYPE::from(data.column_type);
if conn.run(move |c| inventur_db::add_column(c, tblid, data.name.clone(), coltype, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/edit", data="<data>")]
pub async fn edit_column(conn: Db, data: Form<EditColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let idintbl = data.id_in_table;
let clmtype = FIELDTYPE::from(data.column_type);
if conn.run(move |c| inventur_db::edit_column(c, tblid, idintbl, &data.name, clmtype, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/delete", data="<data>")]
pub async fn delete_column(conn: Db, data: Form<DeleteColumn>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let idintbl = data.id_in_table;
if conn.run(move |c| inventur_db::delete_column(c, tblid, idintbl, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}

View File

@@ -0,0 +1,67 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::auth;
use crate::Db;
use crate::table::forms;
use crate::table::table_view;
use inventur_db;
use rocket::form::Form;
use rocket::response::Redirect;
use rocket::http::Status;
use auth::AuthUser;
use self::forms::{NewEntry, EditEntry, DeleteEntry};
use self::table_view::rocket_uri_macro_table_sec;
#[post("/new", data="<data>")]
pub async fn new_entry(conn: Db, data: Form<NewEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let cells = data.cells.clone();
let tblid = data.tblid;
if conn.run(move |c| inventur_db::add_row(c, data.tblid, cells, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/edit", data="<data>")]
pub async fn edit_entry(conn: Db, data: Form<EditEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let cells = data.cells.clone();
let tblid = data.tblid;
let row_pos = data.row_pos;
if conn.run(move |c| inventur_db::edit_row(c, tblid, cells, row_pos, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/delete", data="<data>")]
pub async fn delete_entry(conn: Db, data: Form<DeleteEntry>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
let row_pos = data.row_pos;
if conn.run(move |c| inventur_db::delete_row(c, tblid, row_pos, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}

View File

@@ -0,0 +1,83 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::auth;
use crate::Db;
use crate::table::forms;
use crate::table::table_view;
use inventur_db;
use auth::AuthUser;
use rocket::form::Form;
use rocket::response::Redirect;
use rocket::http::Status;
use rocket::serde::json::Json;
use inventur_db::FIELDTYPE;
use self::forms::{NewTable, DeleteTable, ImportTable, EditTname};
use self::table_view::rocket_uri_macro_table_sec;
#[post("/delete", data="<data>")]
pub async fn delete_table(conn: Db, data: Form<DeleteTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
if conn.run(move |c| inventur_db::delete_table(c, tblid, uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/")))
}
#[post("/create", data="<data>")]
pub async fn create_table(conn: Db, data: Form<NewTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = conn.run(move |c| inventur_db::create_table(c, data.name.clone(), data.field_names.clone(), data.field_types.iter().map(|x: &isize| FIELDTYPE::from(*x)).collect::<Vec<FIELDTYPE>>(), uid)).await;
if tblid.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid.unwrap()))))
}
#[post("/import", data="<data>")]
pub async fn import_table(conn: Db, data: Json<ImportTable>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let columns = data.columns.clone();
let rows = data.rows.clone();
let tblid = conn.run(move |c| inventur_db::create_table(c, data.name.clone(), columns.clone(), vec![FIELDTYPE::TEXT; columns.len()], uid)).await;
if tblid.is_none() {
return Err(Status::UnprocessableEntity);
}
let tblid = tblid.unwrap();
for row in rows {
conn.run(move |c| inventur_db::add_row(c, tblid, row, uid)).await;
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}
#[post("/name/edit", data="<data>")]
pub async fn edit_tname(conn: Db, data: Form<EditTname>, user: AuthUser) -> Result<Redirect, Status> {
let uid = user.uid;
let tblid = data.tblid;
if conn.run(move |c| inventur_db::rename_table(c, data.tblid, data.new_name.clone(), uid)).await.is_none() {
return Err(Status::Forbidden);
}
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
}

115
src/table/table_view.rs Normal file
View File

@@ -0,0 +1,115 @@
/* This file is part of inventur.
* inventur is a simple web app using rocket to help maintain inventory data.
* Copyright (C) 2024 Johannes Randerath
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::auth;
use crate::Db;
use inventur_db;
use auth::AuthUser;
use rocket_dyn_templates::{Template, context};
use rocket::response::Redirect;
#[get("/<tid>?<sort_dir>&<sort_field>&<search_fields>&<search_value>")]
pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<usize>, search_fields: Option<Vec<i32>>, search_value: Option<String>, user: AuthUser) -> Option<Template> {
let uid = user.uid;
let table = conn.run(move |c| inventur_db::get_table(c, tid, uid)).await;
if table.is_none() {
return None;
}
let mut table = table.unwrap();
let searchvalue;
let searchfields;
if search_value.is_some() && !search_value.clone().unwrap().eq_ignore_ascii_case("") && search_fields.is_some() {
searchvalue = search_value.unwrap();
searchfields = search_fields.unwrap();
table = inventur_db::search_table(table, searchfields.clone(), searchvalue.clone());
}else {
if search_value.is_none() {
searchvalue = String::new();
} else {
searchvalue = search_value.unwrap();
}
if search_fields.is_none() {
searchfields = (0i32..(table.column_names.len() as i32)).collect();
} else {
searchfields = search_fields.unwrap();
}
}
let sortdir;
if sort_dir.is_none() || sort_dir.unwrap() > 1 {
sortdir = 0;
}else {
sortdir = sort_dir.unwrap();
}
let sortfield;
if sort_field.is_none() {
sortfield = 0;
}else {
sortfield = sort_field.unwrap();
}
let table = inventur_db::sort_table(table, sortfield, sortdir);
let tname = table.name;
let column_names = table.column_names;
let column_types = table.column_types.iter().map(|x: &inventur_db::FIELDTYPE| *x as i32).collect::<Vec<i32>>();
let rows : Vec<Vec<String>>= table.rows.iter().map(|v| {let mut r = v.cells.clone(); r.insert(0, v.row_pos.to_string()); r}).collect();
let (tids, tnames) = get_tids(&conn, uid).await;
Some(
Template::render("table",
context!{
search_value: searchvalue,
search_fields: searchfields,
sort_field: sortfield,
sort_dir: sortdir,
tblid: tid,
tblname: tname,
tids: tids,
tnames: tnames,
column_names: column_names,
column_types: column_types,
rows: rows,
}
)
)
}
#[get("/<tid>", rank=2)]
pub async fn table_sec(conn: Db, tid: i32, user: AuthUser) -> Redirect {
let nus : Option<usize> = None;
let nu8 : Option<u8> = None;
let nvi32 : Option<Vec<i32>> = None;
let ns : Option<String> = None;
Redirect::to(uri!("/table", table(tid, nu8, nus, nvi32, ns)))
}
pub async fn get_tids(conn: &Db, uid: i32) -> (Vec<i32>, Vec<String>) {
let mut tids = conn.run(move |c| inventur_db::get_user_tblids(c, uid)).await;
let tnames;
if tids.is_none() {
tids = Some(Vec::new());
tnames = Some(Vec::new());
}else {
let tids = tids.clone().unwrap();
tnames = conn.run(move |c| inventur_db::get_tblnames(c, tids)).await;
}
(tids.unwrap(), tnames.unwrap())
}