|
1 | 1 | use grid_search_desktop::{Error, ExperimentFile};
|
2 |
| -use std::fs; |
3 | 2 |
|
4 |
| -#[tauri::command] |
5 |
| -pub fn get_experiments(app_handle: tauri::AppHandle) -> Result<Vec<ExperimentFile>, Error> { |
6 |
| - let binding = app_handle.path_resolver().app_data_dir().unwrap(); |
7 |
| - let app_data_dir = binding.to_str().unwrap(); |
8 |
| - let mut files: Vec<ExperimentFile> = fs::read_dir(app_data_dir)? |
9 |
| - .filter_map(Result::ok) |
10 |
| - .filter_map(|entry| { |
11 |
| - let path = entry.path(); |
12 |
| - if path.extension().unwrap_or_default() != "json" { |
13 |
| - return None; |
14 |
| - } |
15 |
| - let metadata = fs::metadata(&path).ok()?; |
16 |
| - let created = metadata.created().ok()?; |
17 |
| - let contents = fs::read_to_string(&path).ok()?; |
18 |
| - Some(ExperimentFile { |
19 |
| - name: path.file_name()?.to_string_lossy().into_owned(), |
20 |
| - created, |
21 |
| - contents, |
22 |
| - }) |
23 |
| - }) |
24 |
| - .collect(); |
| 3 | +use crate::db::DatabaseState; |
25 | 4 |
|
26 |
| - files.sort_by_key(|file| file.created); |
27 |
| - files.reverse(); |
28 |
| - Ok(files) |
| 5 | +#[tauri::command] |
| 6 | +pub async fn get_experiments( |
| 7 | + state: tauri::State<'_, DatabaseState>, |
| 8 | +) -> Result<Vec<ExperimentFile>, Error> { |
| 9 | + let stmt = r#" |
| 10 | + SELECT |
| 11 | + name, |
| 12 | + created, |
| 13 | + contents |
| 14 | + FROM experiments |
| 15 | + ORDER BY id DESC |
| 16 | + "#; |
| 17 | + |
| 18 | + let query = sqlx::query_as::<_, ExperimentFile>(stmt); |
| 19 | + let pool = &state.0; |
| 20 | + let experiments = query.fetch_all(pool).await?; |
| 21 | + |
| 22 | + println!("\nRetrieved {} experiments:", experiments.len()); |
| 23 | + Ok(experiments) |
29 | 24 | }
|
30 | 25 |
|
31 |
| -// --- |
32 |
| -// Example below connects to the database |
33 |
| -// --- |
34 |
| - |
35 |
| -// #[tauri::command] |
36 |
| -// pub async fn get_experiments( |
37 |
| -// state: tauri::State<'_, DatabaseState>, |
38 |
| -// ) -> Result<Vec<ExperimentFile>, Error> { |
39 |
| -// // Access the database pool |
40 |
| -// let pool = &state.0; |
41 |
| - |
42 |
| -// // Example query |
43 |
| -// let experiments = |
44 |
| -// sqlx::query!("SELECT name, created, contents FROM experiments ORDER BY created DESC") |
45 |
| -// .fetch_all(pool) |
46 |
| -// .await |
47 |
| -// .map_err(|e| Error::StringError(e.to_string()))?; |
48 |
| - |
49 |
| -// // Convert to your ExperimentFile struct |
50 |
| -// let files: Vec<ExperimentFile> = experiments |
51 |
| -// .into_iter() |
52 |
| -// .map(|row| ExperimentFile { |
53 |
| -// name: row.name, |
54 |
| -// created: SystemTime::from(chrono::DateTime::parse_from_rfc3339(&row.created).unwrap()), |
55 |
| -// contents: row.contents, |
56 |
| -// }) |
57 |
| -// .collect(); |
| 26 | +#[tauri::command] |
| 27 | +pub async fn delete_experiments( |
| 28 | + state: tauri::State<'_, DatabaseState>, |
| 29 | + uuid: String, |
| 30 | +) -> Result<(), Error> { |
| 31 | + let pool = &state.0; |
58 | 32 |
|
59 |
| -// Ok(files) |
60 |
| -// } |
| 33 | + let stmt: &str = match uuid.as_str() { |
| 34 | + "*" => "DELETE FROM experiments", |
| 35 | + _ => "DELETE FROM experiments WHERE experiment_uuid = $1", |
| 36 | + }; |
61 | 37 |
|
62 |
| -#[tauri::command] |
63 |
| -pub fn delete_experiment_files( |
64 |
| - app_handle: tauri::AppHandle, |
65 |
| - file_name: String, |
66 |
| -) -> Result<(), String> { |
67 |
| - let app_data_dir = app_handle |
68 |
| - .path_resolver() |
69 |
| - .app_data_dir() |
70 |
| - .ok_or_else(|| "Failed to get application data directory".to_string())?; |
| 38 | + let _ = sqlx::query(stmt).bind(&uuid).execute(pool).await?; |
| 39 | + print!("Deleted experiment with UUID: {}", uuid); |
71 | 40 |
|
72 |
| - if file_name == "*" { |
73 |
| - // Delete all JSON files in the directory |
74 |
| - for entry in fs::read_dir(&app_data_dir).map_err(|e| e.to_string())? { |
75 |
| - let entry = entry.map_err(|e| e.to_string())?; |
76 |
| - let path = entry.path(); |
77 |
| - if path.is_file() && path.extension().map(|e| e == "json").unwrap_or(false) { |
78 |
| - fs::remove_file(path).map_err(|e| e.to_string())?; |
79 |
| - } |
80 |
| - } |
81 |
| - Ok(()) |
82 |
| - } else { |
83 |
| - // Delete a specific file |
84 |
| - let file_path = app_data_dir.join(file_name); |
85 |
| - if file_path.exists() && file_path.is_file() { |
86 |
| - fs::remove_file(file_path).map_err(|e| e.to_string())?; |
87 |
| - Ok(()) |
88 |
| - } else { |
89 |
| - // File doesn't exist, do nothing |
90 |
| - Ok(()) |
91 |
| - } |
92 |
| - } |
| 41 | + Ok(()) |
93 | 42 | }
|
0 commit comments