return json with id instead

This commit is contained in:
catto 2024-04-12 00:01:15 +02:00
parent d6e3f840b4
commit 9a40a48538
5 changed files with 67 additions and 12 deletions

44
libsnotes/Cargo.lock generated
View File

@ -148,6 +148,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.69" version = "0.3.69"
@ -170,6 +176,7 @@ dependencies = [
"chrono", "chrono",
"home", "home",
"rusqlite", "rusqlite",
"serde_json",
] ]
[[package]] [[package]]
@ -242,6 +249,43 @@ dependencies = [
"smallvec", "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]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.13.2"

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.37" chrono = "0.4.37"
home = "0.5.9" home = "0.5.9"
serde_json = "1.0.115"
[dependencies.rusqlite] [dependencies.rusqlite]
version = "0.31.0" version = "0.31.0"
features = ["bundled"] features = ["bundled"]

View File

@ -1,8 +1,10 @@
use chrono::Local; use chrono::Local;
use home::home_dir; use home::home_dir;
use rusqlite::{Connection, Result}; use rusqlite::{Connection, Result};
use serde_json::json;
pub struct Note { pub struct Note {
id: i32,
content: String, content: String,
date: String, date: String,
tag: String, tag: String,
@ -51,17 +53,17 @@ pub fn create_note(content: &String, tag: &String) -> Result<()> {
Ok(()) Ok(())
} }
pub fn show_notes(all: bool, tag: &String) -> Result<String, String> { pub fn show_notes(all: bool, tag: &str) -> Result<String, String> {
let home = home_dir().unwrap().join(".snotes.db"); let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home).unwrap(); 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 { 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); query = format!("SELECT * FROM notes WHERE tag IS '{}'", tag);
} }
@ -69,24 +71,29 @@ pub fn show_notes(all: bool, tag: &String) -> Result<String, String> {
let notes = prepare.query_map([], |row| { let notes = prepare.query_map([], |row| {
Ok(Note { Ok(Note {
id: row.get(0)?,
content: row.get(1)?, content: row.get(1)?,
date: row.get(2)?, date: row.get(2)?,
tag: row.get(3)?, tag: row.get(3)?,
}) })
}).unwrap(); }).unwrap();
let mut noteresult = String::new(); // Initialize an empty result string let mut json_array = Vec::new();
for note in notes { for note in notes {
let unwrapped = note.unwrap(); let unwrapped = note.unwrap();
// Append each note to the result string let note_json = json!({
noteresult.push_str(&format!( "id": unwrapped.id,
"{0} #{2}: {1}\n", "date": unwrapped.date,
&unwrapped.date, &unwrapped.content, &unwrapped.tag "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<()> { pub fn delete_latest_note() -> Result<()> {

1
src-tauri/Cargo.lock generated
View File

@ -1469,6 +1469,7 @@ dependencies = [
"chrono", "chrono",
"home", "home",
"rusqlite", "rusqlite",
"serde_json",
] ]
[[package]] [[package]]

View File

@ -23,7 +23,9 @@ async function createNote() {
// read // read
async function showNotes() { async function showNotes() {
if (notesMsgEl) { 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;
} }
} }