diff options
Diffstat (limited to 'dichroism')
-rw-r--r-- | dichroism/migrations/2020-10-23-010929_products/up.sql | 4 | ||||
-rw-r--r-- | dichroism/migrations/2020-11-07-130734_categories/down.sql | 1 | ||||
-rw-r--r-- | dichroism/migrations/2020-11-07-130734_categories/up.sql | 5 | ||||
-rw-r--r-- | dichroism/src/repo/entities/product.rs | 3 | ||||
-rw-r--r-- | dichroism/src/repo/product_repo.rs | 32 | ||||
-rw-r--r-- | dichroism/src/schema.rs | 10 |
6 files changed, 40 insertions, 15 deletions
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<models::Product> for Product { @@ -25,7 +26,7 @@ impl Into<models::Product> 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<models::Prod pub fn find_all(conn: &DBConn) -> Result<Vec<models::Product>, 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::<Product>(conn)? .into_iter() @@ -45,6 +49,7 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::Product>, 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<Option<models::Product>, Error> photo_sets::fullsize, photo_sets::base, photo_sets::thumbnail, + categories::path, )); let product = query.first::<Product>(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,4 +1,11 @@ table! { + categories (id) { + id -> Integer, + path -> Text, + } +} + +table! { photo_sets (id) { id -> Integer, base -> Text, @@ -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, ); |