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. --- dichroism/src/repo/entities/product.rs | 3 ++- dichroism/src/repo/product_repo.rs | 32 +++++++++++++++++++------------- dichroism/src/schema.rs | 10 ++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) (limited to 'dichroism/src') 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