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.
Fixes the GET Submissions endpoint by making the `status` query string optional. Refactors the PATCH Submissions and add unit tests. Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
2 changed files
with
58 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
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,6 +1,9 @@ | ||
use dotenv::dotenv; | ||
use effortless::api::{missing_parameter, parse_path_parameter, parse_request_body}; | ||
use entity::{prelude::*, wrappers}; | ||
use effortless::{ | ||
api::{missing_parameter, parse_path_parameter, parse_request_body}, | ||
error::APIErrors, | ||
}; | ||
use entity::{prelude::*, submission::ActiveModel, wrappers}; | ||
use lambda_http::{run, service_fn, Body, Error, IntoResponse, Request, Response}; | ||
use lambdas::database_connect; | ||
use sea_orm::{ActiveValue, EntityTrait, IntoActiveModel}; | ||
|
@@ -10,30 +13,14 @@ use tracing::info; | |
async fn function_handler(event: Request) -> Result<Response<Body>, Error> { | ||
dotenv().ok(); | ||
|
||
// Retrieve the ID of the Submission to update. | ||
let parameter = "id"; | ||
let submission_id = match parse_path_parameter::<i32>(&event, parameter) { | ||
Ok(value) => match value { | ||
Some(v) => v, | ||
None => { | ||
return Ok(missing_parameter(&event, parameter).into()); | ||
} | ||
}, | ||
// Retrieve the model to update. | ||
let active_submission = match prepare_active_model(&event) { | ||
Ok(submission) => submission, | ||
Err(e) => return Ok(e.into()), | ||
}; | ||
|
||
// Extract and deserialize the data. | ||
let wrapper = match parse_request_body::<wrappers::Submission>(&event) { | ||
Ok(value) => value, | ||
Err(e) => return Ok(e.into()), | ||
}; | ||
|
||
// Turn the wrapper into an active model. | ||
let mut active_submission = wrapper.into_active_model(); | ||
active_submission.id = ActiveValue::Set(submission_id); | ||
info!( | ||
"updating Submission {submission_id} into database: {:?}", | ||
active_submission | ||
"updating Submission {:?} into database: {:?}", | ||
active_submission.id, active_submission | ||
); | ||
|
||
// Get the database connection. | ||
|
@@ -56,3 +43,48 @@ async fn main() -> Result<(), Error> { | |
|
||
run(service_fn(function_handler)).await | ||
} | ||
|
||
pub fn prepare_active_model(event: &Request) -> Result<ActiveModel, APIErrors> { | ||
// Retrieve the ID of the Submission to update. | ||
let parameter = "id"; | ||
let submission_id = match parse_path_parameter::<i32>(event, parameter) { | ||
Ok(value) => match value { | ||
Some(v) => v, | ||
None => { | ||
return Err(missing_parameter(event, parameter).into()); | ||
} | ||
}, | ||
Err(e) => return Err(e), | ||
}; | ||
|
||
// Extract and deserialize the data. | ||
let wrapper = match parse_request_body::<wrappers::Submission>(event) { | ||
Ok(value) => value, | ||
Err(e) => return Err(e), | ||
}; | ||
|
||
// Turn the wrapper into an active model. | ||
let mut active_submission = wrapper.into_active_model(); | ||
active_submission.id = ActiveValue::Set(submission_id); | ||
Ok(active_submission) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use lambda_http::RequestExt; | ||
use std::collections::HashMap; | ||
|
||
#[test] | ||
fn test_prepare_active_model() { | ||
let event = Request::new("{\"city\":\"santa rosa\",\"consent\":true,\"country\":\"usa\",\"email\":\"[email protected]\",\"fips_code\":\"3570670\",\"first_name\":\"Jane\",\"id\":1,\"last_name\":\"Doe\",\"organization\":\"Organization LLC\",\"region\":\"new mexico\",\"status\":\"Approved\",\"title\":\"CTO\"}" | ||
.into()).with_path_parameters(HashMap::from([("id".to_string(), "1".to_string())])).with_request_context(lambda_http::request::RequestContext::ApiGatewayV2( | ||
lambda_http::aws_lambda_events::apigw::ApiGatewayV2httpRequestContext::default(), | ||
)); | ||
let active_submission = prepare_active_model(&event).unwrap(); | ||
assert_eq!( | ||
active_submission.country, | ||
sea_orm::ActiveValue::Set("usa".to_string()) | ||
) | ||
} | ||
} |