generated from PeopleForBikes/bna-mechanics-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements entity wrappers to be used in the API instead of using the models directly. Each wrapper also implement the `IntoActiveModel` trait to handle to conversion between the Wrapper and the ActiveModel. Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
3 changed files
with
135 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
mod entities; | ||
pub mod wrappers; | ||
|
||
pub use entities::{prelude::*, *}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::entities::submission; | ||
use sea_orm::{ActiveValue, IntoActiveModel}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Submission { | ||
pub first_name: String, | ||
pub last_name: String, | ||
pub title: Option<String>, | ||
pub organization: Option<String>, | ||
pub email: String, | ||
pub country: String, | ||
pub city: String, | ||
pub region: Option<String>, | ||
pub fips_code: String, | ||
pub consent: bool, | ||
} | ||
|
||
impl IntoActiveModel<submission::ActiveModel> for Submission { | ||
fn into_active_model(self) -> submission::ActiveModel { | ||
submission::ActiveModel { | ||
id: ActiveValue::NotSet, | ||
first_name: ActiveValue::Set(self.first_name), | ||
last_name: ActiveValue::Set(self.last_name), | ||
title: self | ||
.title | ||
.map_or(ActiveValue::NotSet, |v| ActiveValue::Set(Some(v))), | ||
organization: self | ||
.organization | ||
.map_or(ActiveValue::NotSet, |v| ActiveValue::Set(Some(v))), | ||
email: ActiveValue::Set(self.email), | ||
country: ActiveValue::Set(self.country), | ||
city: ActiveValue::Set(self.city), | ||
region: self | ||
.region | ||
.map_or(ActiveValue::NotSet, |v| ActiveValue::Set(Some(v))), | ||
fips_code: ActiveValue::Set(self.fips_code), | ||
consent: ActiveValue::Set(self.consent), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_submission_into_active_model_full() { | ||
let first_name = "John".to_string(); | ||
let last_name = "Doe".to_string(); | ||
let title = Some("Director".to_owned()); | ||
let organization = Some("ACME".to_string()); | ||
let email = "[email protected]".to_string(); | ||
let country = "usa".to_string(); | ||
let city = "austin".to_string(); | ||
let region = Some("texas".to_string()); | ||
let fips_code = "0123456".to_string(); | ||
let consent = true; | ||
let wrapper = Submission { | ||
first_name: first_name.clone(), | ||
last_name: last_name.clone(), | ||
title: title.clone(), | ||
organization: organization.clone(), | ||
email: email.clone(), | ||
country: country.clone(), | ||
city: city.clone(), | ||
region: region.clone(), | ||
fips_code: fips_code.clone(), | ||
consent, | ||
}; | ||
let active_model = wrapper.into_active_model(); | ||
let expected = submission::ActiveModel { | ||
id: ActiveValue::NotSet, | ||
first_name: ActiveValue::Set(first_name), | ||
last_name: ActiveValue::Set(last_name), | ||
title: ActiveValue::Set(title), | ||
organization: ActiveValue::Set(organization), | ||
email: ActiveValue::Set(email), | ||
country: ActiveValue::Set(country), | ||
city: ActiveValue::Set(city), | ||
region: ActiveValue::Set(region), | ||
fips_code: ActiveValue::Set(fips_code), | ||
consent: ActiveValue::Set(consent), | ||
}; | ||
assert_eq!(active_model, expected); | ||
} | ||
|
||
#[test] | ||
fn test_submission_into_active_model_required_only() { | ||
let first_name = "John".to_string(); | ||
let last_name = "Doe".to_string(); | ||
let title = None; | ||
let organization = None; | ||
let email = "[email protected]".to_string(); | ||
let country = "usa".to_string(); | ||
let city = "austin".to_string(); | ||
let region = None; | ||
let fips_code = "0123456".to_string(); | ||
let consent = true; | ||
let wrapper = Submission { | ||
first_name: first_name.clone(), | ||
last_name: last_name.clone(), | ||
title: title.clone(), | ||
organization: organization.clone(), | ||
email: email.clone(), | ||
country: country.clone(), | ||
city: city.clone(), | ||
region: region.clone(), | ||
fips_code: fips_code.clone(), | ||
consent, | ||
}; | ||
let active_model = wrapper.into_active_model(); | ||
let expected = submission::ActiveModel { | ||
id: ActiveValue::NotSet, | ||
first_name: ActiveValue::Set(first_name), | ||
last_name: ActiveValue::Set(last_name), | ||
title: ActiveValue::NotSet, | ||
organization: ActiveValue::NotSet, | ||
email: ActiveValue::Set(email), | ||
country: ActiveValue::Set(country), | ||
city: ActiveValue::Set(city), | ||
region: ActiveValue::NotSet, | ||
fips_code: ActiveValue::Set(fips_code), | ||
consent: ActiveValue::Set(consent), | ||
}; | ||
assert_eq!(active_model, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters