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 POST `/cities/submissions` endpoint and the related integration tests. Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
5 changed files
with
75 additions
and
19 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,12 +1,12 @@ | ||
use dotenv::dotenv; | ||
use effortless::api::parse_request_body; | ||
use effortless::api::{invalid_body, parse_request_body}; | ||
use entity::wrappers::submission::SubmissionPost; | ||
use lambda_http::{ | ||
http::{header, StatusCode}, | ||
run, service_fn, Body, Error, Request, Response, | ||
}; | ||
use lambdas::database_connect; | ||
use sea_orm::{ActiveModelTrait, IntoActiveModel}; | ||
use lambdas::{database_connect, db::find_country}; | ||
use sea_orm::{ActiveModelTrait, ActiveValue, IntoActiveModel}; | ||
use serde_json::json; | ||
use tracing::info; | ||
|
||
|
@@ -18,19 +18,38 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> { | |
Ok(value) => value, | ||
Err(e) => return Ok(e.into()), | ||
}; | ||
let country = wrapper.country.clone(); | ||
|
||
// Turn the model wrapper into an active model. | ||
let active_submission = wrapper.into_active_model(); | ||
let mut active_submission = wrapper.into_active_model(); | ||
|
||
// Get the database connection. | ||
let db = database_connect(Some("DATABASE_URL_SECRET_ID")).await?; | ||
|
||
// Add the status. | ||
active_submission.status = ActiveValue::Set("Pending".to_string()); | ||
|
||
// Ensure the country is a valid one. | ||
let country_found = find_country(&db, &country).await?; | ||
dbg!(country_found); | ||
if !find_country(&db, &country).await? { | ||
return Ok(invalid_body( | ||
&event, | ||
format!("the country `{country}` is not in the list of countries supported by the BNA") | ||
.as_str(), | ||
) | ||
.into()); | ||
} | ||
|
||
// And insert a new entry. | ||
info!( | ||
"inserting Submission into database: {:?}", | ||
active_submission | ||
); | ||
let submission = active_submission.insert(&db).await?; | ||
let submission = match active_submission.insert(&db).await { | ||
Ok(m) => m, | ||
Err(e) => return Ok(invalid_body(&event, e.to_string().as_str()).into()), | ||
}; | ||
let response = Response::builder() | ||
.status(StatusCode::CREATED) | ||
.header(header::CONTENT_TYPE, "application/json") | ||
|
@@ -54,3 +73,38 @@ async fn main() -> Result<(), Error> { | |
e | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
// use super::*; | ||
// use aws_lambda_events::http; | ||
// use lambda_http::RequestExt; | ||
|
||
// #[tokio::test] | ||
// async fn test_handler() { | ||
// let event = http::Request::builder() | ||
// .header(http::header::CONTENT_TYPE, "application/json") | ||
// .body(Body::Text( | ||
// r#"{ | ||
// "city": "santa rosa", | ||
// "country": "United States", | ||
// "email": "[email protected]", | ||
// "fips_code": "3570670", | ||
// "first_name": "Jane", | ||
// "last_name": "Doe", | ||
// "organization": "Organization LLC", | ||
// "region": "new mexico", | ||
// "occupation": "CTO", | ||
// "consent": true | ||
// }"# | ||
// .to_string(), | ||
// )) | ||
// .expect("failed to build request") | ||
// .with_request_context(lambda_http::request::RequestContext::ApiGatewayV2( | ||
// lambda_http::aws_lambda_events::apigw::ApiGatewayV2httpRequestContext::default(), | ||
// )); | ||
// let r = function_handler(event).await.unwrap(); | ||
// dbg!(r); | ||
// } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use entity::{country, prelude::*}; | ||
use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter}; | ||
|
||
pub async fn find_country(db: &DatabaseConnection, country: &str) -> Result<bool, DbErr> { | ||
Ok(Country::find() | ||
.filter(country::Column::Name.eq(country)) | ||
.one(db) | ||
.await? | ||
.is_some()) | ||
} |
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,5 +1,6 @@ | ||
pub mod bnas; | ||
pub mod cities; | ||
pub mod db; | ||
pub mod link_header; | ||
|
||
use bnacore::aws::get_aws_secrets_value; | ||
|
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 |
---|---|---|
|
@@ -28,14 +28,14 @@ content-type: application/json | |
|
||
{ | ||
"city": "santa rosa", | ||
"country": "usa", | ||
"country": "United States", | ||
"email": "[email protected]", | ||
"fips_code": "3570670", | ||
"first_name": "Jane", | ||
"last_name": "Doe", | ||
"organization": "Organization LLC", | ||
"region": "new mexico", | ||
"title": "CTO", | ||
"occupation": "CTO", | ||
"consent": true | ||
} | ||
HTTP 200 | ||
|