work on saving continuing

This commit is contained in:
Iaphetes 2025-03-22 10:33:24 +01:00
parent 75cd9752a4
commit d08de3d2b4
3 changed files with 58 additions and 13 deletions

View file

@ -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(())
}