Skip to content

Commit b7c84ec

Browse files
committed
feat(webapi): add more apis
1 parent 567caeb commit b7c84ec

14 files changed

Lines changed: 1166 additions & 163 deletions

File tree

file_classification_cli/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ fn handle_file_group_action(
765765
file_id, group_id
766766
)) {
767767
let dto = models::FileGroupDTO { file_id, group_id };
768-
match service::file_group::delete_file_group(conn, dto) {
768+
match service::file_group::delete_file_group_by_dto(conn, dto) {
769769
Ok(count) => println!("成功删除 {} 个文件组关联", count),
770770
Err(e) => eprintln!("删除文件组关联失败: {:?}", e),
771771
}
@@ -846,7 +846,7 @@ fn handle_group_tag_action(
846846
group_id, tag_id
847847
)) {
848848
let dto = models::GroupTagDTO { group_id, tag_id };
849-
match service::group_tag::delete_group_tag_by_id(conn, dto) {
849+
match service::group_tag::delete_group_tag_by_dto(conn, dto) {
850850
Ok(count) => println!("成功删除 {} 个组标签关联", count),
851851
Err(e) => eprintln!("删除组标签关联失败: {:?}", e),
852852
}

file_classification_core/src/model/models.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Debug for File {
4747
/// 创建文件的DTO对象
4848
///
4949
/// 用于向数据库插入新文件记录时的数据传输对象
50-
#[derive(Insertable)]
50+
#[derive(Insertable, Deserialize)]
5151
#[diesel(table_name = files)]
5252
pub struct CreateFileDTO {
5353
/// 文件类型/扩展名
@@ -117,7 +117,7 @@ pub enum FileCondition {
117117
/// 文件查询选项结构体
118118
///
119119
/// 定义了文件查询的可选参数,如分页、排序等
120-
#[derive(Default)]
120+
#[derive(Default, Deserialize)]
121121
pub struct FileQueryOptions {
122122
/// 查询结果限制数量
123123
pub limit: Option<i64>,
@@ -130,6 +130,7 @@ pub struct FileQueryOptions {
130130
/// 文件排序字段枚举
131131
///
132132
/// 定义了可以用于文件查询结果排序的字段
133+
#[derive(Deserialize)]
133134
pub enum FileOrderBy {
134135
/// 按文件ID排序
135136
Id(OrderDirection),
@@ -335,7 +336,7 @@ pub enum GroupCondition {
335336
/// 分组查询选项结构体
336337
///
337338
/// 定义了分组查询的可选参数,如分页、排序等
338-
#[derive(Default)]
339+
#[derive(Default, Deserialize)]
339340
pub struct GroupQueryOptions {
340341
/// 查询结果限制数量
341342
pub limit: Option<i64>,
@@ -348,6 +349,7 @@ pub struct GroupQueryOptions {
348349
/// 分组排序字段枚举
349350
///
350351
/// 定义了可以用于分组查询结果排序的字段
352+
#[derive(Deserialize)]
351353
pub enum GroupOrderBy {
352354
/// 按分组ID排序
353355
Id(OrderDirection),
@@ -370,6 +372,7 @@ pub enum GroupOrderBy {
370372
/// 排序方向枚举
371373
///
372374
/// 定义了排序的方向,升序或降序
375+
#[derive(Deserialize)]
373376
pub enum OrderDirection {
374377
/// 升序排列
375378
Asc,
@@ -538,7 +541,7 @@ pub enum TagCondition {
538541
/// 标签查询选项结构体
539542
///
540543
/// 定义了标签查询的可选参数,如分页、排序等
541-
#[derive(Default)]
544+
#[derive(Default, Deserialize)]
542545
pub struct TagQueryOptions {
543546
/// 查询结果限制数量
544547
pub limit: Option<i64>,
@@ -551,6 +554,7 @@ pub struct TagQueryOptions {
551554
/// 标签排序字段枚举
552555
///
553556
/// 定义了可以用于标签查询结果排序的字段
557+
#[derive(Deserialize)]
554558
pub enum TagOrderBy {
555559
/// 按标签ID排序
556560
Id(OrderDirection),
@@ -666,7 +670,7 @@ pub enum FileGroupCondition {
666670
/// 文件-分组关联查询选项结构体
667671
///
668672
/// 定义了文件-分组关联查询的可选参数,如分页、排序等
669-
#[derive(Default)]
673+
#[derive(Default, Deserialize)]
670674
pub struct FileGroupQueryOptions {
671675
/// 查询结果限制数量
672676
pub limit: Option<i64>,
@@ -679,6 +683,7 @@ pub struct FileGroupQueryOptions {
679683
/// 文件-分组关联排序字段枚举
680684
///
681685
/// 定义了可以用于文件-分组关联查询结果排序的字段
686+
#[derive(Deserialize)]
682687
pub enum FileGroupOrderBy {
683688
/// 按文件ID排序
684689
FileId(OrderDirection),
@@ -747,7 +752,7 @@ pub enum GroupTagCondition {
747752
/// 分组-标签关联查询选项结构体
748753
///
749754
/// 定义了分组-标签关联查询的可选参数,如分页、排序等
750-
#[derive(Default)]
755+
#[derive(Default, Deserialize)]
751756
pub struct GroupTagQueryOptions {
752757
/// 查询结果限制数量
753758
pub limit: Option<i64>,
@@ -760,6 +765,7 @@ pub struct GroupTagQueryOptions {
760765
/// 分组-标签关联排序字段枚举
761766
///
762767
/// 定义了可以用于分组-标签关联查询结果排序的字段
768+
#[derive(Deserialize)]
763769
pub enum GroupTagOrderBy {
764770
/// 按分组ID排序
765771
GroupId(OrderDirection),
Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
// @generated automatically by Diesel CLI.
22

33
diesel::table! {
4-
file_groups (file_id, group_id) {
5-
file_id -> Integer,
6-
group_id -> Integer,
7-
}
4+
file_groups (file_id, group_id) {
5+
file_id -> Integer,
6+
group_id -> Integer,
7+
}
88
}
99

1010
diesel::table! {
11-
files (id) {
12-
id -> Integer,
13-
#[sql_name = "type"]
14-
type_ -> Text,
15-
path -> Text,
16-
reference_count -> Integer,
17-
group_id -> Integer,
18-
}
11+
files (id) {
12+
id -> Integer,
13+
#[sql_name = "type"]
14+
type_ -> Text,
15+
path -> Text,
16+
reference_count -> Integer,
17+
group_id -> Integer,
18+
}
1919
}
2020

2121
diesel::table! {
22-
group_tags (group_id, tag_id) {
23-
group_id -> Integer,
24-
tag_id -> Integer,
25-
}
22+
group_tags (group_id, tag_id) {
23+
group_id -> Integer,
24+
tag_id -> Integer,
25+
}
2626
}
2727

2828
diesel::table! {
29-
groups (id) {
30-
id -> Integer,
31-
name -> Text,
32-
reference_count -> Integer,
33-
is_primary -> Bool,
34-
click_count -> Integer,
35-
share_count -> Integer,
36-
create_time -> Timestamp,
37-
modify_time -> Timestamp,
38-
}
29+
groups (id) {
30+
id -> Integer,
31+
name -> Text,
32+
reference_count -> Integer,
33+
is_primary -> Bool,
34+
click_count -> Integer,
35+
share_count -> Integer,
36+
create_time -> Timestamp,
37+
modify_time -> Timestamp,
38+
}
3939
}
4040

4141
diesel::table! {
42-
tags (id) {
43-
id -> Integer,
44-
reference_count -> Integer,
45-
name -> Text,
46-
}
42+
tags (id) {
43+
id -> Integer,
44+
reference_count -> Integer,
45+
name -> Text,
46+
}
4747
}
4848

4949
diesel::joinable!(file_groups -> files (file_id));
@@ -52,10 +52,4 @@ diesel::joinable!(files -> groups (group_id));
5252
diesel::joinable!(group_tags -> groups (group_id));
5353
diesel::joinable!(group_tags -> tags (tag_id));
5454

55-
diesel::allow_tables_to_appear_in_same_query!(
56-
file_groups,
57-
files,
58-
group_tags,
59-
groups,
60-
tags,
61-
);
55+
diesel::allow_tables_to_appear_in_same_query!(file_groups, files, group_tags, groups, tags,);

file_classification_core/src/service/file_group.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn create_file_group(
7878
/// - 减少分组的引用计数
7979
/// - 减少文件的引用计数
8080
/// - 删除文件-分组关联记录
81-
pub fn delete_file_group(
81+
pub fn delete_file_group_by_dto(
8282
conn: &mut AnyConnection,
8383
file_group_dto: FileGroupDTO,
8484
) -> Result<usize, AppError> {
@@ -178,7 +178,7 @@ pub fn delete_file_groups_by_conditions(
178178
};
179179

180180
// 调用单个删除函数,复用其业务逻辑和验证规则
181-
let deleted_count = delete_file_group(conn, file_group_dto)?;
181+
let deleted_count = delete_file_group_by_dto(conn, file_group_dto)?;
182182
total_deleted += deleted_count;
183183
}
184184

file_classification_core/src/service/files.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn delete_file(conn: &mut AnyConnection, file_id: i32) -> Result<(), AppErro
140140
Ok(())
141141
}
142142

143-
/// [已弃用] 根据过滤条件查询文件列表
143+
/// 根据过滤条件查询文件列表
144144
///
145145
/// 参数:
146146
/// - `conn`: 数据库连接对象
@@ -149,9 +149,7 @@ pub fn delete_file(conn: &mut AnyConnection, file_id: i32) -> Result<(), AppErro
149149
///
150150
/// 返回值:
151151
/// 查询成功的文件记录列表或数据库错误
152-
#[allow(deprecated)]
153-
#[deprecated]
154-
pub fn select_files(
152+
pub fn select_files_by_filter(
155153
conn: &mut AnyConnection,
156154
search_input: FileFilter,
157155
limit: i64,

file_classification_core/src/service/group_tag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn create_group_tag(
6969
/// - 减少分组的引用计数
7070
/// - 减少标签的引用计数
7171
/// - 删除分组-标签关联记录
72-
pub fn delete_group_tag_by_id(
72+
pub fn delete_group_tag_by_dto(
7373
conn: &mut AnyConnection,
7474
group_tag_dto: GroupTagDTO,
7575
) -> Result<usize, AppError> {
@@ -165,7 +165,7 @@ pub fn delete_group_tags_by_conditions(
165165
};
166166

167167
// 调用单个删除函数,复用其业务逻辑
168-
let deleted_count = delete_group_tag_by_id(conn, group_tag_dto)?;
168+
let deleted_count = delete_group_tag_by_dto(conn, group_tag_dto)?;
169169
total_deleted += deleted_count;
170170
}
171171

file_classification_core/src/service/groups.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn delete_group(
122122
})
123123
}
124124

125-
/// [已弃用] 根据过滤条件查询分组列表
125+
/// 根据过滤条件查询分组列表
126126
///
127127
/// 参数:
128128
/// - `conn`: 数据库连接对象
@@ -131,9 +131,7 @@ pub fn delete_group(
131131
///
132132
/// 返回值:
133133
/// 查询成功的分组记录列表或数据库错误
134-
#[allow(deprecated)]
135-
#[deprecated]
136-
pub fn select_groups(
134+
pub fn select_groups_by_filter(
137135
conn: &mut AnyConnection,
138136
search_input: GroupFilter,
139137
limit: i64,

file_classification_core/src/service/tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn delete_tag(conn: &mut AnyConnection, tag_id: i32) -> Result<usize, diesel
8080
///
8181
/// 返回值:
8282
/// 查询成功的标签记录列表或数据库错误
83-
pub fn select_tags(
83+
pub fn select_tags_by_filter(
8484
conn: &mut AnyConnection,
8585
search_input: TagFilter,
8686
limit: i64,

file_classification_webapi/src/bin/file_classification_webapi.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,61 @@ async fn main() -> std::io::Result<()> {
1212

1313
let pool = establish_connection_pool();
1414

15+
// 在HttpServer::new中添加新的路由
1516
HttpServer::new(move || {
1617
App::new()
1718
.app_data(web::Data::new(pool.clone()))
1819
.wrap(Logger::default())
19-
.service(handlers::files::api_list_files)
20+
// 文件相关路由
21+
.service(handlers::files::api_list_files_by_filter)
22+
.service(handlers::files::api_get_file_by_id)
2023
.service(handlers::files::api_list_files_by_conditions)
21-
// .service(handlers::files::api_create_file)
24+
.service(handlers::files::api_list_files_by_conditions_with_options)
25+
.service(handlers::files::api_list_files_by_group_id)
26+
.service(handlers::files::api_create_file)
2227
.service(handlers::files::api_update_files_by_conditions)
23-
.service(handlers::files::api_delete_file)
24-
.service(handlers::groups::api_list_groups)
28+
.service(handlers::files::api_update_file_by_id)
29+
.service(handlers::files::api_delete_file_by_id)
30+
.service(handlers::files::api_delete_files_by_conditions)
31+
// 组相关路由
32+
.service(handlers::groups::api_list_groups_by_filter)
33+
.service(handlers::groups::api_get_group_by_id)
2534
.service(handlers::groups::api_list_groups_by_conditions)
35+
.service(handlers::groups::api_list_groups_by_conditions_with_options)
36+
.service(handlers::groups::api_list_groups_by_file_id)
37+
.service(handlers::groups::api_list_groups_by_tag_id)
2638
.service(handlers::groups::api_create_group)
2739
.service(handlers::groups::api_update_groups_by_conditions)
28-
.service(handlers::groups::api_delete_group)
29-
.service(handlers::tags::api_list_tags)
40+
.service(handlers::groups::api_update_group_by_id)
41+
.service(handlers::groups::api_delete_group_by_id)
42+
.service(handlers::groups::api_delete_groups_by_conditions)
43+
// 标签相关路由
44+
.service(handlers::tags::api_list_tags_by_filter)
45+
.service(handlers::tags::api_get_tag_by_id)
3046
.service(handlers::tags::api_list_tags_by_conditions)
47+
.service(handlers::tags::api_list_tags_by_conditions_with_options)
48+
.service(handlers::tags::api_list_tags_by_group_id)
3149
.service(handlers::tags::api_create_tag)
3250
.service(handlers::tags::api_update_tags_by_conditions)
33-
.service(handlers::tags::api_delete_tag)
51+
.service(handlers::tags::api_update_tag_by_id)
52+
.service(handlers::tags::api_delete_tag_by_id)
53+
.service(handlers::tags::api_delete_tags_by_conditions)
54+
// 文件组关联路由
3455
.service(handlers::file_groups::api_list_file_groups_by_conditions)
56+
.service(handlers::file_groups::api_list_file_groups_by_conditions_with_options)
3557
.service(handlers::file_groups::api_create_file_group)
3658
.service(handlers::file_groups::api_delete_file_group)
59+
.service(handlers::file_groups::api_delete_file_groups_by_conditions)
60+
// 组标签关联路由
3761
.service(handlers::group_tags::api_list_group_tags_by_conditions)
62+
.service(handlers::group_tags::api_list_group_tags_by_conditions_with_options)
3863
.service(handlers::group_tags::api_create_group_tag)
3964
.service(handlers::group_tags::api_delete_group_tag)
65+
.service(handlers::group_tags::api_delete_group_tags_by_conditions)
4066
})
41-
.bind("127.0.0.1:8080")?
67+
68+
69+
.bind("127.0.0.1:8082")?
4270
.run()
4371
.await
4472
}

0 commit comments

Comments
 (0)