Added documentation and refactored
This commit is contained in:
parent
d422a4b31c
commit
fe04cf539e
|
|
@ -21,26 +21,12 @@ use crate::jrcolumns;
|
||||||
use crate::jrentries;
|
use crate::jrentries;
|
||||||
|
|
||||||
use models::{ Jrcell, NewJrcell };
|
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::prelude::*;
|
||||||
use diesel::mysql::MysqlConnection;
|
use diesel::mysql::MysqlConnection;
|
||||||
|
|
||||||
|
|
||||||
pub fn create_jrcell_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, value: String) -> Result<usize, diesel::result::Error> {
|
|
||||||
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<usize, diesel::result::Error> {
|
pub fn change_jrcell_value_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, new_value: &String) -> Result<usize, diesel::result::Error> {
|
||||||
let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos);
|
let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos);
|
||||||
if ntrid.is_err() {
|
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)
|
change_jrcell_value(conn, ntrid, clmid, new_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_jrcell_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32) -> Result<usize, diesel::result::Error> {
|
|
||||||
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<usize, diesel::result::Error> {
|
pub fn create_jrcell(conn: &mut MysqlConnection, entryid: i32, columnid: i32, value: &String) -> Result<usize, diesel::result::Error> {
|
||||||
use self::schema::jrentries::dsl::jrentries;
|
use self::schema::jrentries::dsl::jrentries;
|
||||||
use self::schema::jrcolumns::dsl::jrcolumns;
|
use self::schema::jrcolumns::dsl::jrcolumns;
|
||||||
|
|
@ -101,12 +73,6 @@ pub fn change_jrcell_value(conn: &mut MysqlConnection, entryid: i32, columnid: i
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_jrcell(conn: &mut MysqlConnection, entryid: i32, columnid: i32) -> Result<usize, diesel::result::Error> {
|
|
||||||
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<Vec<Jrcell>, diesel::result::Error> {
|
pub fn get_entry_cells(conn: &mut MysqlConnection, entryid: i32) -> Result<Vec<Jrcell>, diesel::result::Error> {
|
||||||
jrcells
|
jrcells
|
||||||
.filter(jrentry_id.eq(entryid))
|
.filter(jrentry_id.eq(entryid))
|
||||||
|
|
@ -114,9 +80,3 @@ pub fn get_entry_cells(conn: &mut MysqlConnection, entryid: i32) -> Result<Vec<J
|
||||||
.load(conn)
|
.load(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_entry_cell_ids(conn: &mut MysqlConnection, entryid: i32) -> Result<Vec<i32>, diesel::result::Error> {
|
|
||||||
jrcells
|
|
||||||
.filter(jrentry_id.eq(entryid))
|
|
||||||
.select(id)
|
|
||||||
.load(conn)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,10 @@ pub fn delete_jrentry(conn: &mut MysqlConnection, entryid: i32) -> Result<usize,
|
||||||
if nr.is_err() {
|
if nr.is_err() {
|
||||||
return nr;
|
return nr;
|
||||||
}
|
}
|
||||||
move_jrentry(conn, tblid, rp, nr.unwrap() as i32);
|
let mr = move_jrentry(conn, tblid, rp, nr.unwrap() as i32);
|
||||||
|
if mr.is_err() {
|
||||||
|
return mr;
|
||||||
|
}
|
||||||
diesel::delete(jrentries.find(entryid)).execute(conn)
|
diesel::delete(jrentries.find(entryid)).execute(conn)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,12 +60,6 @@ pub fn delete_jrtable(conn: &mut MysqlConnection, tblid: i32) -> Result<usize, d
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_all_tables(conn: &mut MysqlConnection) -> Result<Vec<Jrtable>, diesel::result::Error> {
|
|
||||||
jrtables
|
|
||||||
.select(Jrtable::as_select())
|
|
||||||
.load(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_tbl(conn: &mut MysqlConnection, tblid: i32) -> Result<Jrtable, diesel::result::Error> {
|
pub fn get_tbl(conn: &mut MysqlConnection, tblid: i32) -> Result<Jrtable, diesel::result::Error> {
|
||||||
jrtables
|
jrtables
|
||||||
.find(tblid)
|
.find(tblid)
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ impl From<isize> for FIELDTYPE {
|
||||||
fn from(value: isize) -> Self {
|
fn from(value: isize) -> Self {
|
||||||
match value {
|
match value {
|
||||||
1 => FIELDTYPE::NUMBER,
|
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
|
||||||
clmn_nms.push(clmn.name.clone());
|
clmn_nms.push(clmn.name.clone());
|
||||||
}
|
}
|
||||||
let clmn_nms = clmn_nms;
|
let clmn_nms = clmn_nms;
|
||||||
let mut clmn_ids = Vec::new();
|
|
||||||
for clmn in &clmns {
|
|
||||||
clmn_ids.push(clmn.id);
|
|
||||||
}
|
|
||||||
let clmn_ids = clmn_ids;
|
|
||||||
let mut clmn_tps = Vec::new();
|
let mut clmn_tps = Vec::new();
|
||||||
for clmn in &clmns {
|
for clmn in &clmns {
|
||||||
clmn_tps.push(clmn.column_type);
|
clmn_tps.push(clmn.column_type);
|
||||||
|
|
@ -210,7 +205,7 @@ pub fn sort_table(tbl: Tbl, sort_field: usize, sort_dir: u8) -> Tbl {
|
||||||
/// search_value is a string to search for in the search_fields.
|
/// 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.
|
/// Returned Tbl is not sorted and should be according to user preferences before being displayed.
|
||||||
pub fn search_table(tbl: Tbl, search_fields: Vec<i32>, search_value: String) -> Tbl {
|
pub fn search_table(tbl: Tbl, search_fields: Vec<i32>, search_value: String) -> Tbl {
|
||||||
let mut rows = tbl.rows;
|
let rows = tbl.rows;
|
||||||
let mut field_sets = HashSet::new();
|
let mut field_sets = HashSet::new();
|
||||||
for field in search_fields {
|
for field in search_fields {
|
||||||
for row in &rows {
|
for row in &rows {
|
||||||
|
|
|
||||||
|
|
@ -58,17 +58,4 @@ pub fn get_uname(conn: &mut MysqlConnection, uid: i32) -> Result<String, diesel:
|
||||||
.first::<String>(conn)
|
.first::<String>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_email(conn: &mut MysqlConnection, uid: i32) -> Result<String, diesel::result::Error> {
|
|
||||||
users
|
|
||||||
.find(uid)
|
|
||||||
.select(email)
|
|
||||||
.first::<String>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_user_tables(conn: &mut MysqlConnection, uid: i32) -> Result<Vec<i32>, diesel::result::Error> {
|
|
||||||
use schema::jrtables::dsl::{jrtables, id as tblid, owner_id};
|
|
||||||
jrtables
|
|
||||||
.filter(owner_id.eq(uid))
|
|
||||||
.select(tblid)
|
|
||||||
.load::<i32>(conn)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ use rocket::http::{Status, Cookie, CookieJar, SameSite};
|
||||||
use inventur_db;
|
use inventur_db;
|
||||||
use rocket_oauth2::{OAuth2, TokenResponse};
|
use rocket_oauth2::{OAuth2, TokenResponse};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use rocket::serde::{Deserialize, json::Json};
|
use rocket::serde::Deserialize;
|
||||||
|
|
||||||
/// OAuth provider
|
/// OAuth provider
|
||||||
pub struct RanderathIdentity;
|
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.
|
/// Unauthorized requests are sent to the oauth provider in order for the user to authenticate.
|
||||||
#[catch(403)]
|
#[catch(403)]
|
||||||
pub async fn redirect_to_login() -> Redirect {
|
pub async fn redirect_to_login_403() -> Redirect {
|
||||||
Redirect::to(uri!(oauth_login()))
|
Redirect::to(uri!(oauth_login()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,7 +116,6 @@ pub fn oauth_login(oauth2: OAuth2<RanderathIdentity>, cookies: &CookieJar<'_>) -
|
||||||
#[get("/auth")]
|
#[get("/auth")]
|
||||||
pub async fn oauth_callback(conn: Db, token: TokenResponse<RanderathIdentity>, cookies: &CookieJar<'_>) -> Result<Redirect, Status> {
|
pub async fn oauth_callback(conn: Db, token: TokenResponse<RanderathIdentity>, cookies: &CookieJar<'_>) -> Result<Redirect, Status> {
|
||||||
let at = token.access_token().to_string();
|
let at = token.access_token().to_string();
|
||||||
let tv = token.as_value();
|
|
||||||
cookies.add_private(
|
cookies.add_private(
|
||||||
Cookie::build(("token", at.to_string()))
|
Cookie::build(("token", at.to_string()))
|
||||||
.same_site(SameSite::Lax)
|
.same_site(SameSite::Lax)
|
||||||
|
|
|
||||||
29
src/main.rs
29
src/main.rs
|
|
@ -30,16 +30,14 @@ use auth::AuthUser;
|
||||||
|
|
||||||
use rocket::fs::{FileServer, relative};
|
use rocket::fs::{FileServer, relative};
|
||||||
use rocket_dyn_templates::{Template, context};
|
use rocket_dyn_templates::{Template, context};
|
||||||
use rocket::request;
|
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::http::{Status, Cookie, CookieJar, SameSite};
|
|
||||||
use inventur_db;
|
use inventur_db;
|
||||||
use rocket_sync_db_pools::{database, diesel};
|
use rocket_sync_db_pools::{database, diesel};
|
||||||
use rocket_oauth2::OAuth2;
|
use rocket_oauth2::OAuth2;
|
||||||
use std::env;
|
use std::env;
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use rocket::Config;
|
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 connection using diesel and rocket_sync_db_pools
|
||||||
#[database("inventur")]
|
#[database("inventur")]
|
||||||
|
|
@ -84,33 +82,32 @@ async fn favicon() -> Redirect {
|
||||||
Redirect::to(uri!("/img/favicon.ico"))
|
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]
|
#[launch]
|
||||||
async fn rocket() -> _ {
|
async fn rocket() -> _ {
|
||||||
/// Load configuration from a file called .env in the project's root.
|
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
let cfg = Config::figment()
|
let cfg = Config::figment()
|
||||||
.merge(Env::prefixed("ROCKET_"));
|
.merge(Env::prefixed("ROCKET_"));
|
||||||
|
|
||||||
|
|
||||||
rocket::custom(cfg)
|
rocket::custom(cfg)
|
||||||
/// Use tera templates
|
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
/// Connect to mysql db
|
|
||||||
.attach(Db::fairing())
|
.attach(Db::fairing())
|
||||||
/// Set up oauth
|
|
||||||
.attach(OAuth2::<auth::RanderathIdentity>::fairing("oauth"))
|
.attach(OAuth2::<auth::RanderathIdentity>::fairing("oauth"))
|
||||||
/// Everything related to the home page and login
|
|
||||||
.mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon])
|
.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])
|
.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])
|
.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, table::create_column])
|
||||||
.mount("/column", routes![table::delete_column, table::edit_column])
|
.register("/", catchers![auth::redirect_to_login_401, auth::redirect_to_login_403])
|
||||||
/// If not logged in, redirect to oauth login
|
|
||||||
.register("/", catchers![auth::redirect_to_login])
|
|
||||||
/// Serve static files in the corresponding subdirs of /static.
|
|
||||||
.mount("/img", FileServer::from(relative!("static/img")))
|
.mount("/img", FileServer::from(relative!("static/img")))
|
||||||
.mount("/css", FileServer::from(relative!("static/css")))
|
.mount("/css", FileServer::from(relative!("static/css")))
|
||||||
.mount("/js", FileServer::from(relative!("static/js")))
|
.mount("/js", FileServer::from(relative!("static/js")))
|
||||||
|
|
|
||||||
|
|
@ -33,5 +33,4 @@ pub use self::table_view::*;
|
||||||
pub use self::table_manipulate_table::*;
|
pub use self::table_manipulate_table::*;
|
||||||
pub use self::table_manipulate_entry::*;
|
pub use self::table_manipulate_entry::*;
|
||||||
pub use self::table_manipulate_column::*;
|
pub use self::table_manipulate_column::*;
|
||||||
pub use self::forms::*;
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,7 @@
|
||||||
|
|
||||||
//! Submodule holding structs relevant to handle form data.
|
//! Submodule holding structs relevant to handle form data.
|
||||||
|
|
||||||
use rocket::form::Form;
|
use rocket::serde::Deserialize;
|
||||||
use rocket::serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
pub struct DeleteColumn {
|
pub struct DeleteColumn {
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<
|
||||||
/// View to redirect a post request handled to manipulate a table or its display representation back to the (new) table view.
|
/// 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.
|
/// Also uses table() but nulls all optional fields.
|
||||||
#[get("/<tid>", rank=2)]
|
#[get("/<tid>", 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<usize> = None;
|
let nus : Option<usize> = None;
|
||||||
let nu8 : Option<u8> = None;
|
let nu8 : Option<u8> = None;
|
||||||
let nvi32 : Option<Vec<i32>> = None;
|
let nvi32 : Option<Vec<i32>> = None;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user