Skip to content

Commit

Permalink
Refactor /bnas endpoints
Browse files Browse the repository at this point in the history
Refactors the /bnas endpoints to match the OpenAPI specification.

Drive-bys:
- Updates sea-orm to 1.0.0.
- Updates the OAS to add the pagination parameters when needed.

Signed-off-by: Rémy Greinhofer <[email protected]>
  • Loading branch information
rgreinho committed Aug 10, 2024
1 parent 72cc6cf commit dc57bef
Show file tree
Hide file tree
Showing 34 changed files with 444 additions and 340 deletions.
2 changes: 1 addition & 1 deletion entity/src/entities/brokenspoke_pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use super::sea_orm_active_enums::BrokenspokeStep;
use sea_orm::entity::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/census.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/city.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/core_services.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/country.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/fargate_price.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/features.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/infrastructure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
pub mod prelude;

Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/opportunity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
pub use super::brokenspoke_pipeline::Entity as BrokenspokePipeline;
pub use super::census::Entity as Census;
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/recreation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/sea_orm_active_enums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/speed_limit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/state_region_crosswalk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use super::sea_orm_active_enums::BnaRegion;
use sea_orm::entity::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/state_speed_limit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion entity/src/entities/submission.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use super::sea_orm_active_enums::ApprovalStatus;
use sea_orm::entity::prelude::*;
Expand Down
12 changes: 6 additions & 6 deletions entity/src/entities/summary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand All @@ -25,15 +25,15 @@ pub enum Relation {
on_delete = "Cascade"
)]
City,
#[sea_orm(has_many = "super::core_services::Entity")]
#[sea_orm(has_one = "super::core_services::Entity")]
CoreServices,
#[sea_orm(has_many = "super::features::Entity")]
#[sea_orm(has_one = "super::features::Entity")]
Features,
#[sea_orm(has_many = "super::infrastructure::Entity")]
#[sea_orm(has_one = "super::infrastructure::Entity")]
Infrastructure,
#[sea_orm(has_many = "super::opportunity::Entity")]
#[sea_orm(has_one = "super::opportunity::Entity")]
Opportunity,
#[sea_orm(has_many = "super::recreation::Entity")]
#[sea_orm(has_one = "super::recreation::Entity")]
Recreation,
}

Expand Down
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ db-to-dbml: db-dump dbml-from-sql dbml-svg
# Generate models
db-generate-models:
rm -fr {{ entites }}
sea-orm-cli generate entity -o {{ entites }} --with-serde both --date-time-crate time
sea-orm-cli generate entity \
-o {{ entites }} \
--with-serde both \
--date-time-crate time


# Apply migrations and seed the database.
Expand Down
10 changes: 5 additions & 5 deletions lambdas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ path = "src/bnas/get-bnas-cities.rs"

[[bin]]
name = "get-bnas-analysis"
path = "src/bnas-analysis/get-bnas-analysis.rs"
path = "src/bnas/get-bnas-analysis.rs"

[[bin]]
name = "get-cities"
Expand All @@ -74,7 +74,7 @@ path = "src/price-fargate/get-price-fargate.rs"

[[bin]]
name = "patch-bnas-analysis"
path = "src/bnas-analysis/patch-bnas-analysis.rs"
path = "src/bnas/patch-bnas-analysis.rs"

[[bin]]
name = "patch-cities-submissions"
Expand All @@ -86,15 +86,15 @@ path = "src/cities/post-cities-submissions.rs"

[[bin]]
name = "post-bnas-analysis"
path = "src/bnas-analysis/post-bnas-analysis.rs"
path = "src/bnas/post-bnas-analysis.rs"

[[bin]]
name = "post-bnas-enqueue"
path = "src/bnas-enqueue/post-bnas-enqueue.rs"
path = "src/bnas/post-bnas-enqueue.rs"

[[bin]]
name = "get-bnas-results"
path = "src/bnas-results/get-bnas-results.rs"
path = "src/bnas/get-bnas-results.rs"

[[bin]]
name = "post-cities"
Expand Down
91 changes: 0 additions & 91 deletions lambdas/src/bnas-analysis/get-bnas-analysis.rs

This file was deleted.

80 changes: 80 additions & 0 deletions lambdas/src/bnas/get-bnas-analysis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use dotenv::dotenv;
use effortless::{
api::{entry_not_found, internal_error},
fragment::BnaRequestExt,
};
use entity::prelude::*;
use lambda_http::{run, service_fn, Body, Error, IntoResponse, Request, Response};
use lambdas::{
api_database_connect, bnas::extract_path_parameters, build_paginated_response,
pagination_parameters,
};
use sea_orm::{EntityTrait, PaginatorTrait};
use serde_json::json;
use tracing::{debug, info};

async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
dotenv().ok();

// Set the database connection.
let db = match api_database_connect(&event).await {
Ok(db) => db,
Err(e) => return Ok(e),
};

if !event.has_path_parameters() {
// Retrieve pagination parameters if any.
let (page_size, page) = match pagination_parameters(&event) {
Ok((page_size, page)) => (page_size, page),
Err(e) => return Ok(e),
};

//
let select = BrokenspokePipeline::find();
let query = select
.clone()
.paginate(&db, page_size)
.fetch_page(page - 1)
.await;
let res: Response<Body> = match query {
Ok(models) => {
let total_items = select.count(&db).await?;
build_paginated_response(json!(models), total_items, page, page_size, &event)?
}
Err(e) => internal_error(&event, e.to_string().as_str()).into(),
};
return Ok(res);
}

let params = match extract_path_parameters(&event) {
Ok(p) => p,
Err(e) => return Ok(e.into()),
};

// Retrieve a specific entry.
debug!("Processing the requests...");
let model = BrokenspokePipeline::find_by_id(params.bna_id)
.one(&db)
.await?;
let res: Response<Body> = match model {
Some(model) => json!(model).into_response().await,
None => entry_not_found(&event).into(),
};
Ok(res)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
// disable printing the name of the module in every log line.
.with_target(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();

run(service_fn(function_handler)).await.map_err(|e| {
info!("{e}");
e
})
}
Loading

0 comments on commit dc57bef

Please sign in to comment.