diff --git a/lab2/.env b/lab2/.env index 5fe80b9..7244267 100644 --- a/lab2/.env +++ b/lab2/.env @@ -1,2 +1 @@ -RUST_BACKTRACE=1 DATABASE_URL="mysql://root@localhost/ice_cream" \ No newline at end of file diff --git a/lab2/.vscode/settings.json b/lab2/.vscode/settings.json index 3ef0f03..05bd5e5 100644 --- a/lab2/.vscode/settings.json +++ b/lab2/.vscode/settings.json @@ -1,5 +1,6 @@ { "rust-analyzer.linkedProjects": [ + ".\\src-tauri\\Cargo.toml", ".\\src-tauri\\Cargo.toml" ] } \ No newline at end of file diff --git a/lab2/src-tauri/src/api.rs b/lab2/src-tauri/src/api.rs index b12b3ab..cbac816 100644 --- a/lab2/src-tauri/src/api.rs +++ b/lab2/src-tauri/src/api.rs @@ -1,4 +1,4 @@ -use crate::{models::{Manager, Factory, ManagerData, FactoryData, Id}, manager_repo, factory_repo}; +use crate::{models::{Manager, Factory, ManagerData, FactoryData, Id, Process, ProcessData}, manager_repo, factory_repo, process_repo}; use sqlx::{Pool, MySql}; use tauri::State; @@ -21,12 +21,14 @@ pub async fn add_manager_factory( Ok((factory_id, manager_id)) } +// --------------------- Factory --------------------------- + #[tauri::command] -pub async fn delete_factory(id: Id, db: State<'_, Database>) -> Result<()> { +pub async fn list_factories(db: State<'_, Database>) -> Result> { let mut tx = db.pool.begin().await?; - factory_repo::delete(&mut tx, id).await?; + let factories = factory_repo::list(&mut tx).await?; tx.commit().await?; - Ok(()) + Ok(factories) } #[tauri::command] @@ -38,13 +40,15 @@ pub async fn update_factory(id: Id, factory: FactoryData, db: State<'_, Database } #[tauri::command] -pub async fn update_manager(id: Id, manager: ManagerData, db: State<'_, Database>) -> Result<()> { +pub async fn delete_factory(id: Id, db: State<'_, Database>) -> Result<()> { let mut tx = db.pool.begin().await?; - manager_repo::update(&mut tx, id, &manager).await?; + factory_repo::delete(&mut tx, id).await?; tx.commit().await?; Ok(()) } +// --------------------- Manager --------------------------- + #[tauri::command] pub async fn list_managers(db: State<'_, Database>) -> Result> { let mut tx = db.pool.begin().await?; @@ -54,10 +58,47 @@ pub async fn list_managers(db: State<'_, Database>) -> Result> { } #[tauri::command] -pub async fn list_factories(db: State<'_, Database>) -> Result> { +pub async fn update_manager(id: Id, manager: ManagerData, db: State<'_, Database>) -> Result<()> { let mut tx = db.pool.begin().await?; - let factories = factory_repo::list(&mut tx).await?; + manager_repo::update(&mut tx, id, &manager).await?; tx.commit().await?; - Ok(factories) + Ok(()) } + +// --------------------- Process --------------------------- + +#[tauri::command] +pub async fn list_processess(db: State<'_, Database>) -> Result> { + let mut tx = db.pool.begin().await?; + let processess = process_repo::list(&mut tx).await?; + tx.commit().await?; + Ok(processess) +} + +#[tauri::command] +pub async fn delete_process(id: Id, db: State<'_, Database>) -> Result<()> { + let mut tx = db.pool.begin().await?; + process_repo::delete(&mut tx, id).await?; + tx.commit().await?; + Ok(()) +} + +#[tauri::command] +pub async fn update_process(id: Id, process: ProcessData, db: State<'_, Database>) -> Result<()> { + let mut tx = db.pool.begin().await?; + process_repo::update(&mut tx, id, &process).await?; + tx.commit().await?; + Ok(()) +} + +#[tauri::command] +pub async fn add_process( + process: ProcessData, + db: State<'_, Database> + ) -> Result { + let mut tx = db.pool.begin().await?; + let id = process_repo::add(&mut tx, &process).await?; + tx.commit().await?; + Ok(id) +} \ No newline at end of file diff --git a/lab2/src-tauri/src/factory_repo.rs b/lab2/src-tauri/src/factory_repo.rs index faa152d..39e3dec 100644 --- a/lab2/src-tauri/src/factory_repo.rs +++ b/lab2/src-tauri/src/factory_repo.rs @@ -7,7 +7,7 @@ type MySqlTransaction<'a> = Transaction<'a, MySql>; pub async fn create_table(tx: &mut MySqlTransaction<'_>) -> Result<()> { sqlx::query(r#" CREATE TABLE IF NOT EXISTS `factory` ( - ID bigint unsigned NOT NULL, + ID bigint unsigned NOT NULL AUTO_INCREMENT, NAME varchar(255) NOT NULL, LOCATION varchar(255) NOT NULL, FLOOR_SIZE float NOT NULL, diff --git a/lab2/src-tauri/src/main.rs b/lab2/src-tauri/src/main.rs index 542f00d..3721b58 100644 --- a/lab2/src-tauri/src/main.rs +++ b/lab2/src-tauri/src/main.rs @@ -4,12 +4,13 @@ mod models; mod factory_repo; mod manager_repo; +mod process_repo; mod api; use std::{env, process::exit}; use dotenv::dotenv; -use models::{ManagerData, FactoryData}; +use models::{ManagerData, FactoryData, ProcessData}; use sqlx::{Pool, MySql, mysql::MySqlPoolOptions, Row}; use api::*; @@ -19,6 +20,7 @@ async fn setup_tables(pool: &Pool) -> Result<()> { let mut tx = pool.begin().await?; manager_repo::create_table(&mut tx).await?; factory_repo::create_table(&mut tx).await?; + process_repo::create_table(&mut tx).await?; tx.commit().await?; Ok(()) } @@ -33,8 +35,12 @@ async fn drop_all_tables(pool: &Pool) -> Result<()> { Ok(()) } -async fn enable_foreign_key_checks(pool: &Pool) -> Result<()> { - sqlx::query("SET GLOBAL FOREIGN_KEY_CHECKS=1").execute(pool).await?; +async fn set_foreign_key_checks(pool: &Pool, enable: bool) -> Result<()> { + let query = match enable { + true => "SET GLOBAL FOREIGN_KEY_CHECKS=1", + false => "SET GLOBAL FOREIGN_KEY_CHECKS=0", + }; + sqlx::query(query).execute(pool).await?; Ok(()) } @@ -52,8 +58,13 @@ async fn add_test_data(pool: &Pool) -> Result<()> { location: "idk".into(), floor_size: 10.0, }; + let process = ProcessData { + name: "Certifuge 9000".into(), + size: 10.0 + }; let id = manager_repo::add(&mut tx, &manager).await?; factory_repo::add(&mut tx, id, &factory).await?; + process_repo::add(&mut tx, &process).await?; tx.commit().await?; Ok(()) @@ -77,8 +88,9 @@ async fn main() { .await .unwrap(); - enable_foreign_key_checks(&pool).await.expect("Enable foreign key checks"); + set_foreign_key_checks(&pool, false).await.expect("Disable foreign key checks"); drop_all_tables(&pool).await.unwrap(); // For testing purposes + set_foreign_key_checks(&pool, true).await.expect("Enable foreign key checks"); setup_tables(&pool).await.expect("Setup tables"); add_test_data(&pool).await.expect("Add test data"); @@ -86,12 +98,19 @@ async fn main() { tauri::Builder::default() .manage(Database { pool }) .invoke_handler(tauri::generate_handler![ - list_factories, - list_managers, add_manager_factory, + + list_factories, delete_factory, update_factory, - update_manager + + list_managers, + update_manager, + + list_processess, + update_process, + delete_process, + add_process ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/lab2/src-tauri/src/manager_repo.rs b/lab2/src-tauri/src/manager_repo.rs index fd2bd48..bc783b6 100644 --- a/lab2/src-tauri/src/manager_repo.rs +++ b/lab2/src-tauri/src/manager_repo.rs @@ -9,12 +9,13 @@ type MySqlTransaction<'a> = Transaction<'a, MySql>; pub async fn create_table(tx: &mut MySqlTransaction<'_>) -> Result<()> { sqlx::query(r#" CREATE TABLE IF NOT EXISTS `manager` ( - ID bigint unsigned NOT NULL, + ID bigint unsigned NOT NULL AUTO_INCREMENT, FIRST_NAME varchar(255) NOT NULL, SURNAME varchar(255) NOT NULL, PHONE_NUMBER varchar(255) NULL, TITLE varchar(255) NOT NULL, EMAIL varchar(255) NULL, + PRIMARY KEY(ID) );"#) .execute(tx).await?; diff --git a/lab2/src-tauri/src/models.rs b/lab2/src-tauri/src/models.rs index ec68da1..2ef6f60 100644 --- a/lab2/src-tauri/src/models.rs +++ b/lab2/src-tauri/src/models.rs @@ -36,4 +36,17 @@ pub struct FactoryData { pub name: String, pub location: String, pub floor_size: f32 +} + +#[derive(Debug, Serialize, FromRow)] +pub struct Process { + pub id: Id, + pub name: String, + pub size: f32 +} + +#[derive(Debug, Deserialize)] +pub struct ProcessData { + pub name: String, + pub size: f32 } \ No newline at end of file diff --git a/lab2/src-tauri/src/process_repo.rs b/lab2/src-tauri/src/process_repo.rs new file mode 100644 index 0000000..101019d --- /dev/null +++ b/lab2/src-tauri/src/process_repo.rs @@ -0,0 +1,83 @@ +use anyhow::Result; +use sqlx::{Transaction, MySql}; + +use crate::models::{ProcessData, Id, Process}; + +type MySqlTransaction<'a> = Transaction<'a, MySql>; + +pub async fn create_table(tx: &mut MySqlTransaction<'_>) -> Result<()> { + sqlx::query(r#" + CREATE TABLE IF NOT EXISTS `process` ( + ID bigint unsigned NOT NULL AUTO_INCREMENT, + NAME varchar(255) NOT NULL, + SIZE float NOT NULL, + + PRIMARY KEY(ID) + );"#) + .execute(&mut *tx).await?; + sqlx::query(r#" + CREATE TABLE `factory_supports_processes` ( + FK_PROCESS_ID bigint unsigned NOT NULL, + FK_FACTORY_ID bigint unsigned NOT NULL, + + PRIMARY KEY(FK_PROCESS_ID, FK_FACTORY_ID), + FOREIGN KEY(FK_PROCESS_ID) REFERENCES PROCESS (ID), + FOREIGN KEY(FK_FACTORY_ID) REFERENCES FACTORY (ID) + );"#) + .execute(&mut *tx).await?; + + Ok(()) +} + +pub async fn add(tx: &mut MySqlTransaction<'_>, process: &ProcessData) -> Result +{ + let id = sqlx::query(r#" + INSERT INTO `process` + (`NAME`, `SIZE`) + VALUES + (?, ?) + "#) + .bind(&process.name) + .bind(process.size) + .execute(&mut *tx).await? + .last_insert_id(); + + Ok(id) +} + +pub async fn list(tx: &mut MySqlTransaction<'_>) -> Result> { + let factories = sqlx::query_as::<_, Process>( + r#" + SELECT + ID as id, + NAME as name, + SIZE as size + FROM `process` + "#).fetch_all(tx).await?; + + Ok(factories) +} + +pub async fn delete(tx: &mut MySqlTransaction<'_>, id: Id) -> Result<()> { + sqlx::query("DELETE FROM `process` WHERE ID = ?") + .bind(id) + .execute(&mut *tx).await?; + + Ok(()) +} + +pub async fn update(tx: &mut MySqlTransaction<'_>, id: Id, process: &ProcessData) -> Result<()> { + sqlx::query( + r#" + UPDATE `process` SET + NAME = ?, + SIZE = ? + WHERE ID = ? + "#) + .bind(&process.name) + .bind(process.size) + .bind(id) + .execute(tx).await?; + + Ok(()) +} \ No newline at end of file diff --git a/lab2/src/App.svelte b/lab2/src/App.svelte index 4ab6150..b71deb9 100644 --- a/lab2/src/App.svelte +++ b/lab2/src/App.svelte @@ -1,10 +1,18 @@ @@ -13,11 +21,17 @@ + @@ -25,5 +39,6 @@ diff --git a/lab2/src/lib/MyDataTable.svelte b/lab2/src/lib/MyDataTable.svelte new file mode 100644 index 0000000..f4b0ba6 --- /dev/null +++ b/lab2/src/lib/MyDataTable.svelte @@ -0,0 +1,129 @@ + + + + + { + e.preventDefault(); + activeDelete = false; + }} + > + + + + + + + + + + {#if cell.key === "update_btn"} +