Skip to content

Commit e67dd91

Browse files
committed
refactor: 重构 translate route
1 parent 14d4a84 commit e67dd91

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/routes/translate.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use actix_web::{get, post, web, HttpResponse, Responder};
1+
use actix_web::{get, post, web, Responder};
22

33
use crate::models::translate::requests::{
44
CurseForgeTranslationRequest, CurseforgeQuery, ModrinthQuery, ModrinthTranslationRequest,
@@ -8,6 +8,7 @@ use crate::models::translate::responses::{
88
};
99
use crate::services::translate::{CurseForgeService, ModrinthService};
1010
use crate::utils::app::AppState;
11+
use crate::errors::ApiError;
1112

1213
#[allow(deprecated)]
1314
pub mod deprecated_routes {
@@ -32,18 +33,14 @@ pub mod deprecated_routes {
3233
pub(super) async fn get_modrinth_translation_deprecated(
3334
query: web::Query<ModrinthQuery>,
3435
data: web::Data<AppState>,
35-
) -> impl Responder {
36+
) -> Result<impl Responder, ApiError> {
3637
let project_id = query.project_id.clone();
37-
if project_id.is_empty() {
38-
return HttpResponse::BadRequest().body("Project ID cannot be empty");
39-
}
4038

4139
let service = ModrinthService::new(data.db.clone());
4240

4341
match service.get_translation(&project_id).await {
44-
Ok(Some(translation)) => HttpResponse::Ok().json(translation),
45-
Ok(None) => HttpResponse::NotFound().body("Translation not found"),
46-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
42+
Ok(translation) => Ok(web::Json(translation)),
43+
Err(e) => Err(ApiError::from(e)),
4744
}
4845
}
4946

@@ -66,15 +63,14 @@ pub mod deprecated_routes {
6663
pub(super) async fn get_curseforge_translation_deprecated(
6764
query: web::Query<CurseforgeQuery>,
6865
data: web::Data<AppState>,
69-
) -> impl Responder {
66+
) -> Result<impl Responder, ApiError> {
7067
let mod_id = query.mod_id;
7168

7269
let service = CurseForgeService::new(data.db.clone());
7370

7471
match service.get_translation(mod_id).await {
75-
Ok(Some(translation)) => HttpResponse::Ok().json(translation),
76-
Ok(None) => HttpResponse::NotFound().body("Translation not found"),
77-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
72+
Ok(translation) => Ok(web::Json(translation)),
73+
Err(e) => Err(ApiError::from(e)),
7874
}
7975
}
8076
}
@@ -111,18 +107,14 @@ pub fn config(cfg: &mut web::ServiceConfig) {
111107
async fn get_modrinth_translation(
112108
path: web::Path<String>,
113109
data: web::Data<AppState>,
114-
) -> impl Responder {
110+
) -> Result<impl Responder, ApiError> {
115111
let project_id = path.into_inner();
116-
if project_id.is_empty() {
117-
return HttpResponse::BadRequest().body("Project ID cannot be empty");
118-
}
119112

120113
let service = ModrinthService::new(data.db.clone());
121114

122115
match service.get_translation(&project_id).await {
123-
Ok(Some(translation)) => HttpResponse::Ok().json(translation),
124-
Ok(None) => HttpResponse::NotFound().body("Translation not found"),
125-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
116+
Ok(translation) => Ok(web::Json(translation)),
117+
Err(e) => Err(ApiError::from(e)),
126118
}
127119
}
128120

@@ -144,15 +136,14 @@ async fn get_modrinth_translation(
144136
async fn get_curseforge_translation(
145137
path: web::Path<i32>,
146138
data: web::Data<AppState>,
147-
) -> impl Responder {
139+
) -> Result<impl Responder, ApiError> {
148140
let mod_id = path.into_inner();
149141

150142
let service = CurseForgeService::new(data.db.clone());
151143

152144
match service.get_translation(mod_id).await {
153-
Ok(Some(translation)) => HttpResponse::Ok().json(translation),
154-
Ok(None) => HttpResponse::NotFound().body("Translation not found"),
155-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
145+
Ok(translation) => Ok(web::Json(translation)),
146+
Err(e) => Err(ApiError::from(e)),
156147
}
157148
}
158149

@@ -171,13 +162,13 @@ async fn get_curseforge_translation(
171162
async fn get_modrinth_translation_batch(
172163
data: web::Data<AppState>,
173164
body: web::Json<ModrinthTranslationRequest>,
174-
) -> impl Responder {
165+
) -> Result<impl Responder, ApiError> {
175166
let project_ids = body.project_ids.clone();
176167
let service = ModrinthService::new(data.db.clone());
177168

178169
match service.get_translations_batch(project_ids).await {
179-
Ok(translation) => HttpResponse::Ok().json(translation),
180-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
170+
Ok(translation) => Ok(web::Json(translation)),
171+
Err(e) => Err(ApiError::from(e)),
181172
}
182173
}
183174

@@ -196,12 +187,12 @@ async fn get_modrinth_translation_batch(
196187
async fn get_curseforge_translation_batch(
197188
data: web::Data<AppState>,
198189
body: web::Json<CurseForgeTranslationRequest>,
199-
) -> impl Responder {
190+
) -> Result<impl Responder, ApiError> {
200191
let mod_ids = body.modids.clone();
201192
let service = CurseForgeService::new(data.db.clone());
202193

203194
match service.get_translations_batch(mod_ids).await {
204-
Ok(translation) => HttpResponse::Ok().json(translation),
205-
Err(e) => HttpResponse::InternalServerError().body(format!("Service error: {:?}", e)),
195+
Ok(translation) => Ok(web::Json(translation)),
196+
Err(e) => Err(ApiError::from(e)),
206197
}
207198
}

src/services/translate.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl ModrinthService {
2121
pub async fn get_translation(
2222
&self,
2323
project_id: &str,
24-
) -> Result<Option<ModrinthTranslationResponse>, ServiceError> {
24+
) -> Result<ModrinthTranslationResponse, ServiceError> {
2525
if project_id.trim().is_empty() {
2626
return Err(ServiceError::InvalidInput {
2727
field: String::from("project_id"),
@@ -35,8 +35,11 @@ impl ModrinthService {
3535
.collection::<ModrinthTranslation>("modrinth_translated");
3636

3737
match collection.find_one(doc! { "_id": project_id }).await? {
38-
Some(doc) => Ok(Some(doc.into())),
39-
None => Ok(None),
38+
Some(doc) => Ok(doc.into()),
39+
None => Err(ServiceError::NotFound {
40+
resource: String::from("Modrinth translation"),
41+
detail: Some(format!("Project ID {}", project_id)),
42+
}),
4043
}
4144
}
4245

@@ -90,7 +93,7 @@ impl CurseForgeService {
9093
pub async fn get_translation(
9194
&self,
9295
mod_id: i32,
93-
) -> Result<Option<CurseForgeTranslationResponse>, ServiceError> {
96+
) -> Result<CurseForgeTranslationResponse, ServiceError> {
9497
if mod_id <= 0 {
9598
return Err(ServiceError::InvalidInput {
9699
field: String::from("mod_id"),
@@ -104,8 +107,11 @@ impl CurseForgeService {
104107
.collection::<CurseForgeTranslation>("curseforge_translated");
105108

106109
match collection.find_one(doc! { "_id": mod_id }).await? {
107-
Some(doc) => Ok(Some(doc.into())),
108-
None => Ok(None),
110+
Some(doc) => Ok(doc.into()),
111+
None => Err(ServiceError::NotFound {
112+
resource: String::from("CurseForge translation"),
113+
detail: Some(format!("Mod ID {}", mod_id)),
114+
}),
109115
}
110116
}
111117

0 commit comments

Comments
 (0)