Skip to content

Commit

Permalink
Implement endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Rémy Greinhofer <[email protected]>
  • Loading branch information
rgreinho committed Nov 17, 2024
1 parent 925ac18 commit c600f7a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
21 changes: 21 additions & 0 deletions entity/src/wrappers/census.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::census;
use sea_orm::{prelude::Uuid, ActiveValue, IntoActiveModel};
use serde::Deserialize;

pub struct CensusPost {
pub city_id: Uuid,
Expand All @@ -21,6 +22,26 @@ impl IntoActiveModel<census::ActiveModel> for CensusPost {
}
}

#[derive(Deserialize)]
pub struct CensusFromCityPost {
pub fips_code: String,
pub pop_size: i32,
pub population: i32,
}

impl IntoActiveModel<census::ActiveModel> for CensusFromCityPost {
fn into_active_model(self) -> census::ActiveModel {
census::ActiveModel {
id: ActiveValue::NotSet,
city_id: ActiveValue::NotSet,
created_at: ActiveValue::NotSet,
fips_code: ActiveValue::Set(self.fips_code),
pop_size: ActiveValue::Set(self.pop_size),
population: ActiveValue::Set(self.population),
}
}
}

pub struct CensusPatch {
pub city_id: Option<Uuid>,
pub fips_code: Option<String>,
Expand Down
32 changes: 32 additions & 0 deletions lambdas/src/core/resource/cities/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::db::{
};
use crate::{database_connect, Context, ExecutionError, PageFlow, Paginatron};
use entity::wrappers::{
census::CensusFromCityPost,
city::CityPost,
submission::{SubmissionPatch, SubmissionPost},
};
Expand Down Expand Up @@ -74,6 +75,37 @@ pub async fn get_cities_censuses_adaptor(
))
}

pub async fn post_cities_census_adaptor(
country: &str,
region: &str,
name: &str,
census: CensusFromCityPost,
) -> Result<Value, ExecutionError> {
// Set the database connection.
let db = database_connect(Some("DATABASE_URL_SECRET_ID")).await?;

// Fetch the city.
let city = fetch_city(&db, country, region, name).await?;
if let Some(city) = city {
// Turn the post model into an active model.
let mut active_model: entity::census::ActiveModel = census.into_active_model();

// Update the active model.
active_model.city_id = ActiveValue::Set(city.id);

// And insert a new entry.
let model = active_model.insert(&db).await?;
let value = json!(model);
Ok(value)
} else {
Err(ExecutionError::NotFound(
Some(country.to_string()),
region.to_string(),
name.to_string(),
))
}
}

pub async fn get_cities_ratings_adaptor(
country: &str,
region: &str,
Expand Down
18 changes: 9 additions & 9 deletions lambdas/src/core/resource/cities/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use entity::{census, city, country, state_region_crosswalk, submission, summary};
use sea_orm::{
ColumnTrait, Condition, DatabaseConnection, EntityTrait, PaginatorTrait, QueryFilter,
ColumnTrait, Condition, DatabaseConnection, DbErr, EntityTrait, PaginatorTrait, QueryFilter,
QuerySelect,
};

Expand All @@ -9,7 +9,7 @@ pub async fn fetch_city(
country: &str,
region: &str,
name: &str,
) -> Result<Option<city::Model>, sea_orm::DbErr> {
) -> Result<Option<city::Model>, DbErr> {
city::Entity::find_by_id((country.to_string(), region.to_string(), name.to_string()))
.one(db)
.await
Expand All @@ -19,7 +19,7 @@ pub async fn fetch_cities(
db: &DatabaseConnection,
page: u64,
page_size: u64,
) -> Result<(u64, Vec<city::Model>), sea_orm::DbErr> {
) -> Result<(u64, Vec<city::Model>), DbErr> {
let select = city::Entity::find();
let count = select
.clone()
Expand All @@ -38,7 +38,7 @@ pub async fn fetch_cities_censuses(
name: &str,
page: u64,
page_size: u64,
) -> Result<(u64, Vec<(city::Model, Option<census::Model>)>), sea_orm::DbErr> {
) -> Result<(u64, Vec<(city::Model, Option<census::Model>)>), DbErr> {
let select =
city::Entity::find_by_id((country.to_string(), region.to_string(), name.to_string()))
.find_also_related(census::Entity);
Expand All @@ -58,7 +58,7 @@ pub async fn fetch_cities_ratings(
name: &str,
page: u64,
page_size: u64,
) -> Result<(u64, Vec<(city::Model, Option<summary::Model>)>), sea_orm::DbErr> {
) -> Result<(u64, Vec<(city::Model, Option<summary::Model>)>), DbErr> {
let select =
city::Entity::find_by_id((country.to_string(), region.to_string(), name.to_string()))
.find_also_related(summary::Entity);
Expand All @@ -74,7 +74,7 @@ pub async fn fetch_cities_ratings(
pub async fn fetch_country(
db: &DatabaseConnection,
country: &str,
) -> Result<Option<country::Model>, sea_orm::DbErr> {
) -> Result<Option<country::Model>, DbErr> {
country::Entity::find()
.filter(country::Column::Name.eq(country))
.one(db)
Expand All @@ -84,7 +84,7 @@ pub async fn fetch_country(
pub async fn fetch_state_region_crosswalk(
db: &DatabaseConnection,
state: &str,
) -> Result<Option<state_region_crosswalk::Model>, sea_orm::DbErr> {
) -> Result<Option<state_region_crosswalk::Model>, DbErr> {
state_region_crosswalk::Entity::find()
.filter(state_region_crosswalk::Column::State.eq(state))
.one(db)
Expand All @@ -95,7 +95,7 @@ pub async fn fetch_cities_submission(
db: &DatabaseConnection,
submission_id: i32,
status: Option<String>,
) -> Result<Option<submission::Model>, sea_orm::DbErr> {
) -> Result<Option<submission::Model>, DbErr> {
// Filter the query if needed.
let mut conditions = Condition::all();
if let Some(status) = status {
Expand All @@ -114,7 +114,7 @@ pub async fn fetch_cities_submissions(
status: Option<String>,
page: u64,
page_size: u64,
) -> Result<(u64, Vec<entity::submission::Model>), sea_orm::DbErr> {
) -> Result<(u64, Vec<entity::submission::Model>), DbErr> {
// Filter the query if needed.
let mut conditions = Condition::all();
if let Some(status) = status {
Expand Down
16 changes: 14 additions & 2 deletions lambdas/src/core/resource/cities/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::adaptor::{
get_cities_adaptor, get_cities_censuses_adaptor, get_cities_ratings_adaptor,
get_cities_submission_adaptor, get_cities_submissions_adaptor, get_city_adaptor,
patch_cities_submission_adaptor, post_cities_adaptor, post_cities_submission_adaptor,
patch_cities_submission_adaptor, post_cities_adaptor, post_cities_census_adaptor,
post_cities_submission_adaptor,
};
use crate::{core::resource::cities::CitiesPathParameters, Context, ExecutionError};
use axum::{
Expand All @@ -15,6 +16,7 @@ use axum::{
use axum_extra::extract::OptionalQuery;
use effortless::api::PaginationParameters;
use entity::wrappers::{
census::CensusFromCityPost,
city::CityPost,
submission::{SubmissionPatch, SubmissionPost},
};
Expand All @@ -27,7 +29,7 @@ pub fn routes() -> Router {
.route("/cities/:country/:region/:name", get(get_city))
.route(
"/cities/:country/:region/:name/census",
get(get_city_censuses),
get(get_city_censuses).post(post_city_census),
)
.route(
"/cities/:country/:region/:name/ratings",
Expand Down Expand Up @@ -141,6 +143,16 @@ async fn post_cities_submissions(
.map(|v| (StatusCode::CREATED, Json(v)))
}

#[axum::debug_handler]
async fn post_city_census(
Path(params): Path<CitiesPathParameters>,
Json(census): Json<CensusFromCityPost>,
) -> Result<(StatusCode, Json<Value>), ExecutionError> {
post_cities_census_adaptor(&params.country, &params.region, &params.name, census)
.await
.map(|v| (StatusCode::CREATED, Json(v)))
}

#[cfg(test)]
mod tests {
use axum::extract::Query;
Expand Down

0 comments on commit c600f7a

Please sign in to comment.