Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix submission endpoint #62

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion entity/src/entities/census.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "census")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub census_id: i32,
pub city_id: Uuid,
pub created_at: Option<DateTimeWithTimeZone>,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "city")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub city_id: Uuid,
pub country: String,
#[sea_orm(column_type = "Double")]
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/core_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "core_services")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
#[sea_orm(column_type = "Double")]
pub dentists: f64,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "features")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
#[sea_orm(column_type = "Double")]
pub people: f64,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "infrastructure")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
#[sea_orm(column_type = "Double")]
pub low_stress_miles: f64,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/opportunity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "opportunity")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
#[sea_orm(column_type = "Double")]
pub employment: f64,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/ranking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "ranking")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub ranking_id: i32,
pub city_id: Uuid,
pub country: i32,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/recreation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "recreation")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
#[sea_orm(column_type = "Double")]
pub community_centers: f64,
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/speed_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "speed_limit")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub speed_limit_id: i32,
pub city_id: Uuid,
pub created_at: Option<DateTimeWithTimeZone>,
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/submission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "submission")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
pub first_name: String,
pub last_name: String,
Expand All @@ -18,6 +17,7 @@ pub struct Model {
pub city: String,
pub region: Option<String>,
pub fips_code: String,
pub consent: bool,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
1 change: 0 additions & 1 deletion entity/src/entities/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "summary")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[serde(skip_deserializing)]
pub bna_uuid: Uuid,
pub city_id: Uuid,
pub created_at: Option<DateTimeWithTimeZone>,
Expand Down
3 changes: 1 addition & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ lint-spellcheck:
# Generate models
db-generate-models:
rm -fr {{ entites }}
sea-orm-cli generate entity -o {{ entites }} --with-serde both --serde-skip-deserializing-primary-key
# sea-orm-cli generate entity -o {{ entites }} --with-serde both -t submission --serde-skip-deserializing-primary-key
sea-orm-cli generate entity -o {{ entites }} --with-serde both


# Apply migrations and seed the database.
Expand Down
4 changes: 2 additions & 2 deletions lambdas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Repository containing all the lambda endpoints for API Gateway.
## Testing

- 2 terminals are required to test the functions.
- Start Docker Compose: `docker compose ud [-d]`
- Start Docker Compose: `docker compose up [-d]`
- Export the database URL:

```bash
Expand Down Expand Up @@ -48,7 +48,7 @@ cargo lambda invoke --data-file lambdas/src/fixtures/${LAMBDA}.json ${LAMBDA}
For instance:

```bash
cargo lambda invoke get-cities --data-file lambdas/src/fixtures/get-cities.json
cargo lambda invoke --data-file lambdas/src/fixtures/get-cities.json get-cities
```

[cargo lambda]: https://www.cargo-lambda.info/
3 changes: 2 additions & 1 deletion lambdas/requests.rest
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ content-type: application/json
"last_name": "Doe",
"organization": "Organization LLC",
"region": "new mexico",
"title": "CTO"
"title": "CTO",
"consent": true
}

###
Expand Down
2 changes: 1 addition & 1 deletion lambdas/src/fixtures/post-submissions-city.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "{\"first_name\": \"Jane\",\"last_name\": \"Doe\",\"organization\": \"Organization LLC\",\"title\": \"CTO\",\"email\": \"[email protected]\",\"country\": \"usa\",\"city\": \"santa rosa\",\"region\": \"new mexico\",\"fips_code\": \"3570670\"}",
"body": "{\n \"city\": \"santa rosa\",\n \"country\": \"usa\",\n \"email\": \"[email protected]\",\n \"fips_code\": \"3570670\",\n \"first_name\": \"Jane\",\n \"last_name\": \"Doe\",\n \"organization\": \"Organization LLC\",\n \"region\": \"new mexico\",\n \"title\": \"CTO\",\n \"consent\": true\n}",
"pathParameters": {
"parameter1": "value1"
},
Expand Down
41 changes: 36 additions & 5 deletions lambdas/src/submissions/post-submissions-city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ use lambda_http::{
};
use lambdas::{database_connect, get_apigw_request_id, APIError, APIErrorSource, APIErrors};
use sea_orm::{ActiveValue, EntityTrait};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::info;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubmissionWrapper {
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,
}

async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
dotenv().ok();
Expand All @@ -17,7 +33,8 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
let apigw_request_id = get_apigw_request_id(&event);
let body = event.body();
let body_str = std::str::from_utf8(body).expect("invalid utf-8 sequence");
let model = match serde_json::from_str::<submission::Model>(body_str) {
info!(body_str);
let wrapper = match serde_json::from_str::<SubmissionWrapper>(body_str) {
Ok(model) => model,
Err(e) => {
let api_error = APIError::new(
Expand All @@ -31,10 +48,24 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
}
};

// Turn the model into an active model and unset the primary key.
let mut active_submission: submission::ActiveModel = model.into();
active_submission.id = ActiveValue::NotSet;
dbg!(&active_submission);
// Turn the model wrapper into an active model and unset the primary key.
let active_submission = submission::ActiveModel {
id: ActiveValue::NotSet,
first_name: ActiveValue::Set(wrapper.first_name),
last_name: ActiveValue::Set(wrapper.last_name),
title: ActiveValue::Set(wrapper.title),
organization: ActiveValue::Set(wrapper.organization),
email: ActiveValue::Set(wrapper.email),
country: ActiveValue::Set(wrapper.country),
city: ActiveValue::Set(wrapper.city),
region: ActiveValue::Set(wrapper.region),
fips_code: ActiveValue::Set(wrapper.fips_code),
consent: ActiveValue::Set(wrapper.consent),
};
info!(
"inserting Submission into database: {:?}",
active_submission
);

// Insert the submission into the database.
let res = submission::Entity::insert(active_submission)
Expand Down
1 change: 1 addition & 0 deletions migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::enum_variant_names)]
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
Expand Down
2 changes: 2 additions & 0 deletions migration/src/m20231010_232527_city_submission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl MigrationTrait for Migration {
.not_null()
.default("0"),
)
.col(ColumnDef::new(Submission::Consent).boolean().not_null())
.to_owned(),
)
.await
Expand All @@ -57,4 +58,5 @@ enum Submission {
City,
Region,
FIPSCode,
Consent,
}