@@ -41,6 +41,10 @@ pub enum StudyError {
41
41
#[ error( "Study '{study_id}', could not be found" ) ]
42
42
#[ editoast_error( status = 404 ) ]
43
43
NotFound { study_id : i64 } ,
44
+ // The study start and end date are in the wrong order
45
+ #[ error( "The study start date must be before the end date" ) ]
46
+ #[ editoast_error( status = 400 ) ]
47
+ StartDateAfterEndDate ,
44
48
}
45
49
46
50
/// This structure is used by the post endpoint to create a study
@@ -68,8 +72,11 @@ struct StudyCreateForm {
68
72
}
69
73
70
74
impl StudyCreateForm {
71
- pub fn into_study ( self , project_id : i64 ) -> Study {
72
- Study {
75
+ pub fn into_study ( self , project_id : i64 ) -> Result < Study > {
76
+ if self . start_date > self . expected_end_date || self . start_date > self . actual_end_date {
77
+ return Err ( StudyError :: StartDateAfterEndDate . into ( ) ) ;
78
+ }
79
+ Ok ( Study {
73
80
name : Some ( self . name ) ,
74
81
project_id : Some ( project_id) ,
75
82
description : Some ( self . description ) ,
@@ -84,7 +91,7 @@ impl StudyCreateForm {
84
91
expected_end_date : Some ( self . expected_end_date ) ,
85
92
actual_end_date : Some ( self . actual_end_date ) ,
86
93
..Default :: default ( )
87
- }
94
+ } )
88
95
}
89
96
}
90
97
@@ -102,7 +109,7 @@ async fn create(
102
109
} ;
103
110
104
111
// Create study
105
- let study: Study = data. into_inner ( ) . into_study ( project_id) ;
112
+ let study: Study = data. into_inner ( ) . into_study ( project_id) ? ;
106
113
let study = study. create ( db_pool. clone ( ) ) . await ?;
107
114
108
115
// Update project last_modification field
@@ -196,9 +203,15 @@ struct StudyPatchForm {
196
203
pub study_type : Option < String > ,
197
204
}
198
205
199
- impl From < StudyPatchForm > for Study {
200
- fn from ( form : StudyPatchForm ) -> Self {
201
- Study {
206
+ impl TryFrom < StudyPatchForm > for Study {
207
+ type Error = crate :: error:: InternalError ;
208
+
209
+ fn try_from ( form : StudyPatchForm ) -> std:: result:: Result < Self , Self :: Error > {
210
+ if form. start_date > form. expected_end_date || form. start_date > form. actual_end_date {
211
+ return Err ( StudyError :: StartDateAfterEndDate . into ( ) ) ;
212
+ }
213
+
214
+ Ok ( Study {
202
215
name : form. name ,
203
216
description : form. description ,
204
217
start_date : Some ( form. start_date ) ,
@@ -211,7 +224,7 @@ impl From<StudyPatchForm> for Study {
211
224
tags : form. tags ,
212
225
study_type : form. study_type ,
213
226
..Default :: default ( )
214
- }
227
+ } )
215
228
}
216
229
}
217
230
@@ -230,7 +243,7 @@ async fn patch(
230
243
} ;
231
244
232
245
// Update study
233
- let study: Study = data. into_inner ( ) . into ( ) ;
246
+ let study: Study = data. into_inner ( ) . try_into ( ) ? ;
234
247
let study = match study. update ( db_pool. clone ( ) , study_id) . await ? {
235
248
Some ( study) => study,
236
249
None => return Err ( StudyError :: NotFound { study_id } . into ( ) ) ,
0 commit comments