diff --git a/inventur_db/src/jrcells.rs b/inventur_db/src/jrcells.rs index 5220b15..9fc4ffc 100644 --- a/inventur_db/src/jrcells.rs +++ b/inventur_db/src/jrcells.rs @@ -21,26 +21,12 @@ use crate::jrcolumns; use crate::jrentries; use models::{ Jrcell, NewJrcell }; -use schema::jrcells::dsl::{jrcells, id, jrentry_id, jrcolumn_id, cell_value}; +use schema::jrcells::dsl::*; use diesel::prelude::*; use diesel::mysql::MysqlConnection; -pub fn create_jrcell_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, value: String) -> Result { - let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos); - if ntrid.is_err() { - return Err(ntrid.err().unwrap()); - } - let ntrid = ntrid.unwrap(); - let clmid = jrcolumns::get_clmid_relative(conn, tblid, idintbl); - if clmid.is_err() { - return Err(clmid.err().unwrap()); - } - let clmid = clmid.unwrap(); - create_jrcell(conn, ntrid, clmid, &value) -} - pub fn change_jrcell_value_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, new_value: &String) -> Result { let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos); if ntrid.is_err() { @@ -55,20 +41,6 @@ pub fn change_jrcell_value_relative(conn: &mut MysqlConnection, tblid: i32, rowp change_jrcell_value(conn, ntrid, clmid, new_value) } -pub fn delete_jrcell_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32) -> Result { - let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos); - if ntrid.is_err() { - return Err(ntrid.err().unwrap()); - } - let ntrid = ntrid.unwrap(); - let clmid = jrcolumns::get_clmid_relative(conn, tblid, idintbl); - if clmid.is_err() { - return Err(clmid.err().unwrap()); - } - let clmid = clmid.unwrap(); - delete_jrcell(conn, ntrid, clmid) -} - pub fn create_jrcell(conn: &mut MysqlConnection, entryid: i32, columnid: i32, value: &String) -> Result { use self::schema::jrentries::dsl::jrentries; use self::schema::jrcolumns::dsl::jrcolumns; @@ -101,12 +73,6 @@ pub fn change_jrcell_value(conn: &mut MysqlConnection, entryid: i32, columnid: i .execute(conn) } -pub fn delete_jrcell(conn: &mut MysqlConnection, entryid: i32, columnid: i32) -> Result { - diesel::delete(jrcells.filter(jrentry_id.eq(entryid)).filter(jrcolumn_id.eq(columnid))) - .execute(conn) -} - - pub fn get_entry_cells(conn: &mut MysqlConnection, entryid: i32) -> Result, diesel::result::Error> { jrcells .filter(jrentry_id.eq(entryid)) @@ -114,9 +80,3 @@ pub fn get_entry_cells(conn: &mut MysqlConnection, entryid: i32) -> Result Result, diesel::result::Error> { - jrcells - .filter(jrentry_id.eq(entryid)) - .select(id) - .load(conn) -} diff --git a/inventur_db/src/jrentries.rs b/inventur_db/src/jrentries.rs index ec44c91..f7a2c9f 100644 --- a/inventur_db/src/jrentries.rs +++ b/inventur_db/src/jrentries.rs @@ -124,7 +124,10 @@ pub fn delete_jrentry(conn: &mut MysqlConnection, entryid: i32) -> Result Result Result, diesel::result::Error> { - jrtables - .select(Jrtable::as_select()) - .load(conn) -} - pub fn get_tbl(conn: &mut MysqlConnection, tblid: i32) -> Result { jrtables .find(tblid) diff --git a/inventur_db/src/lib.rs b/inventur_db/src/lib.rs index 715d87b..5004d26 100644 --- a/inventur_db/src/lib.rs +++ b/inventur_db/src/lib.rs @@ -68,7 +68,7 @@ impl From for FIELDTYPE { fn from(value: isize) -> Self { match value { 1 => FIELDTYPE::NUMBER, - x => FIELDTYPE::TEXT, + _ => FIELDTYPE::TEXT, } } } @@ -148,11 +148,6 @@ pub fn get_table(conn: &mut MysqlConnection, tblid: i32, uid: i32) -> Option Tbl { /// search_value is a string to search for in the search_fields. /// Returned Tbl is not sorted and should be according to user preferences before being displayed. pub fn search_table(tbl: Tbl, search_fields: Vec, search_value: String) -> Tbl { - let mut rows = tbl.rows; + let rows = tbl.rows; let mut field_sets = HashSet::new(); for field in search_fields { for row in &rows { diff --git a/inventur_db/src/users.rs b/inventur_db/src/users.rs index 373c211..29d6add 100644 --- a/inventur_db/src/users.rs +++ b/inventur_db/src/users.rs @@ -58,17 +58,4 @@ pub fn get_uname(conn: &mut MysqlConnection, uid: i32) -> Result(conn) } -pub fn get_email(conn: &mut MysqlConnection, uid: i32) -> Result { - users - .find(uid) - .select(email) - .first::(conn) -} -pub fn get_user_tables(conn: &mut MysqlConnection, uid: i32) -> Result, diesel::result::Error> { - use schema::jrtables::dsl::{jrtables, id as tblid, owner_id}; - jrtables - .filter(owner_id.eq(uid)) - .select(tblid) - .load::(conn) -} diff --git a/src/auth.rs b/src/auth.rs index a25f82a..26dafa1 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -25,7 +25,7 @@ use rocket::http::{Status, Cookie, CookieJar, SameSite}; use inventur_db; use rocket_oauth2::{OAuth2, TokenResponse}; use reqwest::Client; -use rocket::serde::{Deserialize, json::Json}; +use rocket::serde::Deserialize; /// OAuth provider pub struct RanderathIdentity; @@ -101,7 +101,7 @@ pub async fn redirect_to_login_401() -> Redirect { /// Unauthorized requests are sent to the oauth provider in order for the user to authenticate. #[catch(403)] -pub async fn redirect_to_login() -> Redirect { +pub async fn redirect_to_login_403() -> Redirect { Redirect::to(uri!(oauth_login())) } @@ -116,7 +116,6 @@ pub fn oauth_login(oauth2: OAuth2, cookies: &CookieJar<'_>) - #[get("/auth")] pub async fn oauth_callback(conn: Db, token: TokenResponse, cookies: &CookieJar<'_>) -> Result { let at = token.access_token().to_string(); - let tv = token.as_value(); cookies.add_private( Cookie::build(("token", at.to_string())) .same_site(SameSite::Lax) diff --git a/src/main.rs b/src/main.rs index a51973a..d91a7b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,16 +30,14 @@ use auth::AuthUser; use rocket::fs::{FileServer, relative}; use rocket_dyn_templates::{Template, context}; -use rocket::request; use rocket::response::Redirect; -use rocket::http::{Status, Cookie, CookieJar, SameSite}; use inventur_db; use rocket_sync_db_pools::{database, diesel}; use rocket_oauth2::OAuth2; use std::env; use dotenvy::dotenv; use rocket::Config; -use rocket::figment::providers::{Toml, Env, Format}; +use rocket::figment::providers::Env; /// Database connection using diesel and rocket_sync_db_pools #[database("inventur")] @@ -84,33 +82,32 @@ async fn favicon() -> Redirect { Redirect::to(uri!("/img/favicon.ico")) } +/// Setup app for launch: +/// Load configuration from a file called .env in the project's root. +/// Use tera templates, connect to mysql db, setup oauth +/// Serve everything related to ... +/// ... home page and login under / +/// ... display of a table, and the manipulation of the table object under /table +/// ... manipulation of table rows under /row +/// ... manipulation of columns under /column +/// ... requests not logged in to the oauth provider +/// Setup a fileserver to serve static files from the static directory in the file type's directory #[launch] async fn rocket() -> _ { - /// Load configuration from a file called .env in the project's root. dotenv().ok(); let cfg = Config::figment() .merge(Env::prefixed("ROCKET_")); - rocket::custom(cfg) - /// Use tera templates .attach(Template::fairing()) - /// Connect to mysql db .attach(Db::fairing()) - /// Set up oauth .attach(OAuth2::::fairing("oauth")) - /// Everything related to the home page and login .mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon]) - /// Everything related to the table view and modifying the table as an object (as opposed to its rows and columns). .mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create_table, table::import_table, table::delete_table]) - /// Modify table rows and their contents .mount("/row", routes![table::new_entry, table::edit_entry, table::delete_entry]) - /// Modify the table's columns, their names and types. - .mount("/column", routes![table::delete_column, table::edit_column]) - /// If not logged in, redirect to oauth login - .register("/", catchers![auth::redirect_to_login]) - /// Serve static files in the corresponding subdirs of /static. + .mount("/column", routes![table::delete_column, table::edit_column, table::create_column]) + .register("/", catchers![auth::redirect_to_login_401, auth::redirect_to_login_403]) .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 23f4cb9..03896e3 100644 --- a/src/table.rs +++ b/src/table.rs @@ -33,5 +33,4 @@ 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::*; diff --git a/src/table/forms.rs b/src/table/forms.rs index 533fdf6..fa71c36 100644 --- a/src/table/forms.rs +++ b/src/table/forms.rs @@ -18,8 +18,7 @@ //! Submodule holding structs relevant to handle form data. -use rocket::form::Form; -use rocket::serde::{Serialize, Deserialize}; +use rocket::serde::Deserialize; #[derive(FromForm)] pub struct DeleteColumn { diff --git a/src/table/table_view.rs b/src/table/table_view.rs index 50ca862..8efe671 100644 --- a/src/table/table_view.rs +++ b/src/table/table_view.rs @@ -99,7 +99,7 @@ pub async fn table(conn: Db, tid: i32, sort_dir: Option, sort_field: Option< /// View to redirect a post request handled to manipulate a table or its display representation back to the (new) table view. /// Also uses table() but nulls all optional fields. #[get("/", rank=2)] -pub async fn table_sec(conn: Db, tid: i32, user: AuthUser) -> Redirect { +pub async fn table_sec(tid: i32) -> Redirect { let nus : Option = None; let nu8 : Option = None; let nvi32 : Option> = None;