76 lines
2.8 KiB
Rust
76 lines
2.8 KiB
Rust
/* 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/>.
|
|
*/
|
|
|
|
//! Submodule responsible for manipulating and creating a table's column.
|
|
//! For all methods in this module, the authenticated user must be the table's owner.
|
|
|
|
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;
|
|
|
|
/// Add a new column to an existing table
|
|
#[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))))
|
|
}
|
|
|
|
/// Edit a column (its name and type)
|
|
#[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))))
|
|
}
|
|
|
|
/// Delete a column
|
|
#[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))))
|
|
}
|
|
|