snotes-deck/libsnotes/src/lib.rs

137 lines
3.6 KiB
Rust
Raw Normal View History

2024-04-07 21:31:08 +02:00
use chrono::Local;
use home::home_dir;
use rusqlite::{Connection, Result};
2024-04-12 00:01:15 +02:00
use serde_json::json;
2024-04-07 21:31:08 +02:00
pub struct Note {
2024-04-12 00:01:15 +02:00
id: i32,
2024-04-07 21:31:08 +02:00
content: String,
date: String,
tag: String,
}
pub fn init_db() -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home)?;
let query_table = "
CREATE TABLE IF NOT EXISTS notes (
nid INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
date TEXT NOT NULL,
tag TEXT
);
";
match connection.execute(query_table, []) {
Ok(_) => (),
Err(e) => println!("INIT ERR {}", e),
};
Ok(())
}
pub fn create_note(content: &String, tag: &String) -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home)?;
let date = Local::now();
let date_string = date.format("%m-%d-%y %H:%M").to_string();
let tag_string = if *tag == String::new() {
String::from("quick")
} else {
tag.to_string()
};
let query_insert = "INSERT INTO notes (content, date, tag) VALUES (?1, ?2, ?3)";
match connection.execute(query_insert, [&content, &date_string, &tag_string]) {
Ok(v) => println!("CREATE OK {}", v),
Err(e) => println!("CREATE ERR {}", e),
};
Ok(())
}
2024-04-12 00:01:15 +02:00
pub fn show_notes(all: bool, tag: &str) -> Result<String, String> {
2024-04-07 21:31:08 +02:00
let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home).unwrap();
2024-04-12 00:01:15 +02:00
let mut query = "SELECT * FROM notes LIMIT 10".to_string();
2024-04-07 21:31:08 +02:00
if all {
2024-04-12 00:01:15 +02:00
query = "SELECT * FROM notes".to_string();
2024-04-07 21:31:08 +02:00
}
2024-04-12 00:01:15 +02:00
if !tag.is_empty() {
query = format!("SELECT * FROM notes WHERE tag IS '{tag}'");
2024-04-07 21:31:08 +02:00
}
let mut prepare = connection.prepare(&query).unwrap();
let notes = prepare
.query_map([], |row| {
Ok(Note {
id: row.get(0)?,
content: row.get(1)?,
date: row.get(2)?,
tag: row.get(3)?,
})
2024-04-07 21:31:08 +02:00
})
.unwrap();
2024-04-07 21:31:08 +02:00
2024-04-12 00:01:15 +02:00
let mut json_array = Vec::new();
2024-04-07 21:31:08 +02:00
for note in notes {
let unwrapped = note.unwrap();
2024-04-12 00:01:15 +02:00
let note_json = json!({
"id": unwrapped.id,
"date": unwrapped.date,
"content": unwrapped.content,
"tag": unwrapped.tag
});
json_array.push(note_json);
2024-04-07 21:31:08 +02:00
}
2024-04-12 00:01:15 +02:00
let json_string = serde_json::to_string(&json_array).unwrap();
Ok(json_string)
2024-04-07 21:31:08 +02:00
}
pub fn delete_latest_note() -> Result<(), String> {
2024-04-07 21:31:08 +02:00
let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home).map_err(|e| format!("Database Error: {e}"))?;
2024-04-07 21:31:08 +02:00
let query = String::from("DELETE FROM NOTES WHERE nid = (SELECT MAX(nid) FROM notes)");
match connection.execute(&query, []) {
Ok(v) => {
println!("DELETE OK {}", v);
Ok(())
}
Err(e) => Err(format!("Delete Error: {e}")),
2024-04-07 21:31:08 +02:00
}
}
pub fn delete_specific_note(id: i32) -> Result<(), String> {
let home = home_dir().unwrap().join(".snotes.db");
let connection = Connection::open(home).map_err(|e| format!("Database Error: {e}"))?;
let query = "DELETE FROM notes WHERE nid = ?1";
match connection.execute(query, [id]) {
Ok(1) => Ok(()), // 1 row affected means the note was deleted successfully
Ok(_) => Err("No note with the provided ID found.".to_string()),
Err(e) => Err(format!("Delete Error: {e}")),
}
}
2024-04-07 21:31:08 +02:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = init_db();
assert_eq!(result, Ok(()));
}
}