Skip to content

Commit f3c6235

Browse files
committed
perf: provide a unified save button for the path settings
1 parent 18bf76a commit f3c6235

12 files changed

Lines changed: 2199 additions & 2243 deletions

File tree

src-tauri/src/database/dto.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,36 @@ impl UpdateCollectionData {
110110
// ==================== 设置相关 DTO ====================
111111

112112
/// 用于更新设置的数据结构
113-
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
114-
#[serde(default)]
113+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
114+
#[serde(default, rename_all = "camelCase")]
115115
pub struct UpdateSettingsData {
116-
pub bgm_token: Option<String>,
117-
pub vndb_token: Option<String>,
118-
pub save_root_path: Option<String>,
119-
pub db_backup_path: Option<String>,
120-
pub le_path: Option<String>,
121-
pub magpie_path: Option<String>,
116+
#[serde(default, deserialize_with = "double_option")]
117+
pub bgm_token: Option<Option<String>>,
118+
#[serde(default, deserialize_with = "double_option")]
119+
pub vndb_token: Option<Option<String>>,
120+
#[serde(default, deserialize_with = "double_option")]
121+
pub save_root_path: Option<Option<String>>,
122+
#[serde(default, deserialize_with = "double_option")]
123+
pub db_backup_path: Option<Option<String>>,
124+
#[serde(default, deserialize_with = "double_option")]
125+
pub le_path: Option<Option<String>>,
126+
#[serde(default, deserialize_with = "double_option")]
127+
pub magpie_path: Option<Option<String>>,
122128
}
123129

124130
/// 清洗 UpdateSettingsData 中的空字符串
125-
impl UpdateSettingsData {
126-
/// 返回清洗后的数据,将空字符串转换为 None
131+
impl UpdateSettingsData {
132+
/// 返回清洗后的数据,将空字符串转换为 None
127133
pub fn cleaned(mut self) -> Self {
128-
self.bgm_token = self.bgm_token.filter(|s| !s.trim().is_empty());
129-
self.vndb_token = self.vndb_token.filter(|s| !s.trim().is_empty());
130-
self.save_root_path = self.save_root_path.filter(|s| !s.trim().is_empty());
131-
self.db_backup_path = self.db_backup_path.filter(|s| !s.trim().is_empty());
132-
self.le_path = self.le_path.filter(|s| !s.trim().is_empty());
133-
self.magpie_path = self.magpie_path.filter(|s| !s.trim().is_empty());
134+
self.bgm_token = clean_double_option_string(self.bgm_token);
135+
self.vndb_token = clean_double_option_string(self.vndb_token);
136+
self.save_root_path = clean_double_option_string(self.save_root_path);
137+
self.db_backup_path = clean_double_option_string(self.db_backup_path);
138+
self.le_path = clean_double_option_string(self.le_path);
139+
self.magpie_path = clean_double_option_string(self.magpie_path);
134140
self
135-
}
136-
}
141+
}
142+
}
137143

138144
/// 用于插入游戏的数据结构(单表架构)
139145
///

src-tauri/src/database/repository/settings_repository.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,27 @@ impl SettingsRepository {
228228
let mut active: user::ActiveModel = user.into();
229229

230230
if let Some(token) = data.bgm_token {
231-
active.bgm_token = Set(Some(token));
231+
active.bgm_token = Set(token);
232232
}
233233

234234
if let Some(token) = data.vndb_token {
235-
active.vndb_token = Set(Some(token));
235+
active.vndb_token = Set(token);
236236
}
237-
238-
if let Some(path) = data.save_root_path {
239-
active.save_root_path = Set(Some(path));
240-
}
241-
242-
if let Some(path) = data.db_backup_path {
243-
active.db_backup_path = Set(Some(path));
244-
}
245-
246-
if let Some(path) = data.le_path {
247-
active.le_path = Set(Some(path));
248-
}
249-
237+
238+
if let Some(path) = data.save_root_path {
239+
active.save_root_path = Set(path);
240+
}
241+
242+
if let Some(path) = data.db_backup_path {
243+
active.db_backup_path = Set(path);
244+
}
245+
246+
if let Some(path) = data.le_path {
247+
active.le_path = Set(path);
248+
}
249+
250250
if let Some(path) = data.magpie_path {
251-
active.magpie_path = Set(Some(path));
251+
active.magpie_path = Set(path);
252252
}
253253

254254
active.update(db).await?;

src-tauri/src/database/service.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -508,26 +508,16 @@ pub async fn get_all_settings(db: State<'_, DatabaseConnection>) -> Result<user:
508508
.map_err(|e| format!("获取所有设置失败: {}", e))
509509
}
510510

511-
/// 批量更新设置 暂时无用
511+
/// 批量更新设置
512512
#[tauri::command]
513513
pub async fn update_settings(
514514
app: AppHandle,
515515
db: State<'_, DatabaseConnection>,
516-
bgm_token: Option<String>,
517-
vndb_token: Option<String>,
518-
save_root_path: Option<String>,
519-
db_backup_path: Option<String>,
516+
data: UpdateSettingsData,
520517
) -> Result<(), String> {
521518
use crate::utils::fs::PathManager;
522519

523-
let data = UpdateSettingsData {
524-
bgm_token,
525-
vndb_token,
526-
save_root_path,
527-
db_backup_path,
528-
..Default::default()
529-
}
530-
.cleaned(); // 清洗空字符串
520+
let data = data.cleaned(); // 清洗空字符串
531521

532522
SettingsRepository::update_settings(&db, data)
533523
.await

0 commit comments

Comments
 (0)