Skip to content

Commit

Permalink
editoast: properly handle partial patches for scenario, study and pro…
Browse files Browse the repository at this point in the history
…ject using serde with double option

Signed-off-by: Alice Khoudli <[email protected]>
  • Loading branch information
Synar committed Jan 21, 2025
1 parent 614de1c commit 1545dce
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
33 changes: 33 additions & 0 deletions editoast/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions editoast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ serde_json.workspace = true
serde_qs = { version = "0.13.0", git = "https://github.com/Atrox/serde_qs", rev = "2cfa3ee0", features = [
"axum",
] }
serde_with = "3.12.0"
serde_yaml = "0.9.34"
sha1 = "0.10"
strum.workspace = true
Expand Down
28 changes: 17 additions & 11 deletions editoast/src/views/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use editoast_models::DbConnection;
use editoast_models::DbConnectionPoolV2;
use serde::Deserialize;
use serde::Serialize;
use serde_with::rust::double_option;
use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;
Expand Down Expand Up @@ -294,14 +295,19 @@ struct ProjectPatchForm {
#[schema(max_length = 128)]
pub name: Option<String>,
#[schema(max_length = 1024)]
pub description: Option<String>,
#[serde(default, with = "double_option")]
pub description: Option<Option<String>>,
#[schema(max_length = 4096)]
pub objectives: Option<String>,
#[serde(default, with = "double_option")]
pub objectives: Option<Option<String>>,
#[schema(max_length = 1024)]
pub funders: Option<String>,
pub budget: Option<i32>,
#[serde(default, with = "double_option")]
pub funders: Option<Option<String>>,
#[serde(default, with = "double_option")]
pub budget: Option<Option<i32>>,
/// The id of the image document
pub image: Option<i64>,
#[serde(default, with = "double_option")]
pub image: Option<Option<i64>>,
#[schema(max_length = 255)]
pub tags: Option<Tags>,
}
Expand All @@ -310,11 +316,11 @@ impl From<ProjectPatchForm> for Changeset<Project> {
fn from(project: ProjectPatchForm) -> Self {
Project::changeset()
.flat_name(project.name)
.description(project.description)
.objectives(project.objectives)
.funders(project.funders)
.flat_budget(Some(project.budget))
.flat_image(Some(project.image))
.flat_description(project.description)
.flat_objectives(project.objectives)
.flat_funders(project.funders)
.flat_budget(project.budget)
.flat_image(project.image)
.flat_tags(project.tags)
.last_modification(Utc::now().naive_utc())
}
Expand Down Expand Up @@ -348,7 +354,7 @@ async fn patch(
return Err(AuthorizationError::Forbidden.into());
}
let conn = &mut db_pool.get().await?;
if let Some(image) = form.image {
if let Some(Some(image)) = form.image {
check_image_content(conn, image).await?;
}
let project_changeset: Changeset<Project> = form.into();
Expand Down
2 changes: 2 additions & 0 deletions editoast/src/views/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use editoast_models::DbConnection;
use editoast_models::DbConnectionPoolV2;
use serde::Deserialize;
use serde::Serialize;
use serde_with::rust::double_option;
use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;
Expand Down Expand Up @@ -327,6 +328,7 @@ struct ScenarioPatchForm {
pub description: Option<String>,
pub tags: Option<Tags>,
pub infra_id: Option<i64>,
#[serde(default, with = "double_option")]
pub electrical_profile_set_id: Option<Option<i64>>,
}

Expand Down
41 changes: 25 additions & 16 deletions editoast/src/views/study.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use editoast_models::DbConnection;
use editoast_models::DbConnectionPoolV2;
use serde::Deserialize;
use serde::Serialize;
use serde_with::rust::double_option;
use thiserror::Error;
use utoipa::IntoParams;
use utoipa::ToSchema;
Expand Down Expand Up @@ -274,32 +275,40 @@ async fn get(
#[derivative(Default)]
struct StudyPatchForm {
pub name: Option<String>,
pub description: Option<String>,
pub start_date: Option<NaiveDate>,
pub expected_end_date: Option<NaiveDate>,
pub actual_end_date: Option<NaiveDate>,
pub business_code: Option<String>,
pub service_code: Option<String>,
pub budget: Option<i32>,
#[serde(default, with = "double_option")]
pub description: Option<Option<String>>,
#[serde(default, with = "double_option")]
pub start_date: Option<Option<NaiveDate>>,
#[serde(default, with = "double_option")]
pub expected_end_date: Option<Option<NaiveDate>>,
#[serde(default, with = "double_option")]
pub actual_end_date: Option<Option<NaiveDate>>,
#[serde(default, with = "double_option")]
pub business_code: Option<Option<String>>,
#[serde(default, with = "double_option")]
pub service_code: Option<Option<String>>,
#[serde(default, with = "double_option")]
pub budget: Option<Option<i32>>,
pub tags: Option<Tags>,
pub state: Option<String>,
pub study_type: Option<String>,
#[serde(default, with = "double_option")]
pub study_type: Option<Option<String>>,
}

impl StudyPatchForm {
pub fn into_study_changeset(self) -> Result<Changeset<Study>> {
let study_changeset = Study::changeset()
.flat_name(self.name)
.description(self.description)
.business_code(self.business_code)
.service_code(self.service_code)
.flat_start_date(Some(self.start_date))
.flat_expected_end_date(Some(self.expected_end_date))
.flat_actual_end_date(Some(self.actual_end_date))
.flat_budget(Some(self.budget))
.flat_description(self.description)
.flat_business_code(self.business_code)
.flat_service_code(self.service_code)
.flat_start_date(self.start_date)
.flat_expected_end_date(self.expected_end_date)
.flat_actual_end_date(self.actual_end_date)
.flat_budget(self.budget)
.flat_tags(self.tags)
.flat_state(self.state)
.study_type(self.study_type);
.flat_study_type(self.study_type);
Study::validate(&study_changeset)?;
Ok(study_changeset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function StateStep({ study, number, state, done }: Props) {
await patchStudy({
projectId: study.project.id,
studyId: study.id,
studyPatchForm: { ...study, actual_end_date, state },
studyPatchForm: { actual_end_date, state },
});
dispatch(
setSuccess({
Expand Down

0 comments on commit 1545dce

Please sign in to comment.