diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index bff8f63..7e8c121 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -55,6 +55,14 @@ pub fn get_basepath(app_handle: tauri::AppHandle) -> Option { return base_path.map(|p| p.to_string_lossy().into_owned()); } +pub fn _get_client_id(app_handle: &tauri::AppHandle) -> String { + let config = app_handle.state::>(); + + // Lock the mutex to get mutable access: + let config = config.lock().unwrap(); + let client_id = config.core_cfg.client_id.clone(); + return client_id; +} #[tauri::command] pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool { if let Ok(resolved_path) = shellexpand::full(&path) { diff --git a/src-tauri/src/database.rs b/src-tauri/src/database.rs index c0dd2d9..3e0d8d4 100644 --- a/src-tauri/src/database.rs +++ b/src-tauri/src/database.rs @@ -5,7 +5,7 @@ use std::{ str::FromStr, }; -use crate::get_basepath; +use crate::{config::_get_client_id, get_basepath}; use serde::{Deserialize, Serialize}; use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool}; use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; @@ -75,7 +75,7 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE id INTEGER NOT NULL, client_id TEXT NOT NULL, diff_text TEXT NOT NULL, - working_diff BOOLEAN, + current_diff BOOLEAN, FOREIGN KEY (id) REFERENCES file_diffs (parent_diff_id) FOREIGN KEY (id) @@ -171,33 +171,68 @@ async fn get_last_file_diff( Ok(parent_diff_id) } #[tauri::command] -async fn store_diff( +pub async fn store_diff( app_handle: &tauri::AppHandle, relative_file_path: &str, file_name: &str, diff: String, ) -> Result<(), DBError> { - let file_id = get_file_id(app_handle, relative_file_path, file_name) - .await - .map_err(|e| { - DBError::DatabaseQueryError("Got file diff for non existant file".to_string()) - }); - let unicode_db_path = get_database_path(&app_handle)?; + let client_id = _get_client_id(&app_handle); let db = SqlitePool::connect(&unicode_db_path) .await .map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?; + // 1 insert into file_diffs with parent + // 2 make last diff not currennt diff + // + // id INTEGER NOT NULL, + // client_id TEXT NOT NULL, + // diff_text TEXT NOT NULL, + // current_diff BOOLEAN, + // FOREIGN KEY (id) + // REFERENCES file_diffs (parent_diff_id) + // FOREIGN KEY (id) + // REFERENCES files (file_id) + // PRIMARY KEY (id, client_id) let _ = sqlx::query(&format!( " - INSERT INTO - file_diffs (relative_path, file_name) - VALUES - ('{relative_file_path}', '{file_name}');" + UPDATE file_diffs + SET current_diff = 0 + WHERE relative_path IS '{relative_file_path}' + AND file_name IS '{file_name}' + AND working_diff is 1 + " )) .execute(&db) .await .map_err(|e| DBError::DatabaseQueryError(e.to_string()))?; + let _ = sqlx::query(&format!( + " + INSERT INTO + file_diffs (client_id, diff_text, current_diff, file_diffs, file_id) + VALUES + ('{client_id}', '{diff}', 1, ( + SELECT id + FROM file_diffs + WHERE relative_path IS '{relative_file_path}' + AND file_name IS '{file_name}' + AND current_diff is 1 + LIMIT 1 + ), + ( + SELECT id + FROM files + WHERE relative_path IS '{relative_file_path}' + AND file_name IS '{file_name}' + LIMIT 1 + ));" + )) + .execute(&db) + .await + .map_err(|e| DBError::DatabaseQueryError(e.to_string()))?; + db.close().await; + Ok(()) } diff --git a/src-tauri/src/file_handler.rs b/src-tauri/src/file_handler.rs index 65e1faa..883cb5b 100644 --- a/src-tauri/src/file_handler.rs +++ b/src-tauri/src/file_handler.rs @@ -1,5 +1,6 @@ use crate::{ config::{get_open_file_path, set_open_file_path}, + database::store_diff, get_basepath, }; use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput}; @@ -24,6 +25,7 @@ fn compare_content(old_content: &str, new_content: &str) -> Result