1- use actix_web:: { get, post, web, HttpResponse , Responder } ;
1+ use actix_web:: { get, post, web, Responder } ;
22
33use crate :: models:: translate:: requests:: {
44 CurseForgeTranslationRequest , CurseforgeQuery , ModrinthQuery , ModrinthTranslationRequest ,
@@ -8,6 +8,7 @@ use crate::models::translate::responses::{
88} ;
99use crate :: services:: translate:: { CurseForgeService , ModrinthService } ;
1010use crate :: utils:: app:: AppState ;
11+ use crate :: errors:: ApiError ;
1112
1213#[ allow( deprecated) ]
1314pub 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) {
111107async 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(
144136async 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(
171162async 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(
196187async 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}
0 commit comments