11use super :: models:: { FileGroupCondition , FileGroupDTO } ;
2- use crate :: model:: schema:: file_groups;
2+ use crate :: model:: schema:: { file_groups, files } ;
33use diesel:: prelude:: * ;
44use std:: fmt:: { Debug , Formatter , Result as fmtResult} ;
55use crate :: utils:: database:: AnyConnection ;
6- use crate :: model:: models:: { FileGroupOrderBy , FileGroupQueryOptions , OrderDirection } ;
6+ use crate :: model:: models:: { File , FileGroupOrderBy , FileGroupQueryOptions , OrderDirection } ;
7+ use crate :: utils:: errors:: AppError ;
8+ use crate :: utils:: errors:: AppError :: CannotUnbindPrimaryGroup ;
79
810pub fn insert_file_group (
911 conn : & mut AnyConnection ,
@@ -17,13 +19,25 @@ pub fn insert_file_group(
1719pub fn delete_file_group_by_id (
1820 conn : & mut AnyConnection ,
1921 file_group_dto : & FileGroupDTO ,
20- ) -> Result < usize , diesel:: result:: Error > {
22+ ) -> Result < usize , AppError > {
23+ // 判断是否是文件-主组关系 若是则抛异常
24+ let file = files:: table
25+ . select ( File :: as_select ( ) )
26+ . filter ( files:: id. eq ( file_group_dto. file_id ) )
27+ . first ( conn) ;
28+
29+ if file. is_ok ( ) && file. unwrap ( ) . group_id == file_group_dto. group_id {
30+ return Err ( CannotUnbindPrimaryGroup ) ;
31+ }
32+
2133 // 删除关联记录
2234 diesel:: delete (
2335 file_groups:: table
2436 . filter ( file_groups:: group_id. eq ( file_group_dto. group_id ) )
25- . filter ( file_groups:: file_id. eq ( file_group_dto. file_id ) )
26- ) . execute ( conn)
37+ . filter ( file_groups:: file_id. eq ( file_group_dto. file_id ) ) ,
38+ )
39+ . execute ( conn)
40+ . map_err ( AppError :: from) // 将 QueryResult 转换为 AppError
2741}
2842
2943impl Debug for FileGroupDTO {
@@ -94,7 +108,7 @@ pub fn select_file_groups_by_conditions(
94108 }
95109
96110 query
97- . select ( ( file_groups :: file_id , file_groups :: group_id ) )
111+ . select ( FileGroupDTO :: as_select ( ) )
98112 . load ( conn)
99113}
100114
@@ -140,7 +154,7 @@ pub fn select_file_groups_by_conditions_with_options(
140154 }
141155
142156 query
143- . select ( ( file_groups :: file_id , file_groups :: group_id ) )
157+ . select ( FileGroupDTO :: as_select ( ) )
144158 . load ( conn)
145159}
146160
0 commit comments