This commit is contained in:
artin 2025-02-14 04:40:54 -08:00
parent 78b044f051
commit 1a94562052
5 changed files with 64 additions and 26 deletions

View File

@ -22,5 +22,5 @@ tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rusqlite = { version = "0.33.0", features = ["bundled"] }
rusqlite = { version = "0.33.0", features = ["bundled", "blob"] }

View File

@ -1,6 +1,9 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use rusqlite::{params,Connection, Result};
use serde::{Serialize, Deserialize};
use std::fs::File;
use std::io::prelude::*;
use std::fmt;
#[derive(Debug,Clone, Serialize)]
@ -14,6 +17,31 @@ struct Book {
fn search(q: &str) -> Vec<Book> {
searchdb(q).unwrap()
}
#[tauri::command]
fn copybook(bookid: i32) {
println!("copygins {}",bookid);
copybook_background(bookid);
}
fn copybook_background(bookid: i32) -> Result<()>{
let conn = Connection::open("/home/artin/Documents/ebooksol/index.db")?;
let mut stmt = conn.prepare("SELECT books.title, data.format , book_content.content FROM books, book_content ,data where books.id = book_content.id and books.id = data.book and books.id = ?1 ")?;
let mut rows = stmt.query([bookid])?;
while let Some(row) = rows.next()?{
let fname: String = row.get(0)?;
let ext : String = row.get(1)?;
// let filename = format!("{}.{}", row.get(0)?, row.get(1)?);
let filename = format!("{}.{}", fname, ext);
let mut content = Vec::new();
println!("loading");
content = row.get(2)?;
println!("opening");
let mut file = File::create(filename).unwrap();
file.write_all(&content[..]).unwrap();
println!("done");
}
Ok(())
}
fn searchdb(q: &str) -> Result<Vec<Book>>{
let conn = Connection::open("/home/artin/Documents/ebooksol/index.db")?;
@ -50,7 +78,7 @@ fn greet(name: &str) -> String {
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![search])
.invoke_handler(tauri::generate_handler![search,copybook])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -4,36 +4,24 @@
<meta charset="UTF-8" />
<link rel="stylesheet" href="styles.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tauri App</title>
<title>BookMobile</title>
<script type="module" src="/main.js" defer></script>
</head>
<body>
<main class="container">
<h1>Welcome to Tauri</h1>
<div class="row">
<a href="https://tauri.app" target="_blank">
<img src="/assets/tauri.svg" class="logo tauri" alt="Tauri logo" />
</a>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"
target="_blank"
>
<img
src="/assets/javascript.svg"
class="logo vanilla"
alt="JavaScript logo"
/>
</a>
</div>
<p>Click on the Tauri logo to learn more about the framework</p>
<h1>BookMobile</h1>
<form class="row" id="greet-form">
<input id="greet-input" placeholder="Enter a name..." />
<button type="submit">Greet</button>
<input id="greet-input" placeholder="Search a book title or author..." />
<button type="submit">Search</button>
</form>
<form class="row" id="copybook-form">
<input id="bookid" placeholder="bookid to pull" />
<button type="submit">copy</button>
</form>
<p id="greet-msg"></p>
<div id="thebooks"></div>
</main>
</body>
</html>

View File

@ -3,9 +3,19 @@ const { invoke } = window.__TAURI__.core;
let greetInputEl;
let greetMsgEl;
let slocate;
let bookid;
let tb;
async function search() {
slocate = await k();
console.log(slocate);
tb = document.querySelector("#thebooks");
slocate.forEach(function(book) {
let span = document.createElement("span");
span.innerHTML += `${book['title']} ${book['authors']} ${book['id']} <button id=${book['id']} onclick="copybook(${book['id']});" >${book['id']}</button><br>`;
tb.appendChild(span);
});
}
async function k() {
@ -15,12 +25,24 @@ async function greet() {
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
greetMsgEl.textContent = await invoke("greet", { name: greetInputEl.value });
}
async function cb() {
await copybook(parseInt(bookid.value));
}
async function copybook(bookid) {
await invoke("copybook", { bookid: bookid });
}
window.addEventListener("DOMContentLoaded", () => {
greetInputEl = document.querySelector("#greet-input");
greetMsgEl = document.querySelector("#greet-msg");
bookid = document.querySelector("#bookid");
document.querySelector("#greet-form").addEventListener("submit", (e) => {
e.preventDefault();
search();
});
document.querySelector("#copybook-form").addEventListener("submit", (e) => {
console.log("here");
e.preventDefault();
cb();
});
});

View File

@ -22,8 +22,8 @@
padding-top: 10vh;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
justify-content: left;
text-align: left;
}
.logo {
@ -39,7 +39,7 @@
.row {
display: flex;
justify-content: center;
justify-content: left;
}
a {