Add shelves list to interior_ref_lists

So that shops can have multiple shelves that save their page, filter, sort, etc.
state to the server. Buttons for the shelves are reconstructed in the plugin
during the load shop procedure.
This commit is contained in:
Tyler Hallada 2020-11-21 01:29:25 -05:00
parent 0adbf7c5c0
commit 81840b3d34
6 changed files with 66 additions and 15 deletions

View File

@ -137,10 +137,9 @@ PORT=3030
4. Install
[`sqlx_cli`](https://github.com/launchbadge/sqlx/tree/master/sqlx-cli) with
`cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgres`
5. `cd db` to enter the `db` sub-directory of this repo.
6. Run `sqlx migrate run` which will run all the database migrations.
7. `cd ..` to return to the top-level directory of this repo.
8. Run `./devserver.sh` to run the dev server (by default it listens at
5. Run `sqlx migrate --source db/migrations run` which will run all the database
migrations.
6. Run `./devserver.sh` to run the dev server (by default it listens at
`127.0.0.1:3030`). Note that this runs the server in debug mode and shouldn't
be used to serve requests from the mod. You can build the release version of
the server with `cargo build --release`.

View File

@ -22,6 +22,7 @@ CREATE TABLE "interior_ref_lists" (
"shop_id" INTEGER REFERENCES "shops"(id) NOT NULL UNIQUE,
"owner_id" INTEGER REFERENCES "owners"(id) NOT NULL,
"ref_list" jsonb NOT NULL,
"shelves" jsonb NOT NULL,
"created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL
);
@ -53,4 +54,4 @@ CREATE TABLE "transactions" (
);
CREATE INDEX "transactions_shop_id" ON "transactions" ("shop_id");
CREATE INDEX "transactions_owner_id" ON "transactions" ("owner_id");
CREATE INDEX "transactions_mod_name_and_local_form_id" ON "transactions" ("mod_name", "local_form_id");
CREATE INDEX "transactions_mod_name_and_local_form_id" ON "transactions" ("mod_name", "local_form_id");

View File

@ -3,4 +3,4 @@ DROP TABLE shops CASCADE;
DROP TABLE interior_ref_lists CASCADE;
DROP TABLE merchandise_lists CASCADE;
DROP TABLE transactions CASCADE;
DROP TABLE refinery_schema_history CASCADE;
DROP TABLE _sqlx_migrations CASCADE;

View File

@ -95,6 +95,7 @@ pub async fn create(
shop_id: saved_shop.id,
owner_id: Some(owner_id),
ref_list: sqlx::types::Json::default(),
shelves: sqlx::types::Json::default(),
};
InteriorRefList::create(interior_ref_list, &mut tx)
.await

View File

@ -24,12 +24,31 @@ pub struct InteriorRef {
pub scale: u16,
}
#[derive(sqlx::FromRow, Debug, Serialize, Deserialize, Clone)]
pub struct Shelf {
pub shelf_type: u32,
pub position_x: f32,
pub position_y: f32,
pub position_z: f32,
pub angle_x: f32,
pub angle_y: f32,
pub angle_z: f32,
pub scale: u16,
pub page: u32,
pub filter_form_type: Option<u32>,
pub filter_is_food: bool,
pub search: Option<String>,
pub sort_on: Option<String>,
pub sort_asc: bool,
}
#[derive(sqlx::FromRow, Debug, Serialize, Deserialize, Clone)]
pub struct InteriorRefList {
pub id: i32,
pub shop_id: i32,
pub owner_id: i32,
pub ref_list: Json<Vec<InteriorRef>>,
pub shelves: Json<Vec<Shelf>>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
@ -39,6 +58,7 @@ pub struct PostedInteriorRefList {
pub shop_id: i32,
pub owner_id: Option<i32>,
pub ref_list: Json<Vec<InteriorRef>>,
pub shelves: Json<Vec<Shelf>>,
}
impl InteriorRefList {
@ -60,7 +80,8 @@ impl InteriorRefList {
sqlx::query_as!(
Self,
r#"SELECT id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>"
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>"
FROM interior_ref_lists WHERE id = $1"#,
id
)
@ -77,13 +98,15 @@ impl InteriorRefList {
Ok(sqlx::query_as!(
Self,
r#"INSERT INTO interior_ref_lists
(shop_id, owner_id, ref_list, created_at, updated_at)
VALUES ($1, $2, $3, now(), now())
(shop_id, owner_id, ref_list, shelves, created_at, updated_at)
VALUES ($1, $2, $3, $4, now(), now())
RETURNING id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>""#,
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>""#,
interior_ref_list.shop_id,
interior_ref_list.owner_id,
serde_json::json!(interior_ref_list.ref_list),
serde_json::json!(interior_ref_list.shelves),
)
.fetch_one(db)
.await?)
@ -120,7 +143,8 @@ impl InteriorRefList {
sqlx::query_as!(
Self,
r#"SELECT id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>" FROM interior_ref_lists
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>" FROM interior_ref_lists
ORDER BY $1
LIMIT $2
OFFSET $3"#,
@ -134,7 +158,8 @@ impl InteriorRefList {
sqlx::query_as!(
Self,
r#"SELECT id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>" FROM interior_ref_lists
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>" FROM interior_ref_lists
LIMIT $1
OFFSET $2"#,
list_params.limit.unwrap_or(10),
@ -162,12 +187,15 @@ impl InteriorRefList {
Self,
r#"UPDATE interior_ref_lists SET
ref_list = $2,
shelves = $3,
updated_at = now()
WHERE id = $1
RETURNING id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>""#,
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>""#,
id,
serde_json::json!(interior_ref_list.ref_list),
serde_json::json!(interior_ref_list.shelves),
)
.fetch_one(db)
.await?)
@ -184,7 +212,8 @@ impl InteriorRefList {
sqlx::query_as!(
Self,
r#"SELECT id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>" FROM interior_ref_lists
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>" FROM interior_ref_lists
WHERE shop_id = $1"#,
shop_id,
)
@ -211,12 +240,15 @@ impl InteriorRefList {
Self,
r#"UPDATE interior_ref_lists SET
ref_list = $2,
shelves = $3,
updated_at = now()
WHERE shop_id = $1
RETURNING id, shop_id, owner_id, created_at, updated_at,
ref_list as "ref_list: Json<Vec<InteriorRef>>""#,
ref_list as "ref_list: Json<Vec<InteriorRef>>",
shelves as "shelves: Json<Vec<Shelf>>""#,
shop_id,
serde_json::json!(interior_ref_list.ref_list),
serde_json::json!(interior_ref_list.shelves),
)
.fetch_one(db)
.await?)

View File

@ -2602,5 +2602,23 @@
"angle_z": 1.006,
"scale": 1
}
],
"shelves": [
{
"shelf_type": 1,
"position_x": 1.001,
"position_y": 1.002,
"position_z": 1.003,
"angle_x": 1.004,
"angle_y": 1.005,
"angle_z": 1.006,
"scale": 1,
"page": 1,
"filter_form_type": null,
"filter_is_food": false,
"search": null,
"sort_on": null,
"sort_asc": true
}
]
}