Skip to content

Commit 25fb8c1

Browse files
committed
snapshot
1 parent 440297a commit 25fb8c1

6 files changed

Lines changed: 155 additions & 15 deletions

File tree

file_classification_core/src/service/file_group.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ pub fn delete_file_groups_by_conditions(
7777
conn: &mut SqliteConnection,
7878
condition: Vec<FileGroupCondition>,
7979
) -> Result<usize, Error> {
80-
// TODO: 减少文件和组的引用计数
81-
crate::internal::file_group::delete_file_groups_by_conditions(conn, condition)
80+
// 首先查询将要删除的记录
81+
let file_groups_to_delete = select_file_groups_by_conditions(conn, condition.clone(), None)
82+
.map_err(|e| match e {
83+
diesel::result::Error::NotFound => Error::NotFound,
84+
_ => e,
85+
})?;
86+
87+
// 使用事务确保数据一致性
88+
conn.transaction::<_, Error, _>(|conn| {
89+
// 对于每个要删除的文件组关联,减少对应的文件和组的引用计数
90+
for file_group in &file_groups_to_delete {
91+
// 减少文件的引用计数
92+
decrease_file_reference_count(conn, file_group.file_id)?;
93+
94+
// 减少组的引用计数
95+
decrease_group_reference_count(conn, file_group.group_id)?;
96+
}
97+
98+
// 执行实际的删除操作
99+
let deleted_count = crate::internal::file_group::delete_file_groups_by_conditions(conn, condition)?;
100+
101+
Ok(deleted_count)
102+
})
82103
}

file_classification_core/src/service/files.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,43 @@ pub fn delete_files_by_conditions(
155155
conn: &mut SqliteConnection,
156156
conditions: Vec<FileCondition>,
157157
) -> Result<usize, diesel::result::Error> {
158-
// TODO: 减少文件的引用计数
159-
internal::files::delete_files_by_conditions(conn, conditions)
158+
// 首先查询将要删除的文件
159+
let files_to_delete = select_files_by_conditions(conn, conditions.clone(), None)
160+
.map_err(|e| match e {
161+
diesel::result::Error::NotFound => diesel::result::Error::NotFound,
162+
_ => e,
163+
})?;
164+
165+
// 使用事务确保数据一致性
166+
conn.transaction::<_, diesel::result::Error, _>(|conn| {
167+
let mut total_deleted = 0;
168+
169+
// 对于每个要删除的文件,处理相关的引用关系
170+
for file in &files_to_delete {
171+
// 查找与该文件关联的所有文件组关系
172+
let file_groups = select_file_groups_by_conditions(
173+
conn,
174+
vec![FileGroupCondition::FileId(file.id)],
175+
None,
176+
)?;
177+
178+
// 对于每个文件组关系,减少对应组的引用计数
179+
for file_group in &file_groups {
180+
internal::groups::decrease_group_reference_count(conn, file_group.group_id)?;
181+
}
182+
183+
// 删除与该文件关联的所有文件组关系
184+
internal::file_group::delete_file_groups_by_conditions(
185+
conn,
186+
vec![FileGroupCondition::FileId(file.id)],
187+
)?;
188+
189+
// 删除文件本身
190+
let deleted_count = internal::files::delete_file_by_id(conn, file.id)?;
191+
total_deleted += deleted_count;
192+
}
193+
194+
Ok(total_deleted)
195+
})
160196
}
161197

file_classification_core/src/service/group_tag.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ pub fn delete_group_tags_by_conditions(
6969
conn: &mut SqliteConnection,
7070
condition: Vec<GroupTagCondition>,
7171
) -> Result<usize, Error> {
72-
// TODO: 减少组和标签的引用计数
73-
crate::internal::group_tag::delete_group_tags_by_conditions(conn, condition)
72+
// 首先查询将要删除的组标签关联
73+
let group_tags_to_delete = select_group_tags_by_conditions(conn, condition.clone(), None)
74+
.map_err(|e| match e {
75+
diesel::result::Error::NotFound => diesel::result::Error::NotFound,
76+
_ => e,
77+
})?;
78+
79+
// 使用事务确保数据一致性
80+
conn.transaction::<_, Error, _>(|conn| {
81+
// 对于每个要删除的组标签关联,减少对应的组和标签的引用计数
82+
for group_tag in &group_tags_to_delete {
83+
// 减少组的引用计数
84+
decrease_group_reference_count(conn, group_tag.group_id)?;
85+
86+
// 减少标签的引用计数
87+
decrease_tag_reference_count(conn, group_tag.tag_id)?;
88+
}
89+
90+
// 执行实际的删除操作
91+
let deleted_count = crate::internal::group_tag::delete_group_tags_by_conditions(conn, condition)?;
92+
93+
Ok(deleted_count)
94+
})
7495
}

file_classification_core/src/service/groups.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,45 @@ pub fn delete_groups_by_conditions(
7575
conn: &mut SqliteConnection,
7676
conditions: Vec<GroupCondition>,
7777
) -> Result<usize, Error> {
78-
// TODO: 减少组的引用计数
79-
groups::delete_groups_by_conditions(conn, conditions)
78+
// 首先查询将要删除的组
79+
let groups_to_delete = select_groups_by_conditions(conn, conditions.clone(), None)
80+
.map_err(|e| match e {
81+
diesel::result::Error::NotFound => diesel::result::Error::NotFound,
82+
_ => e,
83+
})?;
84+
85+
// 使用事务确保数据一致性
86+
conn.transaction::<_, Error, _>(|conn| {
87+
let mut total_deleted = 0;
88+
89+
// 对于每个要删除的组,处理相关的引用关系和关联数据
90+
for group in &groups_to_delete {
91+
// 如果是主组,则删除相关的文件
92+
if group.is_primary {
93+
crate::internal::files::delete_files_by_conditions(
94+
conn,
95+
vec![FileCondition::GroupId(group.id)]
96+
)?;
97+
} else {
98+
// 如果不是主组,则删除文件组关联
99+
crate::internal::file_group::delete_file_groups_by_conditions(
100+
conn,
101+
vec![FileGroupCondition::GroupId(group.id)]
102+
)?;
103+
}
104+
105+
// 删除组标签关联
106+
crate::internal::group_tag::delete_group_tags_by_conditions(
107+
conn,
108+
vec![GroupTagCondition::GroupId(group.id)]
109+
)?;
110+
111+
// 删除组本身
112+
let deleted_count = groups::delete_group(conn, group.id)?;
113+
total_deleted += deleted_count;
114+
}
115+
116+
Ok(total_deleted)
117+
})
80118
}
81119

file_classification_core/src/service/tags.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::model::models::{TagCondition, UpdateTagDTO};
1+
use crate::model::models::{GroupTagCondition, TagCondition, UpdateTagDTO};
22
use crate::{
33
internal::tags,
44
model::models::{CreateTagDTO, Tag, TagFilter},
55
};
6-
use diesel::SqliteConnection;
6+
use diesel::{Connection, SqliteConnection};
77

88
pub fn create_tag(conn: &mut SqliteConnection, name: &str) -> Result<Tag, diesel::result::Error> {
99
let new_tag = CreateTagDTO { name };
@@ -46,6 +46,30 @@ pub fn delete_tags_by_conditions(
4646
conn: &mut SqliteConnection,
4747
conditions: Vec<TagCondition>,
4848
) -> Result<usize, diesel::result::Error> {
49-
// TODO: 减少标签的引用计数
50-
tags::delete_tags_by_conditions(conn, conditions)
51-
}
49+
// 首先查询将要删除的标签
50+
let tags_to_delete = select_tags_by_conditions(conn, conditions.clone(), None)
51+
.map_err(|e| match e {
52+
diesel::result::Error::NotFound => diesel::result::Error::NotFound,
53+
_ => e,
54+
})?;
55+
56+
// 使用事务确保数据一致性
57+
conn.transaction::<_, diesel::result::Error, _>(|conn| {
58+
let mut total_deleted = 0;
59+
60+
// 对于每个要删除的标签,处理相关的引用关系和关联数据
61+
for tag in &tags_to_delete {
62+
// 删除与该标签关联的所有组标签关系
63+
crate::internal::group_tag::delete_group_tags_by_conditions(
64+
conn,
65+
vec![GroupTagCondition::TagId(tag.id)]
66+
)?;
67+
68+
// 删除标签本身
69+
let deleted_count = tags::delete_tag(conn, tag.id)?;
70+
total_deleted += deleted_count;
71+
}
72+
73+
Ok(total_deleted)
74+
})
75+
}

file_classification_webapi/src/bin/utils/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub type PooledConnection = r2d2::PooledConnection<ConnectionManager<SqliteConne
1818
//
1919
// Arc::new(pool)
2020
// });
21-
22-
// 获取连接的辅助函数
21+
//
22+
// // 获取连接的辅助函数
2323
// pub fn get_connection() -> Result<PooledConnection, r2d2::Error> {
2424
// DB_POOL.get()
2525
// }

0 commit comments

Comments
 (0)