From 9a40a48538d05c7b0969891bf6d3bbcc85e74ae3 Mon Sep 17 00:00:00 2001 From: catto Date: Fri, 12 Apr 2024 00:01:15 +0200 Subject: [PATCH] return json with id instead --- libsnotes/Cargo.lock | 44 ++++++++++++++++++++++++++++++++++++++++++++ libsnotes/Cargo.toml | 1 + libsnotes/src/lib.rs | 29 ++++++++++++++++++----------- src-tauri/Cargo.lock | 1 + src/main.ts | 4 +++- 5 files changed, 67 insertions(+), 12 deletions(-) diff --git a/libsnotes/Cargo.lock b/libsnotes/Cargo.lock index 6e120cc..ed55c3c 100644 --- a/libsnotes/Cargo.lock +++ b/libsnotes/Cargo.lock @@ -148,6 +148,12 @@ dependencies = [ "cc", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "js-sys" version = "0.3.69" @@ -170,6 +176,7 @@ dependencies = [ "chrono", "home", "rusqlite", + "serde_json", ] [[package]] @@ -242,6 +249,43 @@ dependencies = [ "smallvec", ] +[[package]] +name = "ryu" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" + +[[package]] +name = "serde" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "smallvec" version = "1.13.2" diff --git a/libsnotes/Cargo.toml b/libsnotes/Cargo.toml index 9e1fc77..a690533 100644 --- a/libsnotes/Cargo.toml +++ b/libsnotes/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] chrono = "0.4.37" home = "0.5.9" +serde_json = "1.0.115" [dependencies.rusqlite] version = "0.31.0" features = ["bundled"] diff --git a/libsnotes/src/lib.rs b/libsnotes/src/lib.rs index d953217..924f42c 100644 --- a/libsnotes/src/lib.rs +++ b/libsnotes/src/lib.rs @@ -1,8 +1,10 @@ use chrono::Local; use home::home_dir; use rusqlite::{Connection, Result}; +use serde_json::json; pub struct Note { + id: i32, content: String, date: String, tag: String, @@ -51,17 +53,17 @@ pub fn create_note(content: &String, tag: &String) -> Result<()> { Ok(()) } -pub fn show_notes(all: bool, tag: &String) -> Result { +pub fn show_notes(all: bool, tag: &str) -> Result { let home = home_dir().unwrap().join(".snotes.db"); let connection = Connection::open(home).unwrap(); - let mut query = String::from("SELECT * FROM notes LIMIT 10"); + let mut query = "SELECT * FROM notes LIMIT 10".to_string(); if all { - query = String::from("SELECT * FROM notes"); + query = "SELECT * FROM notes".to_string(); } - if tag != &String::new() { + if !tag.is_empty() { query = format!("SELECT * FROM notes WHERE tag IS '{}'", tag); } @@ -69,24 +71,29 @@ pub fn show_notes(all: bool, tag: &String) -> Result { let notes = prepare.query_map([], |row| { Ok(Note { + id: row.get(0)?, content: row.get(1)?, date: row.get(2)?, tag: row.get(3)?, }) }).unwrap(); - let mut noteresult = String::new(); // Initialize an empty result string + let mut json_array = Vec::new(); for note in notes { let unwrapped = note.unwrap(); - // Append each note to the result string - noteresult.push_str(&format!( - "{0} #{2}: {1}\n", - &unwrapped.date, &unwrapped.content, &unwrapped.tag - )); + let note_json = json!({ + "id": unwrapped.id, + "date": unwrapped.date, + "content": unwrapped.content, + "tag": unwrapped.tag + }); + json_array.push(note_json); } - Ok(noteresult) // Return the concatenated result string + let json_string = serde_json::to_string(&json_array).unwrap(); + + Ok(json_string) } pub fn delete_latest_note() -> Result<()> { diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index a6c7d42..7e48332 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1469,6 +1469,7 @@ dependencies = [ "chrono", "home", "rusqlite", + "serde_json", ] [[package]] diff --git a/src/main.ts b/src/main.ts index e052443..6c7d8d5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,7 +23,9 @@ async function createNote() { // read async function showNotes() { if (notesMsgEl) { - notesMsgEl.textContent = await invoke("get_notes_list"); + const notesJson : string = await invoke("get_notes_list"); + const formattedJson = JSON.stringify(JSON.parse(notesJson), null, 2); // Indentation of 2 spaces + notesMsgEl.textContent = formattedJson; } }