feat: complete application
This commit is contained in:
10 files changed
+236
-72
No files matched your search
@@ -1,10 +1,29 @@
|
||||
use tauri::{State};
|
||||
use rusqlite::{OptionalExtension};
|
||||
use crate::database::Database; // 自己定义的含互斥锁包装 SQLite 连接的结构
|
||||
|
||||
#[tauri::command]
|
||||
pub fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_message(level: &str) -> String {
|
||||
println!("level = {level}");
|
||||
"".to_string()
|
||||
pub async fn get_message(state: State<'_, Database>, level: Option<String>) -> Result<String, String> {
|
||||
let conn = state.connection().lock().map_err(|e| e.to_string())?;
|
||||
|
||||
let mut stmt = if let Some(ref _level) = level {
|
||||
conn.prepare("SELECT `text` FROM `main` WHERE LEVEL = ?1 ORDER BY RANDOM() LIMIT 1")
|
||||
.map_err(|e| e.to_string())?
|
||||
} else {
|
||||
conn.prepare("SELECT `text` FROM `main` ORDER BY RANDOM() LIMIT 1")
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
|
||||
let message_opt: Option<String> = if let Some(_level) = &level {
|
||||
stmt.query_row([_level], |row| row.get(0)).optional().map_err(|e| e.to_string())?
|
||||
} else {
|
||||
stmt.query_row([], |row| row.get(0)).optional().map_err(|e| e.to_string())?
|
||||
};
|
||||
|
||||
Ok(message_opt.unwrap_or_default())
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub const APP_NAME: &str = "ZuAnBot";
|
||||
@@ -0,0 +1,37 @@
|
||||
use rusqlite::Connection;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use anyhow::Result;
|
||||
use tauri::Manager;
|
||||
|
||||
pub struct Database {
|
||||
conn: Mutex<Connection>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// 初始化数据库连接
|
||||
pub fn new(app_handle: &tauri::AppHandle) -> Result<Self> {
|
||||
let db_path = get_bundled_database_path(app_handle)?;
|
||||
let conn = Connection::open_with_flags(
|
||||
db_path,
|
||||
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
|
||||
)?;
|
||||
|
||||
Ok(Database { conn: Mutex::from(conn) })
|
||||
}
|
||||
|
||||
/// 获取数据库连接的引用
|
||||
pub fn connection(&self) -> &Mutex<Connection> {
|
||||
&self.conn
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取打包的数据库文件路径
|
||||
fn get_bundled_database_path(app_handle: &tauri::AppHandle) -> Result<PathBuf> {
|
||||
let resource_path = app_handle
|
||||
.path()
|
||||
.resolve("data.db", tauri::path::BaseDirectory::Resource)?;
|
||||
|
||||
println!("Database path: {:?}", resource_path);
|
||||
Ok(resource_path)
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Get system-wide data dir.
|
||||
pub fn get_global_data_dir() -> Result<PathBuf> {
|
||||
#[cfg(target_os = "macos")]
|
||||
let global_data_dir = PathBuf::from("/Library/Application Support/");
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
let global_data_dir = PathBuf::from("C:\\ProgramData\\");
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
let global_data_dir = PathBuf::from("/var/lib/");
|
||||
|
||||
Ok(global_data_dir)
|
||||
}
|
||||
+20
-4
@@ -1,14 +1,30 @@
|
||||
use crate::commands::{greet, get_message};
|
||||
use tauri::Manager;
|
||||
use crate::commands::{get_message};
|
||||
use crate::database::Database;
|
||||
|
||||
mod commands;
|
||||
mod dirs;
|
||||
mod constants;
|
||||
mod database;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet, get_message])
|
||||
.setup(|app| {
|
||||
// 初始化数据库
|
||||
match Database::new(app.handle()) {
|
||||
Ok(db) => {
|
||||
println!("Database initialized successfully");
|
||||
// 可以将数据库实例存储到应用状态中,供后续使用
|
||||
app.manage(db);
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Failed to initialize database: {}", e);
|
||||
return Err(Box::from(e));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![get_message])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Reference in new issue
Block a user