1
1
use crate :: error:: Result ;
2
2
use crate :: schema:: operation:: RailjsonObject ;
3
3
use crate :: schema:: { OSRDIdentified , ObjectType } ;
4
+ use diesel:: result:: Error as DieselError ;
4
5
use diesel:: sql_types:: { BigInt , Json , Jsonb , Text } ;
5
6
use diesel:: { sql_query, PgConnection , QueryableByName , RunQueryDsl } ;
6
7
use json_patch:: Patch ;
@@ -21,13 +22,24 @@ impl UpdateOperation {
21
22
pub fn apply ( & self , infra_id : i64 , conn : & mut PgConnection ) -> Result < RailjsonObject > {
22
23
// Load object
23
24
24
- let mut obj: DataObject = sql_query ( format ! (
25
+ let mut obj: DataObject = match sql_query ( format ! (
25
26
"SELECT data FROM {} WHERE infra_id = $1 AND obj_id = $2" ,
26
27
self . obj_type. get_table( )
27
28
) )
28
29
. bind :: < BigInt , _ > ( infra_id)
29
30
. bind :: < Text , _ > ( & self . obj_id )
30
- . get_result ( conn) ?;
31
+ . get_result ( conn)
32
+ {
33
+ Ok ( obj) => obj,
34
+ Err ( DieselError :: NotFound ) => {
35
+ return Err ( OperationError :: ObjectNotFound {
36
+ obj_id : self . obj_id . clone ( ) ,
37
+ infra_id,
38
+ }
39
+ . into ( ) )
40
+ }
41
+ Err ( err) => return Err ( err. into ( ) ) ,
42
+ } ;
31
43
32
44
// Apply and check patch
33
45
let railjson_obj = obj. patch_and_check ( self ) ?;
@@ -43,7 +55,11 @@ impl UpdateOperation {
43
55
. execute ( conn)
44
56
{
45
57
Ok ( 1 ) => Ok ( railjson_obj) ,
46
- Ok ( _) => Err ( OperationError :: ObjectNotFound ( self . obj_id . clone ( ) ) . into ( ) ) ,
58
+ Ok ( _) => Err ( OperationError :: ObjectNotFound {
59
+ obj_id : self . obj_id . clone ( ) ,
60
+ infra_id,
61
+ }
62
+ . into ( ) ) ,
47
63
Err ( err) => Err ( err. into ( ) ) ,
48
64
}
49
65
}
@@ -90,7 +106,6 @@ mod tests {
90
106
use crate :: schema:: operation:: OperationError ;
91
107
use crate :: schema:: { OSRDIdentified , ObjectType } ;
92
108
use actix_web:: test as actix_test;
93
- use actix_web:: ResponseError ;
94
109
use diesel:: sql_query;
95
110
use diesel:: sql_types:: { Double , Text } ;
96
111
use diesel:: RunQueryDsl ;
@@ -157,8 +172,8 @@ mod tests {
157
172
158
173
assert ! ( res. is_err( ) ) ;
159
174
assert_eq ! (
160
- res. unwrap_err( ) . status_code ( ) ,
161
- OperationError :: ModifyId . get_status ( )
175
+ res. unwrap_err( ) . get_type ( ) ,
176
+ OperationError :: ModifyId . get_type ( )
162
177
) ;
163
178
} )
164
179
. await ;
@@ -250,4 +265,33 @@ mod tests {
250
265
assert_eq ! ( updated_speed. val, 80.0 ) ;
251
266
} ) . await ;
252
267
}
268
+
269
+ #[ actix_test]
270
+ async fn wrong_id_update_track ( ) {
271
+ test_infra_transaction ( |conn, infra| {
272
+ let update_track = UpdateOperation {
273
+ obj_id : "non_existent_id" . to_string ( ) ,
274
+ obj_type : ObjectType :: TrackSection ,
275
+ railjson_patch : from_str (
276
+ r#"[
277
+ { "op": "replace", "path": "/length", "value": 80.0 }
278
+ ]"# ,
279
+ )
280
+ . unwrap ( ) ,
281
+ } ;
282
+
283
+ let res = update_track. apply ( infra. id . unwrap ( ) , conn) ;
284
+
285
+ assert ! ( res. is_err( ) ) ;
286
+ assert_eq ! (
287
+ res. unwrap_err( ) . get_type( ) ,
288
+ OperationError :: ObjectNotFound {
289
+ obj_id: "non_existent_id" . to_string( ) ,
290
+ infra_id: infra. id. unwrap( )
291
+ }
292
+ . get_type( )
293
+ ) ;
294
+ } )
295
+ . await ;
296
+ }
253
297
}
0 commit comments