Fixed several features
This commit is contained in:
@@ -65,8 +65,9 @@ async fn rocket() -> _ {
|
||||
.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])
|
||||
.mount("/entry", routes![table::new_entry])
|
||||
.mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create, table::import_table, table::delete_table])
|
||||
.mount("/row", routes![table::new_entry, table::edit_entry, table::delete_entry])
|
||||
.mount("/column", routes![table::delete_column])
|
||||
.register("/", catchers![auth::redirect_to_login])
|
||||
.mount("/static", FileServer::from(relative!("static")))
|
||||
|
||||
|
||||
83
src/table.rs
83
src/table.rs
@@ -9,8 +9,13 @@ 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 NewEntry {
|
||||
@@ -18,6 +23,19 @@ struct NewEntry {
|
||||
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,
|
||||
@@ -27,7 +45,8 @@ struct EditTname {
|
||||
#[derive(FromForm)]
|
||||
struct NewTable {
|
||||
name: String,
|
||||
fields: Vec<String>,
|
||||
field_names: Vec<String>,
|
||||
field_types: Vec<isize>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -38,6 +57,11 @@ struct ImportTable {
|
||||
rows: Vec<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct DeleteTable {
|
||||
tblid: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
struct JRows {
|
||||
@@ -109,7 +133,8 @@ pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<
|
||||
let table = inventur_db::sort_table(table, sortfield, sortdir);
|
||||
|
||||
let tname = table.name;
|
||||
let columns = table.column_names;
|
||||
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;
|
||||
|
||||
@@ -124,13 +149,24 @@ pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<
|
||||
tblname: tname,
|
||||
tids: tids,
|
||||
tnames: tnames,
|
||||
columns: columns,
|
||||
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;
|
||||
@@ -142,6 +178,40 @@ pub async fn new_entry(conn: Db, data: Form<NewEntry>, user: AuthUser) -> Result
|
||||
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("/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;
|
||||
@@ -155,7 +225,7 @@ pub async fn edit_tname(conn: Db, data: Form<EditTname>, user: AuthUser) -> Resu
|
||||
#[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.fields.clone(), uid)).await;
|
||||
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);
|
||||
}
|
||||
@@ -167,7 +237,7 @@ pub async fn import_table(conn: Db, data: Json<ImportTable>, user: AuthUser) ->
|
||||
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, uid)).await;
|
||||
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);
|
||||
}
|
||||
@@ -177,4 +247,3 @@ pub async fn import_table(conn: Db, data: Json<ImportTable>, user: AuthUser) ->
|
||||
}
|
||||
Ok(Redirect::to(uri!("/table", table_sec(tblid))))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user