Skip to content

Commit

Permalink
Fix BnaPost wrapper
Browse files Browse the repository at this point in the history
Fixes the wrapper's fields to match the database.

Signed-off-by: Rémy Greinhofer <[email protected]>
  • Loading branch information
rgreinho committed Dec 8, 2024
1 parent 9c2ce02 commit 81e1697
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 31 deletions.
14 changes: 10 additions & 4 deletions entity/src/wrappers/bna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BNASummary {
pub bna_uuid: Uuid,
pub rating_id: Uuid,
pub city_id: Uuid,
pub score: f64,
pub version: String,
Expand Down Expand Up @@ -93,13 +93,13 @@ pub struct BNATransit {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BNAPost {
pub core_services: BNACoreServices,
pub people: BNAPeople,
pub retail: BNARetail,
pub transit: BNATransit,
pub infrastructure: BNAInfrastructure,
pub opportunity: BNAOpportunity,
pub people: BNAPeople,
pub recreation: BNARecreation,
pub retail: BNARetail,
pub summary: BNASummary,
pub transit: BNATransit,
}

impl BNAPost {
Expand All @@ -124,3 +124,9 @@ pub struct BNAPatch {
pub recreation: BNARecreation,
pub summary: BNASummary,
}

// impl IntoActiveModel<bna::ActiveModel> for BNAPost {
// fn into_active_model(self) -> bna::ActiveModel {
// todo!()
// }
// }
35 changes: 21 additions & 14 deletions lambdas/requests.rest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@host=https://api.peopleforbikes.xyz
# @host=http://localhost:3000
# @host=https://api.peopleforbikes.xyz
@host=http://localhost:3000
# Austin, TX, USA.
@city_id=ef8384d5-b96f-439d-a83b-bc801735ddc6
@rating_id=1a759b85-cd87-4bb1-9efa-5789e38e9982
Expand Down Expand Up @@ -192,34 +192,41 @@ Authorization: Bearer {{cognito_access}}
"grocery": 1.69,
"hospitals": 5.18,
"pharmacies": 0,
"social_services": 0
},
"features": {
"people": 19.17,
"retail": 0,
"transit": 0
"score": 3.24
},
"infrastructure": {
"low_stress_miles": 9.3,
"high_stress_miles": 64.5
"high_stress_miles": 64.5,
"low_stress_miles": 9.3
},
"opportunity": {
"employment": 8.26,
"higher_education": 0,
"k12_education": 8.31,
"score": 8.29,
"technical_vocational_college": 0
},
"people": {
"score": 19.17
},
"recreation": {
"community_centers": 0,
"parks": 7.13,
"recreation_trails": 0
"recreation_trails": 0,
"score": 7.13
},
"retail": {
"score": 0
},
"summary": {
"bna_uuid": "3414afd2-42be-412b-9fce-3597b466e291",
"version": "24.04.8"
"city_id": "aac22433-37d4-4cb7-b6b8-e7e77cbbcf41",
"rating_id": "7b80ac57-f32e-44db-8442-e9cbf449b306",
"score": 0,
"version": "24.12"
},
"transit": {
"score": 0
}
}

### Create a new city.
POST {{host}}/cities/
content-type: application/json
Expand Down
16 changes: 8 additions & 8 deletions lambdas/src/bin/ratings/post-ratings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {

// Turn the model wrapper into active models.
let summary = summary::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
city_id: ActiveValue::Set(wrapper.summary.city_id),
created_at: ActiveValue::NotSet,
score: ActiveValue::Set(wrapper.summary.score),
version: ActiveValue::Set(wrapper.summary.version),
};
info!("{:?}", summary);
let core_services = core_services::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
dentists: ActiveValue::Set(wrapper.core_services.dentists),
doctors: ActiveValue::Set(wrapper.core_services.doctors),
grocery: ActiveValue::Set(wrapper.core_services.grocery),
Expand All @@ -65,28 +65,28 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
};
info!("{:?}", core_services);
let people = people::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
score: ActiveValue::Set(wrapper.people.score),
};
info!("{:?}", people);
let retail = retail::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
score: ActiveValue::Set(wrapper.retail.score),
};
info!("{:?}", people);
let transit = transit::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
score: ActiveValue::Set(wrapper.transit.score),
};
info!("{:?}", people);
let infrastructure = infrastructure::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
low_stress_miles: ActiveValue::Set(wrapper.infrastructure.low_stress_miles),
high_stress_miles: ActiveValue::Set(wrapper.infrastructure.high_stress_miles),
};
info!("{:?}", infrastructure);
let opportunity = opportunity::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
employment: ActiveValue::Set(wrapper.opportunity.employment),
higher_education: ActiveValue::Set(wrapper.opportunity.higher_education),
k12_education: ActiveValue::Set(wrapper.opportunity.k12_education),
Expand All @@ -97,7 +97,7 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
};
info!("{:?}", opportunity);
let recreation = recreation::ActiveModel {
id: ActiveValue::Set(wrapper.summary.bna_uuid),
id: ActiveValue::Set(wrapper.summary.rating_id),
community_centers: ActiveValue::Set(wrapper.recreation.community_centers),
parks: ActiveValue::Set(wrapper.recreation.parks),
recreation_trails: ActiveValue::Set(wrapper.recreation.recreation_trails),
Expand Down
97 changes: 96 additions & 1 deletion lambdas/src/core/resource/ratings/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use super::{
BNAComponent,
};
use crate::{database_connect, Context, ExecutionError, PageFlow, Paginatron};
use entity::wrappers::bna_pipeline::{BNAPipelinePatch, BNAPipelinePost};
use entity::{
core_services, infrastructure, opportunity, people, recreation, retail, summary, transit,
wrappers::{
bna::BNAPost,
bna_pipeline::{BNAPipelinePatch, BNAPipelinePost},
},
};
use sea_orm::{ActiveModelTrait, ActiveValue, IntoActiveModel};
use serde_json::{json, Value};
use tracing::info;
Expand Down Expand Up @@ -170,3 +176,92 @@ pub async fn patch_ratings_analysis_adaptor(
let model = active_model.update(&db).await?;
Ok(json!(model))
}

pub async fn post_ratings_adaptor(bna: BNAPost) -> Result<Value, ExecutionError> {
// Turn the model wrapper into active models.
let summary = summary::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
city_id: ActiveValue::Set(bna.summary.city_id),
created_at: ActiveValue::NotSet,
score: ActiveValue::Set(bna.summary.score),
version: ActiveValue::Set(bna.summary.version),
};
info!("{:?}", summary);
let core_services = core_services::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
dentists: ActiveValue::Set(bna.core_services.dentists),
doctors: ActiveValue::Set(bna.core_services.doctors),
grocery: ActiveValue::Set(bna.core_services.grocery),
hospitals: ActiveValue::Set(bna.core_services.hospitals),
pharmacies: ActiveValue::Set(bna.core_services.pharmacies),
score: ActiveValue::Set(bna.core_services.score),
social_services: ActiveValue::Set(bna.core_services.social_services),
};
info!("{:?}", core_services);
let people = people::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
score: ActiveValue::Set(bna.people.score),
};
info!("{:?}", people);
let retail = retail::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
score: ActiveValue::Set(bna.retail.score),
};
info!("{:?}", people);
let transit = transit::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
score: ActiveValue::Set(bna.transit.score),
};
info!("{:?}", people);
let infrastructure = infrastructure::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
low_stress_miles: ActiveValue::Set(bna.infrastructure.low_stress_miles),
high_stress_miles: ActiveValue::Set(bna.infrastructure.high_stress_miles),
};
info!("{:?}", infrastructure);
let opportunity = opportunity::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
employment: ActiveValue::Set(bna.opportunity.employment),
higher_education: ActiveValue::Set(bna.opportunity.higher_education),
k12_education: ActiveValue::Set(bna.opportunity.k12_education),
score: ActiveValue::Set(bna.opportunity.score),
technical_vocational_college: ActiveValue::Set(
bna.opportunity.technical_vocational_college,
),
};
info!("{:?}", opportunity);
let recreation = recreation::ActiveModel {
id: ActiveValue::Set(bna.summary.rating_id),
community_centers: ActiveValue::Set(bna.recreation.community_centers),
parks: ActiveValue::Set(bna.recreation.parks),
recreation_trails: ActiveValue::Set(bna.recreation.recreation_trails),
score: ActiveValue::Set(bna.recreation.score),
};
info!("{:?}", recreation);

// Get the database connection.
let db = database_connect(Some("DATABASE_URL_SECRET_ID")).await?;
info!("DB acquired");

// And insert a new entry for each model.
let summary_res = summary.insert(&db).await?;
let core_services_res = core_services.insert(&db).await?;
let people_res = people.insert(&db).await?;
let retail_res = retail.insert(&db).await?;
let transit_res = transit.insert(&db).await?;
let infrastructure_res = infrastructure.insert(&db).await?;
let opportunity_res = opportunity.insert(&db).await?;
let recreation_res = recreation.insert(&db).await?;
let res = (
summary_res,
core_services_res,
people_res,
retail_res,
transit_res,
infrastructure_res,
opportunity_res,
recreation_res,
);
info!("{:?}", res);
Ok(json!(res))
}
21 changes: 18 additions & 3 deletions lambdas/src/core/resource/ratings/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
adaptor::{
get_rating_adaptor, get_ratings_analyses_adaptor, get_ratings_analysis_adaptor,
get_ratings_city_adaptor, get_ratings_summaries_adaptor, patch_ratings_analysis_adaptor,
post_ratings_analysis_adaptor,
post_ratings_adaptor, post_ratings_analysis_adaptor,
},
BNAComponent,
};
Expand All @@ -15,14 +15,17 @@ use axum::{
Json, Router,
};
use effortless::api::PaginationParameters;
use entity::wrappers::bna_pipeline::{BNAPipelinePatch, BNAPipelinePost};
use entity::wrappers::{
bna::BNAPost,
bna_pipeline::{BNAPipelinePatch, BNAPipelinePost},
};
use serde_json::{json, Value};
use tracing::debug;
use uuid::Uuid;

pub fn routes() -> Router {
Router::new()
.route("/ratings", get(get_ratings))
.route("/ratings", get(get_ratings).post(post_ratings))
.route("/ratings/:rating_id", get(get_rating))
.route("/ratings/:rating_id/city", get(get_ratings_city))
.route(
Expand Down Expand Up @@ -107,3 +110,15 @@ async fn patch_ratings_analysis(
.await
.map(Json)
}

async fn post_ratings(
Json(bna): Json<BNAPost>,
) -> Result<(StatusCode, Json<Value>), ExecutionError> {
post_ratings_adaptor(bna)
.await
.map_err(|e| {
debug!("{e}");
e
})
.map(|v| (StatusCode::CREATED, Json(v)))
}
2 changes: 1 addition & 1 deletion lambdas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub async fn api_database_connect(event: &Request) -> APIResult<DatabaseConnecti
#[derive(Debug, thiserror::Error)]
pub enum ExecutionError {
/// An error from unsuccessful database operations.
#[error("database error")]
#[error("database error: {0:?}")]
DatabaseError(#[from] sea_orm::DbErr),

/// Unexpected error.
Expand Down

0 comments on commit 81e1697

Please sign in to comment.