Handle update merchandise quantity to 0 and < 0

When quantity = 0 remove the merchandise item from the form_list.

When quantity < 0 continue with the update which will fail on the new constraint
This commit is contained in:
Tyler Hallada 2020-10-28 23:40:38 -04:00
parent e831a925f5
commit e482e7764d
5 changed files with 15 additions and 8 deletions

View File

@ -98,8 +98,8 @@ same one in all future requests.
``` ```
http POST "http://localhost:3030/v1/owners" @test_data\owner.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439" http POST "http://localhost:3030/v1/owners" @test_data\owner.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439"
http POST "http://localhost:3030/v1/shops" @test_data\shop.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439" http POST "http://localhost:3030/v1/shops" @test_data\shop.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439"
http POST "http://localhost:3030/v1/interior_ref_lists" @test_data\interior_ref_list.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439" http PATCH "http://localhost:3030/v1/shops/1/interior_ref_list" @test_data\interior_ref_list.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439"
http POST "http://localhost:3030/v1/merchandise_lists" @test_data\merchandise_list.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439" http PATCH "http://localhost:3030/v1/shops/1/merchandise_list" @test_data\merchandise_list.json api-key:"13e2f39c-033f-442f-b42a-7ad640d2e439"
# Then, you can test the GET endpoints # Then, you can test the GET endpoints
http GET "http://localhost:3030/v1/owners" http GET "http://localhost:3030/v1/owners"
http GET "http://localhost:3030/v1/shops" http GET "http://localhost:3030/v1/shops"

View File

@ -29,7 +29,8 @@ CREATE TABLE "merchandise_lists" (
"id" SERIAL PRIMARY KEY NOT NULL, "id" SERIAL PRIMARY KEY NOT NULL,
"shop_id" INTEGER REFERENCES "shops"(id) NOT NULL UNIQUE, "shop_id" INTEGER REFERENCES "shops"(id) NOT NULL UNIQUE,
"owner_id" INTEGER REFERENCES "owners"(id) NOT NULL, "owner_id" INTEGER REFERENCES "owners"(id) NOT NULL,
"form_list" jsonb NOT NULL, "form_list" jsonb NOT NULL
CONSTRAINT "merchandise_quantity_gt_zero" CHECK (form_list = '[]' OR NOT jsonb_path_exists(form_list, '$[*].quantity ? (@ < 1)')),
"created_at" timestamp(3) NOT NULL, "created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL "updated_at" timestamp(3) NOT NULL
); );

View File

@ -7,7 +7,6 @@ use serde::{de::DeserializeOwned, Serialize};
use sqlx::postgres::PgPool; use sqlx::postgres::PgPool;
use std::convert::Infallible; use std::convert::Infallible;
use std::env; use std::env;
use tracing::info;
use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::format::FmtSpan;
use url::Url; use url::Url;
use warp::Filter; use warp::Filter;
@ -278,7 +277,7 @@ async fn main() -> Result<()> {
); );
let buy_merchandise_handler = warp::path("shops").and( let buy_merchandise_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
.and(warp::path("merchandise_list")) .and(warp::path("buy_merchandise"))
.and(warp::path::end()) .and(warp::path::end())
.and(warp::post()) .and(warp::post())
.and(warp::query::<MerchandiseParams>()) .and(warp::query::<MerchandiseParams>())

View File

@ -2,7 +2,6 @@ use anyhow::{Error, Result};
use async_trait::async_trait; use async_trait::async_trait;
use chrono::prelude::*; use chrono::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::postgres::PgPool; use sqlx::postgres::PgPool;
use sqlx::types::Json; use sqlx::types::Json;
use tracing::instrument; use tracing::instrument;
@ -207,13 +206,16 @@ impl MerchandiseList {
"UPDATE "UPDATE
merchandise_lists merchandise_lists
SET SET
form_list = form_list = CASE
jsonb_set( WHEN quantity::int + $4 = 0
THEN form_list - elem_index::int
ELSE jsonb_set(
form_list, form_list,
array[elem_index::text, 'quantity'], array[elem_index::text, 'quantity'],
to_jsonb(quantity::int + $4), to_jsonb(quantity::int + $4),
true true
) )
END
FROM ( FROM (
SELECT SELECT
pos - 1 as elem_index, pos - 1 as elem_index,

View File

@ -73,6 +73,11 @@ pub fn from_anyhow(error: anyhow::Error) -> HttpApiProblem {
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
) )
.set_detail("Owner already has a shop with that name"); .set_detail("Owner already has a shop with that name");
} else if code == "23514" && constraint == "merchandise_quantity_gt_zero" {
return HttpApiProblem::with_title_and_type_from_status(
StatusCode::BAD_REQUEST,
)
.set_detail("Quantity of merchandise must be greater than zero");
} }
} }
} }