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
63 additions
and
30 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,57 @@ | ||
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: ActiveValue::Set(self.title), | ||
organization: ActiveValue::Set(self.organization), | ||
email: ActiveValue::Set(self.email), | ||
country: ActiveValue::Set(self.country), | ||
city: ActiveValue::Set(self.city), | ||
region: ActiveValue::Set(self.region), | ||
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() { | ||
let wrapper = Submission { | ||
first_name: "John".to_string(), | ||
last_name: "Doe".to_string(), | ||
title: Some("Director".to_owned()), | ||
organization: Some("ACME".to_string()), | ||
email: "[email protected]".to_string(), | ||
country: "usa".to_string(), | ||
city: "austin".to_string(), | ||
region: Some("texas".to_string()), | ||
fips_code: "0123456".to_string(), | ||
consent: true, | ||
}; | ||
let _active_model = wrapper.into_active_model(); | ||
} | ||
} |
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