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:
parent
e831a925f5
commit
e482e7764d
@ -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/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 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/interior_ref_list" @test_data\interior_ref_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
|
||||
http GET "http://localhost:3030/v1/owners"
|
||||
http GET "http://localhost:3030/v1/shops"
|
||||
|
@ -29,7 +29,8 @@ CREATE TABLE "merchandise_lists" (
|
||||
"id" SERIAL PRIMARY KEY NOT NULL,
|
||||
"shop_id" INTEGER REFERENCES "shops"(id) NOT NULL UNIQUE,
|
||||
"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,
|
||||
"updated_at" timestamp(3) NOT NULL
|
||||
);
|
@ -7,7 +7,6 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||
use sqlx::postgres::PgPool;
|
||||
use std::convert::Infallible;
|
||||
use std::env;
|
||||
use tracing::info;
|
||||
use tracing_subscriber::fmt::format::FmtSpan;
|
||||
use url::Url;
|
||||
use warp::Filter;
|
||||
@ -278,7 +277,7 @@ async fn main() -> Result<()> {
|
||||
);
|
||||
let buy_merchandise_handler = warp::path("shops").and(
|
||||
warp::path::param()
|
||||
.and(warp::path("merchandise_list"))
|
||||
.and(warp::path("buy_merchandise"))
|
||||
.and(warp::path::end())
|
||||
.and(warp::post())
|
||||
.and(warp::query::<MerchandiseParams>())
|
||||
|
@ -2,7 +2,6 @@ use anyhow::{Error, Result};
|
||||
use async_trait::async_trait;
|
||||
use chrono::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use sqlx::postgres::PgPool;
|
||||
use sqlx::types::Json;
|
||||
use tracing::instrument;
|
||||
@ -207,13 +206,16 @@ impl MerchandiseList {
|
||||
"UPDATE
|
||||
merchandise_lists
|
||||
SET
|
||||
form_list =
|
||||
jsonb_set(
|
||||
form_list = CASE
|
||||
WHEN quantity::int + $4 = 0
|
||||
THEN form_list - elem_index::int
|
||||
ELSE jsonb_set(
|
||||
form_list,
|
||||
array[elem_index::text, 'quantity'],
|
||||
to_jsonb(quantity::int + $4),
|
||||
true
|
||||
)
|
||||
END
|
||||
FROM (
|
||||
SELECT
|
||||
pos - 1 as elem_index,
|
||||
|
@ -73,6 +73,11 @@ pub fn from_anyhow(error: anyhow::Error) -> HttpApiProblem {
|
||||
StatusCode::BAD_REQUEST,
|
||||
)
|
||||
.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user