11use crate :: database:: dto:: UpdateSettingsData ;
22use crate :: entity:: prelude:: * ;
33use crate :: entity:: user;
4+ use crate :: entity:: user:: Model ;
45use sea_orm:: * ;
56
67/// 用户设置仓库
78pub struct SettingsRepository ;
89
10+ pub trait DbSettingsExt {
11+ /// 获取设置模型,并自动处理好错误转换
12+ async fn get_settings ( & self ) -> Result < Model , String > ;
13+ }
14+
15+ impl DbSettingsExt for DatabaseConnection {
16+ async fn get_settings ( & self ) -> Result < Model , String > {
17+ SettingsRepository :: get_all_settings ( self )
18+ . await
19+ . map_err ( |e| format ! ( "获取设置失败: {}" , e) )
20+ }
21+ }
22+
923impl SettingsRepository {
1024 /// 确保用户记录存在(ID 固定为 1)
1125 async fn ensure_user_exists ( db : & DatabaseConnection ) -> Result < ( ) , DbErr > {
1226 let existing = User :: find_by_id ( 1 ) . one ( db) . await ?;
1327
1428 if existing. is_none ( ) {
15- let user = user:: ActiveModel {
16- id : Set ( 1 ) ,
17- bgm_token : Set ( None ) ,
18- vndb_token : Set ( None ) ,
19- save_root_path : Set ( None ) ,
20- db_backup_path : Set ( None ) ,
21- le_path : Set ( None ) ,
22- magpie_path : Set ( None ) ,
23- } ;
29+ let user = user:: ActiveModel {
30+ id : Set ( 1 ) ,
31+ bgm_token : Set ( None ) ,
32+ vndb_token : Set ( None ) ,
33+ save_root_path : Set ( None ) ,
34+ db_backup_path : Set ( None ) ,
35+ le_path : Set ( None ) ,
36+ magpie_path : Set ( None ) ,
37+ } ;
2438
2539 user. insert ( db) . await ?;
2640 }
2741
2842 Ok ( ( ) )
2943 }
3044
31- /// 获取 BGM Token
32- pub async fn get_bgm_token ( db : & DatabaseConnection ) -> Result < String , DbErr > {
33- Self :: ensure_user_exists ( db) . await ?;
34-
35- let user = User :: find_by_id ( 1 )
36- . one ( db)
37- . await ?
38- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
39-
40- Ok ( user. bgm_token . unwrap_or_default ( ) )
41- }
42-
43- /// 设置 BGM Token
44- pub async fn set_bgm_token ( db : & DatabaseConnection , token : String ) -> Result < ( ) , DbErr > {
45- Self :: ensure_user_exists ( db) . await ?;
46-
47- let user = User :: find_by_id ( 1 )
48- . one ( db)
49- . await ?
50- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
51-
52- let mut active: user:: ActiveModel = user. into ( ) ;
53- // 清洗空字符串为 NULL
54- active. bgm_token = Set ( Some ( token) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
55-
56- active. update ( db) . await ?;
57- Ok ( ( ) )
58- }
59-
60- /// 获取 VNDB Token
61- pub async fn get_vndb_token ( db : & DatabaseConnection ) -> Result < String , DbErr > {
62- Self :: ensure_user_exists ( db) . await ?;
63-
64- let user = User :: find_by_id ( 1 )
65- . one ( db)
66- . await ?
67- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
68-
69- Ok ( user. vndb_token . unwrap_or_default ( ) )
70- }
71-
72- /// 设置 VNDB Token
73- pub async fn set_vndb_token ( db : & DatabaseConnection , token : String ) -> Result < ( ) , DbErr > {
74- Self :: ensure_user_exists ( db) . await ?;
75-
76- let user = User :: find_by_id ( 1 )
77- . one ( db)
78- . await ?
79- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
80-
81- let mut active: user:: ActiveModel = user. into ( ) ;
82- active. vndb_token = Set ( Some ( token) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
83-
84- active. update ( db) . await ?;
85- Ok ( ( ) )
86- }
87-
88- /// 获取存档根路径
89- pub async fn get_save_root_path ( db : & DatabaseConnection ) -> Result < String , DbErr > {
90- Self :: ensure_user_exists ( db) . await ?;
91-
92- let user = User :: find_by_id ( 1 )
93- . one ( db)
94- . await ?
95- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
96-
97- Ok ( user. save_root_path . unwrap_or_default ( ) )
98- }
99-
100- /// 设置存档根路径
101- pub async fn set_save_root_path ( db : & DatabaseConnection , path : String ) -> Result < ( ) , DbErr > {
102- Self :: ensure_user_exists ( db) . await ?;
103-
104- let user = User :: find_by_id ( 1 )
105- . one ( db)
106- . await ?
107- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
108-
109- let mut active: user:: ActiveModel = user. into ( ) ;
110- // 清洗空字符串为 NULL
111- active. save_root_path = Set ( Some ( path) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
112-
113- active. update ( db) . await ?;
114- Ok ( ( ) )
115- }
116-
117- /// 获取数据库备份保存路径
118- pub async fn get_db_backup_path ( db : & DatabaseConnection ) -> Result < String , DbErr > {
119- Self :: ensure_user_exists ( db) . await ?;
120-
121- let user = User :: find_by_id ( 1 )
122- . one ( db)
123- . await ?
124- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
125-
126- Ok ( user. db_backup_path . unwrap_or_default ( ) )
127- }
128-
129- /// 设置数据库备份保存路径
130- pub async fn set_db_backup_path ( db : & DatabaseConnection , path : String ) -> Result < ( ) , DbErr > {
131- Self :: ensure_user_exists ( db) . await ?;
132-
133- let user = User :: find_by_id ( 1 )
134- . one ( db)
135- . await ?
136- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
137-
138- let mut active: user:: ActiveModel = user. into ( ) ;
139- // 清洗空字符串为 NULL
140- active. db_backup_path = Set ( Some ( path) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
141-
142- active. update ( db) . await ?;
143- Ok ( ( ) )
144- }
145-
146- /// 获取LE转区软件路径
147- pub async fn get_le_path ( db : & DatabaseConnection ) -> Result < String , DbErr > {
148- Self :: ensure_user_exists ( db) . await ?;
149-
150- let user = User :: find_by_id ( 1 )
151- . one ( db)
152- . await ?
153- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
154-
155- Ok ( user. le_path . unwrap_or_default ( ) )
156- }
157-
158- /// 设置LE转区软件路径
159- pub async fn set_le_path ( db : & DatabaseConnection , path : String ) -> Result < ( ) , DbErr > {
160- Self :: ensure_user_exists ( db) . await ?;
161-
162- let user = User :: find_by_id ( 1 )
163- . one ( db)
164- . await ?
165- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
166-
167- let mut active: user:: ActiveModel = user. into ( ) ;
168- // 清洗空字符串为 NULL
169- active. le_path = Set ( Some ( path) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
170-
171- active. update ( db) . await ?;
172- Ok ( ( ) )
173- }
174-
175- /// 获取Magpie转区软件路径
176- pub async fn get_magpie_path ( db : & DatabaseConnection ) -> Result < String , DbErr > {
177- Self :: ensure_user_exists ( db) . await ?;
178-
179- let user = User :: find_by_id ( 1 )
180- . one ( db)
181- . await ?
182- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
183-
184- Ok ( user. magpie_path . unwrap_or_default ( ) )
185- }
186-
187- /// 设置Magpie转区软件路径
188- pub async fn set_magpie_path ( db : & DatabaseConnection , path : String ) -> Result < ( ) , DbErr > {
189- Self :: ensure_user_exists ( db) . await ?;
190-
191- let user = User :: find_by_id ( 1 )
192- . one ( db)
193- . await ?
194- . ok_or ( DbErr :: RecordNotFound ( "User record not found" . to_string ( ) ) ) ?;
195-
196- let mut active: user:: ActiveModel = user. into ( ) ;
197- // 清洗空字符串为 NULL
198- active. magpie_path = Set ( Some ( path) . filter ( |s| !s. trim ( ) . is_empty ( ) ) ) ;
199-
200- active. update ( db) . await ?;
201- Ok ( ( ) )
202- }
203-
20445 /// 获取所有设置
20546 pub async fn get_all_settings ( db : & DatabaseConnection ) -> Result < user:: Model , DbErr > {
20647 Self :: ensure_user_exists ( db) . await ?;
@@ -227,31 +68,31 @@ impl SettingsRepository {
22768
22869 let mut active: user:: ActiveModel = user. into ( ) ;
22970
230- if let Some ( token) = data. bgm_token {
231- active. bgm_token = Set ( token) ;
232- }
233-
234- if let Some ( token) = data. vndb_token {
235- active. vndb_token = Set ( token) ;
236- }
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-
250- if let Some ( path) = data. magpie_path {
251- active. magpie_path = Set ( path) ;
252- }
253-
254- active. update ( db) . await ?;
255- Ok ( ( ) )
256- }
257- }
71+ if let Some ( token) = data. bgm_token {
72+ active. bgm_token = Set ( token) ;
73+ }
74+
75+ if let Some ( token) = data. vndb_token {
76+ active. vndb_token = Set ( token) ;
77+ }
78+
79+ if let Some ( path) = data. save_root_path {
80+ active. save_root_path = Set ( path) ;
81+ }
82+
83+ if let Some ( path) = data. db_backup_path {
84+ active. db_backup_path = Set ( path) ;
85+ }
86+
87+ if let Some ( path) = data. le_path {
88+ active. le_path = Set ( path) ;
89+ }
90+
91+ if let Some ( path) = data. magpie_path {
92+ active. magpie_path = Set ( path) ;
93+ }
94+
95+ active. update ( db) . await ?;
96+ Ok ( ( ) )
97+ }
98+ }
0 commit comments