Lots of refactoring. More, smaller files.
This commit is contained in:
92
src/table/forms.rs
Normal file
92
src/table/forms.rs
Normal 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>>,
|
||||
}
|
||||
69
src/table/table_manipulate_column.rs
Normal file
69
src/table/table_manipulate_column.rs
Normal 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))))
|
||||
}
|
||||
|
||||
67
src/table/table_manipulate_entry.rs
Normal file
67
src/table/table_manipulate_entry.rs
Normal 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))))
|
||||
}
|
||||
83
src/table/table_manipulate_table.rs
Normal file
83
src/table/table_manipulate_table.rs
Normal 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
115
src/table/table_view.rs
Normal 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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user