Skip to content

Commit 7e5a041

Browse files
committed
editoast: fix study start before end
1 parent 49c076f commit 7e5a041

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

editoast/src/views/study.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub enum StudyError {
4141
#[error("Study '{study_id}', could not be found")]
4242
#[editoast_error(status = 404)]
4343
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,
4448
}
4549

4650
/// This structure is used by the post endpoint to create a study
@@ -68,8 +72,11 @@ struct StudyCreateForm {
6872
}
6973

7074
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 {
7380
name: Some(self.name),
7481
project_id: Some(project_id),
7582
description: Some(self.description),
@@ -84,7 +91,7 @@ impl StudyCreateForm {
8491
expected_end_date: Some(self.expected_end_date),
8592
actual_end_date: Some(self.actual_end_date),
8693
..Default::default()
87-
}
94+
})
8895
}
8996
}
9097

@@ -102,7 +109,7 @@ async fn create(
102109
};
103110

104111
// Create study
105-
let study: Study = data.into_inner().into_study(project_id);
112+
let study: Study = data.into_inner().into_study(project_id)?;
106113
let study = study.create(db_pool.clone()).await?;
107114

108115
// Update project last_modification field
@@ -196,9 +203,15 @@ struct StudyPatchForm {
196203
pub study_type: Option<String>,
197204
}
198205

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 {
202215
name: form.name,
203216
description: form.description,
204217
start_date: Some(form.start_date),
@@ -211,7 +224,7 @@ impl From<StudyPatchForm> for Study {
211224
tags: form.tags,
212225
study_type: form.study_type,
213226
..Default::default()
214-
}
227+
})
215228
}
216229
}
217230

@@ -230,7 +243,7 @@ async fn patch(
230243
};
231244

232245
// Update study
233-
let study: Study = data.into_inner().into();
246+
let study: Study = data.into_inner().try_into()?;
234247
let study = match study.update(db_pool.clone(), study_id).await? {
235248
Some(study) => study,
236249
None => return Err(StudyError::NotFound { study_id }.into()),

0 commit comments

Comments
 (0)