WIP adding interior_refs endpoints

Ran into some limitations of sqlx while trying to bulk create interior_refs. I
also discovered how slow creating hundreds of rows in postgres is and I'm
planning on saving interior_refs data in a jsonb column instead which seems to
be much faster.
This commit is contained in:
2020-07-19 03:01:20 -04:00
parent 9985f123c9
commit 65e6ba1f8a
10 changed files with 272 additions and 14 deletions

View File

@@ -4,7 +4,7 @@ use warp::http::StatusCode;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use super::models::{ListParams, Model, Owner, Shop};
use super::models::{InteriorRef, ListParams, Model, Owner, Shop};
use super::problem::reject_anyhow;
use super::Environment;
@@ -74,3 +74,46 @@ pub async fn create_owner(
let reply = with_status(reply, StatusCode::CREATED);
Ok(reply)
}
pub async fn get_interior_ref(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
let interior_ref = InteriorRef::get(&env.db, id).await.map_err(reject_anyhow)?;
let reply = json(&interior_ref);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
}
pub async fn list_interior_refs(
list_params: ListParams,
env: Environment,
) -> Result<impl Reply, Rejection> {
let interior_refs = InteriorRef::list(&env.db, list_params)
.await
.map_err(reject_anyhow)?;
let reply = json(&interior_refs);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
}
pub async fn create_interior_ref(
interior_ref: InteriorRef,
env: Environment,
) -> Result<impl Reply, Rejection> {
let saved_interior_ref = interior_ref.save(&env.db).await.map_err(reject_anyhow)?;
let url = saved_interior_ref
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&saved_interior_ref);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
Ok(reply)
}
pub async fn bulk_create_interior_refs(
interior_refs: Vec<InteriorRef>,
env: Environment,
) -> Result<impl Reply, Rejection> {
InteriorRef::bulk_save(&env.db, interior_refs)
.await
.map_err(reject_anyhow)?;
Ok(StatusCode::CREATED)
}