save db in new folder

This commit is contained in:
catto 2024-04-23 13:07:04 +02:00
parent efb306ac05
commit 61d1776239
4 changed files with 65 additions and 19 deletions

View File

@ -30,6 +30,7 @@
<button class="row" id="new-button">New</button> <button class="row" id="new-button">New</button>
<button class="row" id="image-button">OCR</button> <button class="row" id="image-button">OCR</button>
<button class="row" id="export-button">Export</button> <button class="row" id="export-button">Export</button>
<button class="row" id="settings-button">Settings</button>
<input type="file" id="fileInput" accept="image/*" style="display: none;" /> <input type="file" id="fileInput" accept="image/*" style="display: none;" />
</div> </div>
</div> </div>
@ -67,7 +68,14 @@
</div> </div>
</div> </div>
<div id="settings-modal-container">
<div class="settings-flex">
<div class="fontsize-setting">
<h3>Font Size (in px)</h3>
<input type="number" name="fontsize" id="fontsize-setting-input" placeholder="">
</div>
</div>
</div>
</body> </body>

View File

@ -2,6 +2,8 @@ use chrono::Local;
use home::home_dir; use home::home_dir;
use rusqlite::{Connection, Result}; use rusqlite::{Connection, Result};
use serde_json::json; use serde_json::json;
use std::fs;
use std::path::PathBuf;
pub struct Note { pub struct Note {
id: i32, id: i32,
@ -10,9 +12,19 @@ pub struct Note {
tag: String, tag: String,
} }
fn get_db_dir() -> PathBuf {
let dir = home_dir().unwrap().join(".snotes-data");
dbg!(&dir);
if !&dir.exists() {
fs::create_dir(&dir).unwrap();
}
dir.join(".snotes.db")
}
pub fn init_db() -> Result<()> { pub fn init_db() -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home)?; let connection = Connection::open(db)?;
let query_table = " let query_table = "
CREATE TABLE IF NOT EXISTS notes ( CREATE TABLE IF NOT EXISTS notes (
@ -32,8 +44,8 @@ pub fn init_db() -> Result<()> {
} }
pub fn create_note(content: &String, tag: &String) -> Result<()> { pub fn create_note(content: &String, tag: &String) -> Result<()> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home)?; let connection = Connection::open(db)?;
let date = Local::now(); let date = Local::now();
let date_string = date.format("%m-%d-%y %H:%M").to_string(); let date_string = date.format("%m-%d-%y %H:%M").to_string();
let tag_string = if *tag == String::new() { let tag_string = if *tag == String::new() {
@ -53,8 +65,8 @@ pub fn create_note(content: &String, tag: &String) -> Result<()> {
} }
pub fn show_notes(all: bool, tag: &str) -> Result<String, String> { pub fn show_notes(all: bool, tag: &str) -> Result<String, String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).unwrap(); let connection = Connection::open(db).unwrap();
let mut query = "SELECT * FROM notes LIMIT 10".to_string(); let mut query = "SELECT * FROM notes LIMIT 10".to_string();
@ -98,8 +110,8 @@ pub fn show_notes(all: bool, tag: &str) -> Result<String, String> {
} }
pub fn delete_latest_note() -> Result<(), String> { pub fn delete_latest_note() -> Result<(), String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {e}"))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {e}"))?;
let query = String::from("DELETE FROM NOTES WHERE nid = (SELECT MAX(nid) FROM notes)"); let query = String::from("DELETE FROM NOTES WHERE nid = (SELECT MAX(nid) FROM notes)");
@ -113,8 +125,8 @@ pub fn delete_latest_note() -> Result<(), String> {
} }
pub fn delete_specific_note(id: i32) -> Result<(), String> { pub fn delete_specific_note(id: i32) -> Result<(), String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {e}"))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {e}"))?;
let query = "DELETE FROM notes WHERE nid = ?1"; let query = "DELETE FROM notes WHERE nid = ?1";
match connection.execute(query, [id]) { match connection.execute(query, [id]) {
@ -125,8 +137,8 @@ pub fn delete_specific_note(id: i32) -> Result<(), String> {
} }
pub fn edit_specific_note(id: i32, tag: &str, content: &str) -> Result<(), String> { pub fn edit_specific_note(id: i32, tag: &str, content: &str) -> Result<(), String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {}", e))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {}", e))?;
let query = "UPDATE notes SET tag = ?1, content = ?2 WHERE nid = ?3"; let query = "UPDATE notes SET tag = ?1, content = ?2 WHERE nid = ?3";
match connection.execute(query, [&tag, &content, &id.to_string().as_str()]) { match connection.execute(query, [&tag, &content, &id.to_string().as_str()]) {
@ -138,8 +150,8 @@ pub fn edit_specific_note(id: i32, tag: &str, content: &str) -> Result<(), Strin
/// Looks for matches in both content and tag. /// Looks for matches in both content and tag.
pub fn search_notes(query: &str) -> Result<String, String> { pub fn search_notes(query: &str) -> Result<String, String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {}", e))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {}", e))?;
let query = format!( let query = format!(
"SELECT * FROM notes WHERE nid LIKE '%{}%' OR content LIKE '%{}%' OR tag LIKE '%{}%'", "SELECT * FROM notes WHERE nid LIKE '%{}%' OR content LIKE '%{}%' OR tag LIKE '%{}%'",
@ -183,8 +195,8 @@ pub fn search_notes(query: &str) -> Result<String, String> {
/// get latest note /// get latest note
/// Returns a json array string of size 1 /// Returns a json array string of size 1
pub fn get_latest_note() -> Result<String, String> { pub fn get_latest_note() -> Result<String, String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {}", e))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {}", e))?;
let query = "SELECT * FROM notes WHERE ROWID IN (SELECT max(ROWID) FROM notes); let query = "SELECT * FROM notes WHERE ROWID IN (SELECT max(ROWID) FROM notes);
" "
@ -224,8 +236,8 @@ pub fn get_latest_note() -> Result<String, String> {
} }
pub fn get_note_by_id(id: u32) -> Result<String, String> { pub fn get_note_by_id(id: u32) -> Result<String, String> {
let home = home_dir().unwrap().join(".snotes.db"); let db = get_db_dir();
let connection = Connection::open(home).map_err(|e| format!("Database Error: {}", e))?; let connection = Connection::open(db).map_err(|e| format!("Database Error: {}", e))?;
let query = format!("SELECT * FROM notes WHERE nid IS {};", id.to_string()); let query = format!("SELECT * FROM notes WHERE nid IS {};", id.to_string());
let mut prepare = connection let mut prepare = connection

View File

@ -145,6 +145,10 @@ window.addEventListener("DOMContentLoaded", async () => {
e.preventDefault(); e.preventDefault();
exportNote(createNoteContentEl ? createNoteContentEl.value : null); exportNote(createNoteContentEl ? createNoteContentEl.value : null);
}) })
document.querySelector('#settings-button')?.addEventListener("click", (e) => {
e.preventDefault();
handleOpenSettingsModal();
})
// Pressing TAB should insert intends in the editor. // Pressing TAB should insert intends in the editor.
// This could potentially cause issues later... // This could potentially cause issues later...
@ -611,3 +615,14 @@ async function exportNote(contents: string | null) {
} }
} }
function handleOpenSettingsModal() {
const modalBg = document.getElementById("id-modal-bg");
const setingsModalContainer = document.getElementById("settings-modal-container");
const settingsFontsizeInput = document.getElementById("fontsize-setting-input");
if (modalBg && setingsModalContainer && settingsFontsizeInput) {
console.log("settings")
} else {
console.error("Failed to get Settings Modal elements.");
}
}

View File

@ -264,6 +264,17 @@ button {
z-index: 3; z-index: 3;
} }
#settings-modal-container {
display: none;
position: fixed;
margin-top: 20%;
margin-left: 50%;
z-index: 4;
}
/* MISC */ /* MISC */
/* Fancier Scrollbar */ /* Fancier Scrollbar */