From c430f3e1eb9b243298cf4efe2d1e5a9a4667b055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Greinhofer?= Date: Sun, 8 Dec 2024 11:19:35 -0600 Subject: [PATCH] Fix BnaPost wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the wrapper's fields to match the database. Signed-off-by: Rémy Greinhofer --- entity/src/wrappers/bna.rs | 8 +- lambdas/requests.rest | 35 ++++--- lambdas/src/bin/ratings/post-ratings.rs | 16 +-- lambdas/src/core/resource/ratings/adaptor.rs | 97 ++++++++++++++++++- lambdas/src/core/resource/ratings/endpoint.rs | 21 +++- lambdas/src/lib.rs | 2 +- 6 files changed, 148 insertions(+), 31 deletions(-) diff --git a/entity/src/wrappers/bna.rs b/entity/src/wrappers/bna.rs index c9e4ba7..d13a895 100644 --- a/entity/src/wrappers/bna.rs +++ b/entity/src/wrappers/bna.rs @@ -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, @@ -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 { diff --git a/lambdas/requests.rest b/lambdas/requests.rest index 489da2f..9b25a94 100644 --- a/lambdas/requests.rest +++ b/lambdas/requests.rest @@ -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 @@ -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 diff --git a/lambdas/src/bin/ratings/post-ratings.rs b/lambdas/src/bin/ratings/post-ratings.rs index 3d63da4..c90d075 100644 --- a/lambdas/src/bin/ratings/post-ratings.rs +++ b/lambdas/src/bin/ratings/post-ratings.rs @@ -46,7 +46,7 @@ async fn function_handler(event: Request) -> Result, 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), @@ -54,7 +54,7 @@ async fn function_handler(event: Request) -> Result, Error> { }; 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), @@ -65,28 +65,28 @@ async fn function_handler(event: Request) -> Result, 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), @@ -97,7 +97,7 @@ async fn function_handler(event: Request) -> Result, 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), diff --git a/lambdas/src/core/resource/ratings/adaptor.rs b/lambdas/src/core/resource/ratings/adaptor.rs index 5e54509..3c19519 100644 --- a/lambdas/src/core/resource/ratings/adaptor.rs +++ b/lambdas/src/core/resource/ratings/adaptor.rs @@ -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; @@ -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 { + // 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)) +} diff --git a/lambdas/src/core/resource/ratings/endpoint.rs b/lambdas/src/core/resource/ratings/endpoint.rs index 0f45bb8..39fa00f 100644 --- a/lambdas/src/core/resource/ratings/endpoint.rs +++ b/lambdas/src/core/resource/ratings/endpoint.rs @@ -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, }; @@ -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( @@ -107,3 +110,15 @@ async fn patch_ratings_analysis( .await .map(Json) } + +async fn post_ratings( + Json(bna): Json, +) -> Result<(StatusCode, Json), ExecutionError> { + post_ratings_adaptor(bna) + .await + .map_err(|e| { + debug!("{e}"); + e + }) + .map(|v| (StatusCode::CREATED, Json(v))) +} diff --git a/lambdas/src/lib.rs b/lambdas/src/lib.rs index 75d9233..17138f2 100644 --- a/lambdas/src/lib.rs +++ b/lambdas/src/lib.rs @@ -317,7 +317,7 @@ pub async fn api_database_connect(event: &Request) -> APIResult