整理并提交 Rust 端剩余改动

This commit is contained in:
JSR
2026-03-20 18:43:07 +08:00
parent 61f3b24a1c
commit aed72b111b
6 changed files with 49 additions and 49 deletions
+8 -12
View File
@@ -25,8 +25,8 @@ pub async fn register_url_protocol(
{ {
use std::process::Command; use std::process::Command;
let exe_path = std::env::current_exe() let exe_path =
.map_err(|e| format!("Failed to get executable path: {}", e))?; std::env::current_exe().map_err(|e| format!("Failed to get executable path: {}", e))?;
let exe_path_str = exe_path.to_string_lossy(); let exe_path_str = exe_path.to_string_lossy();
@@ -45,17 +45,11 @@ pub async fn register_url_protocol(
protocol, exe_path_str protocol, exe_path_str
); );
let _ = Command::new("cmd") let _ = Command::new("cmd").args(["/C", &reg_command]).output();
.args(["/C", &reg_command])
.output();
let _ = Command::new("cmd") let _ = Command::new("cmd").args(["/C", &reg_command2]).output();
.args(["/C", &reg_command2])
.output();
let _ = Command::new("cmd") let _ = Command::new("cmd").args(["/C", &reg_command3]).output();
.args(["/C", &reg_command3])
.output();
let _ = app; let _ = app;
@@ -83,7 +77,9 @@ pub async fn register_url_protocol(
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{ {
let _ = app; let _ = app;
Ok(IpcResponse::error("URL protocol registration is not supported on this platform")) Ok(IpcResponse::error(
"URL protocol registration is not supported on this platform",
))
} }
} }
+19 -18
View File
@@ -5,8 +5,8 @@ use std::sync::Arc;
use tauri::{AppHandle, Emitter, Manager, State}; use tauri::{AppHandle, Emitter, Manager, State};
use tokio::time::{timeout, Duration}; use tokio::time::{timeout, Duration};
use crate::db::connection::{create_postgres_connection, create_sqlite_connection};
use crate::db::connection::DatabaseType; use crate::db::connection::DatabaseType;
use crate::db::connection::{create_postgres_connection, create_sqlite_connection};
use crate::db::entities::{ use crate::db::entities::{
reasons, reward_redemptions, reward_settings, score_events, student_tags, students, tags, reasons, reward_redemptions, reward_settings, score_events, student_tags, students, tags,
}; };
@@ -331,16 +331,17 @@ async fn load_student_tag_pairs(
.all(conn) .all(conn)
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let tag_rows = tags::Entity::find().all(conn).await.map_err(|e| e.to_string())?; let tag_rows = tags::Entity::find()
.all(conn)
.await
.map_err(|e| e.to_string())?;
let link_rows = student_tags::Entity::find() let link_rows = student_tags::Entity::find()
.all(conn) .all(conn)
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let student_name_map: std::collections::HashMap<i32, String> = student_rows let student_name_map: std::collections::HashMap<i32, String> =
.into_iter() student_rows.into_iter().map(|s| (s.id, s.name)).collect();
.map(|s| (s.id, s.name))
.collect();
let tag_name_map: std::collections::HashMap<i32, String> = let tag_name_map: std::collections::HashMap<i32, String> =
tag_rows.into_iter().map(|t| (t.id, t.name)).collect(); tag_rows.into_iter().map(|t| (t.id, t.name)).collect();
@@ -486,7 +487,10 @@ async fn upsert_reason(
} }
} }
async fn upsert_tag(conn: &sea_orm::DatabaseConnection, data: &TagNormalized) -> Result<bool, String> { async fn upsert_tag(
conn: &sea_orm::DatabaseConnection,
data: &TagNormalized,
) -> Result<bool, String> {
let existing = tags::Entity::find() let existing = tags::Entity::find()
.filter(tags::Column::Name.eq(&data.name)) .filter(tags::Column::Name.eq(&data.name))
.one(conn) .one(conn)
@@ -702,11 +706,9 @@ async fn ensure_student_tag_pair(
id: sea_orm::ActiveValue::NotSet, id: sea_orm::ActiveValue::NotSet,
student_id: Set(student_row.id), student_id: Set(student_row.id),
tag_id: Set(tag_row.id), tag_id: Set(tag_row.id),
created_at: Set( created_at: Set(chrono::Utc::now()
chrono::Utc::now() .format("%Y-%m-%dT%H:%M:%S%.3fZ")
.format("%Y-%m-%dT%H:%M:%S%.3fZ") .to_string()),
.to_string(),
),
} }
.insert(conn) .insert(conn)
.await .await
@@ -717,10 +719,7 @@ async fn ensure_student_tag_pair(
async fn current_remote_and_local_from_state( async fn current_remote_and_local_from_state(
app_handle: &AppHandle, app_handle: &AppHandle,
app_state: &Arc<RwLock<AppState>>, app_state: &Arc<RwLock<AppState>>,
) -> Result< ) -> Result<Option<(sea_orm::DatabaseConnection, sea_orm::DatabaseConnection)>, String> {
Option<(sea_orm::DatabaseConnection, sea_orm::DatabaseConnection)>,
String,
> {
let (current_conn, db_type) = { let (current_conn, db_type) = {
let state_guard = app_state.read(); let state_guard = app_state.read();
let db = state_guard.db.read().clone(); let db = state_guard.db.read().clone();
@@ -1053,7 +1052,8 @@ pub async fn db_switch_connection(
) -> Result<IpcResponse<SwitchConnectionResult>, String> { ) -> Result<IpcResponse<SwitchConnectionResult>, String> {
check_admin_permission(&state)?; check_admin_permission(&state)?;
let (db_type, saved_connection_string, saved_status, conn) = if connection_string.starts_with("postgres://") let (db_type, saved_connection_string, saved_status, conn) = if connection_string
.starts_with("postgres://")
|| connection_string.starts_with("postgresql://") || connection_string.starts_with("postgresql://")
{ {
let conn = timeout( let conn = timeout(
@@ -1390,7 +1390,8 @@ pub async fn db_sync(
let db_guard = state_guard.db.read(); let db_guard = state_guard.db.read();
if let Some(conn) = db_guard.as_ref() { if let Some(conn) = db_guard.as_ref() {
let settings = state_guard.settings.read(); let settings = state_guard.settings.read();
let status_json = settings.get_value(crate::services::settings::SettingsKey::PgConnectionStatus); let status_json =
settings.get_value(crate::services::settings::SettingsKey::PgConnectionStatus);
let db_type = match status_json { let db_type = match status_json {
crate::services::settings::SettingsValue::Json(json) => json crate::services::settings::SettingsValue::Json(json) => json
.get("type") .get("type")
+8 -8
View File
@@ -117,14 +117,14 @@ pub async fn reason_create(
updated_at: Set(now), updated_at: Set(now),
}; };
match new_reason.insert(conn).await { match new_reason.insert(conn).await {
Ok(inserted) => { Ok(inserted) => {
realtime_dual_write_sync(state.inner()).await?; realtime_dual_write_sync(state.inner()).await?;
Ok(IpcResponse::success(inserted.id)) Ok(IpcResponse::success(inserted.id))
} }
Err(e) => Ok(IpcResponse::error(&format!( Err(e) => Ok(IpcResponse::error(&format!(
"Failed to create reason: {}", "Failed to create reason: {}",
e e
))), ))),
} }
} else { } else {
+9 -9
View File
@@ -37,15 +37,15 @@ pub async fn window_maximize(
#[cfg(desktop)] #[cfg(desktop)]
{ {
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
let is_maximized = window.is_maximized().map_err(|e| e.to_string())?; let is_maximized = window.is_maximized().map_err(|e| e.to_string())?;
if is_maximized { if is_maximized {
window.unmaximize().map_err(|e| e.to_string())?; window.unmaximize().map_err(|e| e.to_string())?;
Ok(false) Ok(false)
} else { } else {
window.maximize().map_err(|e| e.to_string())?; window.maximize().map_err(|e| e.to_string())?;
Ok(true) Ok(true)
} }
} else { } else {
Err("Window not found".to_string()) Err("Window not found".to_string())
} }
@@ -83,7 +83,7 @@ pub async fn window_is_maximized(
#[cfg(desktop)] #[cfg(desktop)]
{ {
if let Some(window) = app.get_webview_window("main") { if let Some(window) = app.get_webview_window("main") {
window.is_maximized().map_err(|e| e.to_string()) window.is_maximized().map_err(|e| e.to_string())
} else { } else {
Ok(false) Ok(false)
} }
+1 -1
View File
@@ -1,7 +1,7 @@
use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr};
use std::time::Duration;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tracing::{info, warn}; use tracing::{info, warn};
+4 -1
View File
@@ -125,7 +125,10 @@ impl Migration {
let db_backend = Self::get_db_backend(sqlite); let db_backend = Self::get_db_backend(sqlite);
let alter_sql = "ALTER TABLE students ADD COLUMN reward_points INTEGER DEFAULT 0"; let alter_sql = "ALTER TABLE students ADD COLUMN reward_points INTEGER DEFAULT 0";
let result = conn let result = conn
.execute(Statement::from_string(db_backend.clone(), alter_sql.to_string())) .execute(Statement::from_string(
db_backend.clone(),
alter_sql.to_string(),
))
.await; .await;
match result { match result {