Skip to content

Commit 3200650

Browse files
committed
fix: lifecycle problem
1 parent e14da58 commit 3200650

10 files changed

Lines changed: 49 additions & 117 deletions

File tree

file_classification_cli/src/bin/file_classification_cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ fn handle_command(command: Cli, conn: &mut AnyConnection, context: &mut Context)
716716
});
717717

718718
let dto = CreateFileDTO {
719-
type_: &type_,
720-
path: &path,
719+
type_,
720+
path,
721721
group_id,
722722
};
723723
match files::create_file(conn, dto) {

file_classification_core/src/model/models.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ pub struct File {
2020

2121
#[derive(Insertable)]
2222
#[diesel(table_name = files)]
23-
pub struct CreateFileDTO<'a, 'b> {
24-
pub type_: &'a str,
25-
pub path: &'b str,
23+
pub struct CreateFileDTO {
24+
pub type_: String,
25+
pub path: String,
2626
pub group_id: i32,
2727
}
2828

@@ -119,12 +119,13 @@ pub struct Group {
119119
pub modify_time: chrono::NaiveDateTime,
120120
}
121121

122-
#[derive(Insertable)]
122+
#[derive(Insertable, Deserialize, Serialize)]
123123
#[diesel(table_name = groups)]
124-
pub struct CreateGroupDTO<'a> {
125-
pub name: &'a str,
124+
pub struct CreateGroupDTO {
125+
pub name: String,
126126
}
127127

128+
128129
#[derive(Deserialize,Clone)]
129130
pub enum GroupCondition {
130131
Id(i32),
@@ -241,10 +242,10 @@ pub struct Tag {
241242
pub reference_count: i32,
242243
}
243244

244-
#[derive(Insertable)]
245+
#[derive(Insertable, Deserialize, Serialize)]
245246
#[diesel(table_name = tags)]
246-
pub struct CreateTagDTO<'a> {
247-
pub name: &'a str,
247+
pub struct CreateTagDTO {
248+
pub name: String,
248249
}
249250

250251
#[derive(Deserialize, Clone)]

file_classification_core/src/service/files.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ use crate::{internal, service};
88
use diesel::Connection;
99
use crate::utils::database::AnyConnection;
1010

11-
pub fn raw_create_file(
11+
pub fn raw_create_file<S1, S2>(
1212
conn: &mut AnyConnection,
13-
type_: &str,
14-
path_: &str,
13+
type_: S1,
14+
path_: S2,
1515
group_id: i32,
16-
) -> Result<usize, diesel::result::Error> {
17-
let new_file = CreateFileDTO { type_, path: path_, group_id };
16+
) -> Result<usize, diesel::result::Error>
17+
where
18+
S1: Into<String>,
19+
S2: Into<String>,
20+
{
21+
let new_file = CreateFileDTO {
22+
type_: type_.into(),
23+
path: path_.into(),
24+
group_id
25+
};
1826
internal::files::create_file(conn, &new_file)
1927
}
2028
// pub fn create_file(

file_classification_core/src/service/groups.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ use diesel::result::Error;
55
use diesel::{Connection, JoinOnDsl};
66
use crate::utils::database::AnyConnection;
77

8-
pub fn create_group_by_name(conn: &mut AnyConnection, name: &str) -> Result<usize, Error> {
9-
let new_group = CreateGroupDTO { name };
8+
pub fn create_group_by_name<S>(conn: &mut AnyConnection, name: S) -> Result<usize, Error>
9+
where
10+
S: Into<String>,
11+
{
12+
let new_group = CreateGroupDTO { name: name.into() };
1013
groups::create_group(conn, &new_group)
1114
}
15+
1216
pub fn create_group(conn: &mut AnyConnection, create_group_dTO: &CreateGroupDTO) -> Result<usize, Error> {
1317
groups::create_group(conn, create_group_dTO)
1418
}

file_classification_core/src/service/tags.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use crate::{
66
use diesel::{Connection, JoinOnDsl};
77
use crate::utils::database::AnyConnection;
88

9-
pub fn create_tag_by_name(conn: &mut AnyConnection, name: &str) -> Result<Tag, diesel::result::Error> {
10-
let new_tag = CreateTagDTO { name };
9+
pub fn create_tag_by_name<S>(conn: &mut AnyConnection, name: S) -> Result<Tag, diesel::result::Error>
10+
where
11+
S: Into<String>,
12+
{
13+
let new_tag = CreateTagDTO { name: name.into() };
1114
tags::create_tag(conn, &new_tag)
1215
}
1316

file_classification_webapi/src/bin/handlers/files.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use actix_web::{get, post, put, delete, web, HttpResponse, Result};
22
use serde_json::json;
33
use file_classification_core::{model::models::{FileCondition, UpdateFileDTO, FileFilter}, service::files::{select_files, select_files_by_conditions, update_files_by_conditions, delete_file}, utils};
4-
use file_classification_core::service::files::create_file;
5-
use crate::utils::database::{DbPool, DbPooledConnection};
6-
use crate::utils::models::{ApiResponse, ApiError, CreateFileDTO};
4+
use crate::utils::database::{DbPool};
5+
use crate::utils::models::{ApiResponse, ApiError};
76

87
#[get("/api/files")]
98
async fn api_list_files(

file_classification_webapi/src/bin/handlers/group_tags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use actix_web::{get, post, delete, web, HttpResponse, Result};
22
use serde_json::json;
33
use file_classification_core::{model::models::GroupTagCondition, service::group_tag::{select_group_tags_by_conditions, create_group_tag, delete_group_tag_by_id}, utils};
44
use file_classification_core::model::models::GroupTagDTO;
5-
use crate::utils::database::{DbPool, DbPooledConnection};
6-
use crate::utils::models::{CreateGroupTagDTO, ApiResponse, ApiError};
5+
use crate::utils::database::{DbPool};
6+
use crate::utils::models::{ ApiResponse, ApiError};
77

88
#[get("/api/group-tags")]
99
async fn api_list_group_tags_by_conditions(

file_classification_webapi/src/bin/handlers/groups.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use actix_web::{get, post, put, delete, web, HttpResponse, Result};
22
use serde_json::json;
33
use file_classification_core::{model::models::{GroupCondition, UpdateGroupDTO, GroupFilter}, service::groups::{select_groups, select_groups_by_conditions, create_group, update_groups_by_conditions, delete_group}, utils};
44
use crate::utils::database::{DbPool, DbPooledConnection};
5-
use crate::utils::models::{CreateGroupDTO, ApiResponse, ApiError};
5+
use crate::utils::models::{ApiResponse, ApiError};
66

77
#[get("/api/groups")]
88
async fn api_list_groups(
@@ -52,11 +52,12 @@ async fn api_list_groups_by_conditions(
5252

5353
#[post("/api/groups")]
5454
async fn api_create_group(
55-
group_dto: web::Json<CreateGroupDTO>,
55+
payload: web::Json<file_classification_core::model::models::CreateGroupDTO>,
5656
pool: web::Data<DbPool>,
5757
) -> Result<HttpResponse> {
5858
let mut conn = pool.get().expect("Failed to get connection from pool");
59-
match create_group(&mut conn, &group_dto.name) {
59+
let group_dto = payload.into_inner();
60+
match create_group(&mut conn, &group_dto) {
6061
Ok(group) => Ok(HttpResponse::Created().json(ApiResponse::from(group))),
6162
Err(e) => Ok(HttpResponse::InternalServerError().json(ApiError {
6263
success: false,

file_classification_webapi/src/bin/handlers/tags.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use actix_web::{get, post, put, delete, web, HttpResponse, Result};
22
use serde_json::json;
33
use file_classification_core::{model::models::{TagCondition, UpdateTagDTO, TagFilter}, service::tags::{select_tags, select_tags_by_conditions, create_tag, update_tags_by_conditions, delete_tag}, utils};
4+
use file_classification_core::model::models::CreateTagDTO;
45
use crate::utils::database::{DbPool, DbPooledConnection};
5-
use crate::utils::models::{CreateTagDTO, ApiResponse, ApiError};
6+
use crate::utils::models::{ApiResponse, ApiError};
67

78
#[get("/api/tags")]
89
async fn api_list_tags(
@@ -52,11 +53,12 @@ async fn api_list_tags_by_conditions(
5253

5354
#[post("/api/tags")]
5455
async fn api_create_tag(
55-
tag_dto: web::Json<CreateTagDTO>,
56+
payload: web::Json<CreateTagDTO>,
5657
pool: web::Data<DbPool>,
5758
) -> Result<HttpResponse> {
5859
let mut conn = pool.get().expect("Failed to get connection from pool");
59-
match create_tag(&mut conn, &tag_dto.name) {
60+
let tag_dto = payload.into_inner();
61+
match create_tag(&mut conn, &tag_dto) {
6062
Ok(tag) => Ok(HttpResponse::Created().json(ApiResponse::from(tag))),
6163
Err(e) => Ok(HttpResponse::InternalServerError().json(ApiError {
6264
success: false,

file_classification_webapi/src/bin/utils/models.rs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,6 @@
33
use serde::{Deserialize, Serialize};
44
use chrono::{NaiveDateTime, serde::ts_seconds_option};
55

6-
// File 相关 DTO
7-
#[derive(Debug, Serialize, Deserialize)]
8-
pub struct CreateFileDTO {
9-
pub type_: String,
10-
pub path: String,
11-
pub group_id: i32,
12-
}
13-
14-
#[derive(Debug, Serialize, Deserialize)]
15-
pub struct UpdateFileDTO {
16-
pub path: Option<String>,
17-
pub type_: Option<String>,
18-
pub reference_count: Option<i32>,
19-
pub group_id: Option<i32>,
20-
}
21-
22-
#[derive(Debug, Serialize, Deserialize)]
23-
pub struct FileFilter {
24-
pub id: Option<i32>,
25-
pub type_: Option<String>,
26-
pub path: Option<String>,
27-
pub reference_count: Option<i32>,
28-
pub group_id: Option<i32>,
29-
}
30-
31-
// Group 相关 DTO
32-
#[derive(Debug, Serialize, Deserialize)]
33-
pub struct CreateGroupDTO {
34-
pub name: String,
35-
}
36-
37-
#[derive(Debug, Serialize, Deserialize)]
38-
pub struct UpdateGroupDTO {
39-
pub name: Option<String>,
40-
pub reference_count: Option<i32>,
41-
pub is_primary: Option<bool>,
42-
pub click_count: Option<i32>,
43-
pub share_count: Option<i32>,
44-
pub create_time: Option<NaiveDateTime>,
45-
pub modify_time: Option<NaiveDateTime>,
46-
}
47-
48-
#[derive(Debug, Serialize, Deserialize)]
49-
pub struct GroupFilter {
50-
pub id: Option<i32>,
51-
pub name: Option<String>,
52-
pub reference_count: Option<i32>,
53-
pub is_primary: Option<bool>,
54-
pub click_count: Option<i32>,
55-
pub share_count: Option<i32>,
56-
pub create_time: Option<NaiveDateTime>,
57-
pub modify_time: Option<NaiveDateTime>,
58-
}
59-
60-
// Tag 相关 DTO
61-
#[derive(Debug, Serialize, Deserialize)]
62-
pub struct CreateTagDTO {
63-
pub name: String,
64-
}
65-
66-
#[derive(Debug, Serialize, Deserialize)]
67-
pub struct UpdateTagDTO {
68-
pub name: Option<String>,
69-
pub reference_count: Option<i32>,
70-
}
71-
72-
#[derive(Debug, Serialize, Deserialize)]
73-
pub struct TagFilter {
74-
pub id: Option<i32>,
75-
pub name: Option<String>,
76-
pub reference_count: Option<i32>,
77-
}
78-
79-
// FileGroup 相关 DTO
80-
#[derive(Debug, Serialize, Deserialize)]
81-
pub struct CreateFileGroupDTO {
82-
pub file_id: i32,
83-
pub group_id: i32,
84-
}
85-
86-
// GroupTag 相关 DTO
87-
#[derive(Debug, Serialize, Deserialize)]
88-
pub struct CreateGroupTagDTO {
89-
pub group_id: i32,
90-
pub tag_id: i32,
91-
}
926

937
// 通用响应结构
948
#[derive(Debug, Serialize)]

0 commit comments

Comments
 (0)