From 2c408648c70614a88c0412faf6d3a00d147379a2 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 7 Nov 2020 08:15:37 -0500 Subject: Added category to products. --- .../migrations/2020-10-23-010929_products/up.sql | 4 ++- .../2020-11-07-130734_categories/down.sql | 1 + .../migrations/2020-11-07-130734_categories/up.sql | 5 ++++ dichroism/src/repo/entities/product.rs | 3 +- dichroism/src/repo/product_repo.rs | 32 +++++++++++++--------- dichroism/src/schema.rs | 10 +++++++ 6 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 dichroism/migrations/2020-11-07-130734_categories/down.sql create mode 100644 dichroism/migrations/2020-11-07-130734_categories/up.sql (limited to 'dichroism') diff --git a/dichroism/migrations/2020-10-23-010929_products/up.sql b/dichroism/migrations/2020-10-23-010929_products/up.sql index 9e9c696..72a7750 100644 --- a/dichroism/migrations/2020-10-23-010929_products/up.sql +++ b/dichroism/migrations/2020-10-23-010929_products/up.sql @@ -6,6 +6,8 @@ CREATE TABLE "products" ( "name" TEXT NOT NULL, "description" TEXT NOT NULL, "featured" INTEGER NOT NULL, + "category" INTEGER NOT NULL, PRIMARY KEY("id"), - FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id") + FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id"), + FOREIGN KEY("category") REFERENCES "categories"("id") ) diff --git a/dichroism/migrations/2020-11-07-130734_categories/down.sql b/dichroism/migrations/2020-11-07-130734_categories/down.sql new file mode 100644 index 0000000..68ea5a7 --- /dev/null +++ b/dichroism/migrations/2020-11-07-130734_categories/down.sql @@ -0,0 +1 @@ +DROP TABLE "categories" diff --git a/dichroism/migrations/2020-11-07-130734_categories/up.sql b/dichroism/migrations/2020-11-07-130734_categories/up.sql new file mode 100644 index 0000000..1bdd50d --- /dev/null +++ b/dichroism/migrations/2020-11-07-130734_categories/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE "categories" ( + "id" INTEGER NOT NULL, + "path" TEXT NOT NULL UNIQUE, + PRIMARY KEY("id") +) diff --git a/dichroism/src/repo/entities/product.rs b/dichroism/src/repo/entities/product.rs index e6ba223..81f3d9e 100644 --- a/dichroism/src/repo/entities/product.rs +++ b/dichroism/src/repo/entities/product.rs @@ -14,6 +14,7 @@ pub struct Product { pub fullsize: String, pub base: String, pub thumbnail: String, + pub category: String, } impl Into for Product { @@ -25,7 +26,7 @@ impl Into for Product { quantity: self.quantity, cents: self.cents, featured: self.featured != 0, // TODO: is this safe? - category: String::new(), // TODO: real category + category: self.category, photo_set: models::PhotoSet { id: Some(self.photo_set_id), original: models::Photo::new(self.original), diff --git a/dichroism/src/repo/product_repo.rs b/dichroism/src/repo/product_repo.rs index 7b3aaac..d7dff65 100644 --- a/dichroism/src/repo/product_repo.rs +++ b/dichroism/src/repo/product_repo.rs @@ -21,19 +21,23 @@ pub fn store(conn: &DBConn, mut product: models::Product) -> Result Result, Error> { use crate::schema::*; - let query = products::table.inner_join(photo_sets::table).select(( - products::id, - products::name, - products::description, - products::quantity, - products::cents, - products::featured, - photo_sets::id, - photo_sets::original, - photo_sets::fullsize, - photo_sets::base, - photo_sets::thumbnail, - )); + let query = products::table + .inner_join(photo_sets::table) + .inner_join(categories::table) + .select(( + products::id, + products::name, + products::description, + products::quantity, + products::cents, + products::featured, + photo_sets::id, + photo_sets::original, + photo_sets::fullsize, + photo_sets::base, + photo_sets::thumbnail, + categories::path, + )); Ok(query .load::(conn)? .into_iter() @@ -45,6 +49,7 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result, Error> use crate::schema::*; let query = products::table .inner_join(photo_sets::table) + .inner_join(categories::table) .filter(products::id.eq(dbid)) .select(( products::id, @@ -58,6 +63,7 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result, Error> photo_sets::fullsize, photo_sets::base, photo_sets::thumbnail, + categories::path, )); let product = query.first::(conn).map(|p| p.into()); match product { diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs index fb59bf6..45501cf 100644 --- a/dichroism/src/schema.rs +++ b/dichroism/src/schema.rs @@ -1,3 +1,10 @@ +table! { + categories (id) { + id -> Integer, + path -> Text, + } +} + table! { photo_sets (id) { id -> Integer, @@ -17,12 +24,15 @@ table! { name -> Text, description -> Text, featured -> Integer, + category -> Integer, } } +joinable!(products -> categories (category)); joinable!(products -> photo_sets (photo_set)); allow_tables_to_appear_in_same_query!( + categories, photo_sets, products, ); -- cgit v1.2.3