From 9dafd7504b89ef08f1544f426831c27b7b2b4803 Mon Sep 17 00:00:00 2001 From: Johannes Randerath Date: Thu, 29 Aug 2024 16:30:19 +0200 Subject: [PATCH] Lots of refactoring. More, smaller files. --- src/main.rs | 13 +- src/table.rs | 298 +----------------------- src/table/forms.rs | 92 ++++++++ src/table/table_manipulate_column.rs | 69 ++++++ src/table/table_manipulate_entry.rs | 67 ++++++ src/table/table_manipulate_table.rs | 83 +++++++ src/table/table_view.rs | 115 +++++++++ static/{ => img}/favicon.ico | Bin static/js/base.js | 102 ++++++++ static/js/table.js | 85 +++++++ templates/base.html.tera | 197 ++-------------- templates/base_modals.html.tera | 120 ++++++++++ templates/home.html.tera | 39 ++-- templates/table.html.tera | 335 +++++---------------------- templates/table_modals.html.tera | 197 ++++++++++++++++ 15 files changed, 1052 insertions(+), 760 deletions(-) create mode 100644 src/table/forms.rs create mode 100644 src/table/table_manipulate_column.rs create mode 100644 src/table/table_manipulate_entry.rs create mode 100644 src/table/table_manipulate_table.rs create mode 100644 src/table/table_view.rs rename static/{ => img}/favicon.ico (100%) create mode 100644 static/js/base.js create mode 100644 static/js/table.js create mode 100644 templates/base_modals.html.tera create mode 100644 templates/table_modals.html.tera diff --git a/src/main.rs b/src/main.rs index a293de2..bd0d587 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::::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"))) } diff --git a/src/table.rs b/src/table.rs index 87bd913..592cf9f 100644 --- a/src/table.rs +++ b/src/table.rs @@ -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 . */ -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, -} - -#[derive(FromForm)] -struct EditEntry { - tblid: i32, - cells: Vec, - 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, - field_types: Vec, -} - -#[derive(Deserialize)] -#[serde(crate = "rocket::serde")] -struct ImportTable { - name: String, - columns: Vec, - rows: Vec>, -} - -#[derive(FromForm)] -struct DeleteTable { - tblid: i32, -} - -#[derive(Serialize)] -#[serde(crate = "rocket::serde")] -struct JRows { - rows: Vec>, -} - -pub async fn get_tids(conn: &Db, uid: i32) -> (Vec, Vec) { - 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("/", rank=2)] -pub async fn table_sec(conn: Db, tid: i32, user: AuthUser) -> Redirect { - let nus : Option = None; - let nu8 : Option = None; - let nvi32 : Option> = None; - let ns : Option = None; - Redirect::to(uri!("/table", table(tid, nu8, nus, nvi32, ns))) -} - -#[get("/?&&&")] -pub async fn table(conn: Db, tid: i32, sort_dir: Option, sort_field: Option, search_fields: Option>, search_value: Option, user: AuthUser) -> Option