From 5cb0869b94fc8b9aff564055af9a60046de7b028 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Thu, 22 Oct 2020 22:09:46 -0400 Subject: Skipped separate photo table; working handler for get all products (lacking cats) --- dichroism/migrations/.gitkeep | 0 .../migrations/2020-10-09-210205_photos/down.sql | 1 - .../migrations/2020-10-09-210205_photos/up.sql | 5 -- .../2020-10-09-210247_photo_sets/down.sql | 1 - .../migrations/2020-10-09-210247_photo_sets/up.sql | 12 --- .../migrations/2020-10-21-221149_products/down.sql | 1 - .../migrations/2020-10-21-221149_products/up.sql | 11 --- .../2020-10-23-010629_photo_sets/down.sql | 1 + .../migrations/2020-10-23-010629_photo_sets/up.sql | 8 ++ .../migrations/2020-10-23-010929_products/down.sql | 1 + .../migrations/2020-10-23-010929_products/up.sql | 11 +++ dichroism/src/entities/mod.rs | 41 ++++++++-- dichroism/src/entities/photo.rs | 5 -- dichroism/src/entities/photo_set.rs | 8 -- dichroism/src/entities/product.rs | 11 --- dichroism/src/error.rs | 1 - dichroism/src/handlers.rs | 13 +-- dichroism/src/models/mod.rs | 6 -- dichroism/src/models/new_photo.rs | 6 -- dichroism/src/models/new_photo_data.rs | 95 ---------------------- dichroism/src/models/new_photo_set.rs | 8 -- dichroism/src/models/photo.rs | 4 + dichroism/src/models/photo_set.rs | 25 ++---- dichroism/src/product_repo.rs | 21 ++++- dichroism/src/schema.rs | 16 +--- 25 files changed, 98 insertions(+), 214 deletions(-) delete mode 100644 dichroism/migrations/.gitkeep delete mode 100644 dichroism/migrations/2020-10-09-210205_photos/down.sql delete mode 100644 dichroism/migrations/2020-10-09-210205_photos/up.sql delete mode 100644 dichroism/migrations/2020-10-09-210247_photo_sets/down.sql delete mode 100644 dichroism/migrations/2020-10-09-210247_photo_sets/up.sql delete mode 100644 dichroism/migrations/2020-10-21-221149_products/down.sql delete mode 100644 dichroism/migrations/2020-10-21-221149_products/up.sql create mode 100644 dichroism/migrations/2020-10-23-010629_photo_sets/down.sql create mode 100644 dichroism/migrations/2020-10-23-010629_photo_sets/up.sql create mode 100644 dichroism/migrations/2020-10-23-010929_products/down.sql create mode 100644 dichroism/migrations/2020-10-23-010929_products/up.sql delete mode 100644 dichroism/src/entities/photo.rs delete mode 100644 dichroism/src/entities/photo_set.rs delete mode 100644 dichroism/src/entities/product.rs delete mode 100644 dichroism/src/models/new_photo.rs delete mode 100644 dichroism/src/models/new_photo_data.rs delete mode 100644 dichroism/src/models/new_photo_set.rs (limited to 'dichroism') diff --git a/dichroism/migrations/.gitkeep b/dichroism/migrations/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/dichroism/migrations/2020-10-09-210205_photos/down.sql b/dichroism/migrations/2020-10-09-210205_photos/down.sql deleted file mode 100644 index 1f9850a..0000000 --- a/dichroism/migrations/2020-10-09-210205_photos/down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE "photos" diff --git a/dichroism/migrations/2020-10-09-210205_photos/up.sql b/dichroism/migrations/2020-10-09-210205_photos/up.sql deleted file mode 100644 index 0e74ff9..0000000 --- a/dichroism/migrations/2020-10-09-210205_photos/up.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE "photos" ( - "id" INTEGER NOT NULL, - "path" TEXT NOT NULL UNIQUE, - PRIMARY KEY("id") -) diff --git a/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql b/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql deleted file mode 100644 index d02736a..0000000 --- a/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE "photo_sets" diff --git a/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql b/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql deleted file mode 100644 index 98e6050..0000000 --- a/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE "photo_sets" ( - "id" INTEGER NOT NULL, - "base" INTEGER NOT NULL, - "fullsize" INTEGER NOT NULL, - "thumbnail" INTEGER NOT NULL, - "original" INTEGER NOT NULL, - FOREIGN KEY("thumbnail") REFERENCES "photos"("id"), - FOREIGN KEY("original") REFERENCES "photos"("id"), - FOREIGN KEY("base") REFERENCES "photos"("id"), - FOREIGN KEY("fullsize") REFERENCES "photos"("id"), - PRIMARY KEY("id") -) diff --git a/dichroism/migrations/2020-10-21-221149_products/down.sql b/dichroism/migrations/2020-10-21-221149_products/down.sql deleted file mode 100644 index f800c5c..0000000 --- a/dichroism/migrations/2020-10-21-221149_products/down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE "products" diff --git a/dichroism/migrations/2020-10-21-221149_products/up.sql b/dichroism/migrations/2020-10-21-221149_products/up.sql deleted file mode 100644 index 9e9c696..0000000 --- a/dichroism/migrations/2020-10-21-221149_products/up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE "products" ( - "id" INTEGER NOT NULL, - "photo_set" INTEGER NOT NULL, - "cents" INTEGER NOT NULL, - "quantity" INTEGER NOT NULL, - "name" TEXT NOT NULL, - "description" TEXT NOT NULL, - "featured" INTEGER NOT NULL, - PRIMARY KEY("id"), - FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id") -) diff --git a/dichroism/migrations/2020-10-23-010629_photo_sets/down.sql b/dichroism/migrations/2020-10-23-010629_photo_sets/down.sql new file mode 100644 index 0000000..d02736a --- /dev/null +++ b/dichroism/migrations/2020-10-23-010629_photo_sets/down.sql @@ -0,0 +1 @@ +DROP TABLE "photo_sets" diff --git a/dichroism/migrations/2020-10-23-010629_photo_sets/up.sql b/dichroism/migrations/2020-10-23-010629_photo_sets/up.sql new file mode 100644 index 0000000..ed10670 --- /dev/null +++ b/dichroism/migrations/2020-10-23-010629_photo_sets/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE "photo_sets" ( + "id" INTEGER NOT NULL, + "base" TEXT NOT NULL, + "fullsize" TEXT NOT NULL, + "thumbnail" TEXT NOT NULL, + "original" TEXT NOT NULL, + PRIMARY KEY("id") +) diff --git a/dichroism/migrations/2020-10-23-010929_products/down.sql b/dichroism/migrations/2020-10-23-010929_products/down.sql new file mode 100644 index 0000000..f800c5c --- /dev/null +++ b/dichroism/migrations/2020-10-23-010929_products/down.sql @@ -0,0 +1 @@ +DROP TABLE "products" diff --git a/dichroism/migrations/2020-10-23-010929_products/up.sql b/dichroism/migrations/2020-10-23-010929_products/up.sql new file mode 100644 index 0000000..9e9c696 --- /dev/null +++ b/dichroism/migrations/2020-10-23-010929_products/up.sql @@ -0,0 +1,11 @@ +CREATE TABLE "products" ( + "id" INTEGER NOT NULL, + "photo_set" INTEGER NOT NULL, + "cents" INTEGER NOT NULL, + "quantity" INTEGER NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT NOT NULL, + "featured" INTEGER NOT NULL, + PRIMARY KEY("id"), + FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id") +) diff --git a/dichroism/src/entities/mod.rs b/dichroism/src/entities/mod.rs index 0b17cf7..a127ef1 100644 --- a/dichroism/src/entities/mod.rs +++ b/dichroism/src/entities/mod.rs @@ -1,7 +1,36 @@ -mod photo; -mod photo_set; -mod product; +use crate::models; +use crate::schema::products; -pub use photo::Photo; -pub use photo_set::PhotoSet; -pub use product::Product; +#[derive(Debug, Clone, Identifiable, Queryable, Serialize)] +pub struct Product { + pub id: i32, + pub name: String, + pub description: String, + pub quantity: i32, + pub cents: i32, + pub featured: i32, + pub original: String, + pub fullsize: String, + pub base: String, + pub thumbnail: String, +} + +impl Into for Product { + fn into(self) -> models::Product { + models::Product { + id: self.id as u32, + name: self.name, + description: self.description, + quantity: self.quantity as u32, + cents: self.cents as u32, + featured: self.featured != 0, // TODO: is this safe? + category: String::new(), // TODO: real category + photo_set: models::PhotoSet::from_photos( + models::Photo::from_path(self.original), + models::Photo::from_path(self.fullsize), + models::Photo::from_path(self.base), + models::Photo::from_path(self.thumbnail), + ), + } + } +} diff --git a/dichroism/src/entities/photo.rs b/dichroism/src/entities/photo.rs deleted file mode 100644 index 34e9c28..0000000 --- a/dichroism/src/entities/photo.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[derive(Debug, Clone, Serialize)] -pub struct Photo { - pub id: i32, - pub path: String, -} diff --git a/dichroism/src/entities/photo_set.rs b/dichroism/src/entities/photo_set.rs deleted file mode 100644 index b9f7710..0000000 --- a/dichroism/src/entities/photo_set.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[derive(Debug, Clone, Serialize)] -pub struct PhotoSet { - pub id: i32, - pub base: i32, - pub fullsize: i32, - pub thumbnail: i32, - pub original: i32, -} diff --git a/dichroism/src/entities/product.rs b/dichroism/src/entities/product.rs deleted file mode 100644 index 4f47c1d..0000000 --- a/dichroism/src/entities/product.rs +++ /dev/null @@ -1,11 +0,0 @@ -#[derive(Debug, Clone, Queryable, Serialize)] -pub struct Product { - pub id: i32, - pub photo_set: i32, - pub cents: i32, - pub quantity: i32, - pub name: String, - pub description: String, - pub featured: i32, -} - diff --git a/dichroism/src/error.rs b/dichroism/src/error.rs index 534f070..62333d0 100644 --- a/dichroism/src/error.rs +++ b/dichroism/src/error.rs @@ -1,7 +1,6 @@ #[derive(Debug)] pub enum DichroismError { UriDataExtract, - InvalidPhotoRoot, ImageWrite, } diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs index beecb72..23d2039 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -11,15 +11,16 @@ async fn hello() -> impl Responder { #[get("/products")] async fn get_products(pool: web::Data) -> Result { let conn = pool.get().expect("Couldn't get DB connection from pool."); - let products = web::block(move || product_repo::read_products(&conn)) + let products: Vec = web::block(move || product_repo::read_products(&conn)) .await .map_err(|e| { eprintln!("{}", e); - HttpResponse::InternalServerError().finish() - })?; - //dbg!(&products); - //Ok(HttpResponse::Ok().json(products)) - todo!() + HttpResponse::InternalServerError().body(e.to_string()) + })? + .into_iter() + .map(|p| p.into()) + .collect(); + Ok(HttpResponse::Ok().json(products)) } #[patch("/products/{id}")] diff --git a/dichroism/src/models/mod.rs b/dichroism/src/models/mod.rs index e0fc3bd..529a9bc 100644 --- a/dichroism/src/models/mod.rs +++ b/dichroism/src/models/mod.rs @@ -1,13 +1,7 @@ -mod new_photo; -mod new_photo_data; -mod new_photo_set; mod photo; mod photo_set; mod product; -pub use new_photo::*; -pub use new_photo_data::*; -pub use new_photo_set::*; pub use photo::*; pub use photo_set::*; pub use product::*; diff --git a/dichroism/src/models/new_photo.rs b/dichroism/src/models/new_photo.rs deleted file mode 100644 index d348e95..0000000 --- a/dichroism/src/models/new_photo.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::schema::photos; -#[table_name = "photos"] -#[derive(Debug, Insertable)] -pub struct NewPhoto { - pub path: String, -} diff --git a/dichroism/src/models/new_photo_data.rs b/dichroism/src/models/new_photo_data.rs deleted file mode 100644 index 19a6557..0000000 --- a/dichroism/src/models/new_photo_data.rs +++ /dev/null @@ -1,95 +0,0 @@ -use super::NewPhoto; -use super::NewPhotoSet; -use crate::error::DichroismError; -use crate::result::Result; -use base64::decode; -use image::imageops::FilterType; -use image::DynamicImage; -use image::GenericImageView; -use regex::Regex; -use std::path::PathBuf; -use uuid::Uuid; - -use once_cell::sync::Lazy; -static DATA_URI_RE: Lazy = Lazy::new(|| { - Regex::new("^data:image/(png|jpeg);base64,(?P.+)") - .expect("Couldn't parse data URI Regex.") -}); - -pub struct NewPhotoSetData { - original: DynamicImage, // original, just for safe-keeping - fullsize: DynamicImage, // full-size, "zoomed" view - base: DynamicImage, // basic viewing - thumbnail: DynamicImage, // tiny, square thumbnail -} - -impl NewPhotoSetData { - pub fn from_data_uri(uri: &str) -> Result { - let data = DATA_URI_RE - .captures(uri) - .ok_or(DichroismError::UriDataExtract)? - .name("data") - .ok_or(DichroismError::UriDataExtract)? - .as_str(); - let original = image::load_from_memory(&decode(data)?)?; - let fullsize = original.resize(1000, 1000, FilterType::Lanczos3); - let base = original.resize(640, 640, FilterType::Lanczos3); - - let (width, height) = original.dimensions(); - let thumbnail = if width > height { - let offset = (width - height) / 2; - original.crop_imm(offset, 0, width - offset * 2, height) - } else { - let offset = (height - width) / 2; - original.crop_imm(0, offset, width, height - offset * 2) - } - .resize(300, 300, FilterType::Lanczos3); - - Ok(NewPhotoSetData { - original, - fullsize, - base, - thumbnail, - }) - } - - pub fn commit(self, prefix: &str) -> Result { - Ok(NewPhotoSet { - original: self.commit_single(prefix, &self.original)?, - fullsize: self.commit_single(prefix, &self.fullsize)?, - base: self.commit_single(prefix, &self.base)?, - thumbnail: self.commit_single(prefix, &self.thumbnail)?, - }) - } - - fn commit_single(&self, prefix: &str, image: &DynamicImage) -> Result { - let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) - .to_hyphenated() - .to_string(); - let mut path = PathBuf::new(); - path.push(prefix); - path.push(base_name); - path.set_extension("jpg"); - image.save(&path)?; - - Ok(NewPhoto { - path: path.to_str().ok_or(DichroismError::ImageWrite)?.to_string(), - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_DATA_URI: &str = include_str!("../unit_test_data/img_data_uri.txt"); - const TEST_DATA_BASE64: &str = include_str!("../unit_test_data/test_data_base64.txt"); - - #[test] - fn test_gen_product_images() { - NewPhotoSetData::from_data_uri(TEST_DATA_URI.trim()) - .unwrap() - .commit(".") - .unwrap(); - } -} diff --git a/dichroism/src/models/new_photo_set.rs b/dichroism/src/models/new_photo_set.rs deleted file mode 100644 index e907592..0000000 --- a/dichroism/src/models/new_photo_set.rs +++ /dev/null @@ -1,8 +0,0 @@ -use super::NewPhoto; - -pub struct NewPhotoSet { - pub original: NewPhoto, - pub fullsize: NewPhoto, - pub base: NewPhoto, - pub thumbnail: NewPhoto, -} diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs index dee6288..6069085 100644 --- a/dichroism/src/models/photo.rs +++ b/dichroism/src/models/photo.rs @@ -11,6 +11,10 @@ pub struct Photo { } impl Photo { + pub fn from_path(path: String) -> Self { + Self { path } + } + pub fn from_image(image: &DynamicImage) -> Result { let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) .to_hyphenated() diff --git a/dichroism/src/models/photo_set.rs b/dichroism/src/models/photo_set.rs index 84fe0e1..7187c65 100644 --- a/dichroism/src/models/photo_set.rs +++ b/dichroism/src/models/photo_set.rs @@ -21,6 +21,15 @@ pub struct PhotoSet { } impl PhotoSet { + pub fn from_photos(original: Photo, fullsize: Photo, base: Photo, thumbnail: Photo) -> Self { + Self { + original, + fullsize, + base, + thumbnail, + } + } + pub fn from_data_uri(uri: &str) -> Result { let data = DATA_URI_RE .captures(uri) @@ -50,19 +59,3 @@ impl PhotoSet { }) } } - -#[cfg(test)] -mod tests { - //use super::*; - - const TEST_DATA_URI: &str = include_str!("../unit_test_data/img_data_uri.txt"); - const TEST_DATA_BASE64: &str = include_str!("../unit_test_data/test_data_base64.txt"); - - #[test] - fn test_gen_product_images() { - // PhotoSet::from_data_uri(TEST_DATA_URI.trim()) - // .unwrap() - // .commit(".") - // .unwrap(); - } -} diff --git a/dichroism/src/product_repo.rs b/dichroism/src/product_repo.rs index 93176b2..6f40b20 100644 --- a/dichroism/src/product_repo.rs +++ b/dichroism/src/product_repo.rs @@ -1,12 +1,27 @@ use crate::entities; use crate::models; +use crate::schema::*; use diesel::prelude::*; use diesel::result::Error; type DBConn = SqliteConnection; pub fn read_products(conn: &DBConn) -> Result, Error> { - use crate::schema::products::dsl::*; - let results = products.load::(conn)?; - todo!() + let query = products::table.inner_join(photo_sets::table).select(( + products::id, + products::name, + products::description, + products::quantity, + products::cents, + products::featured, + photo_sets::original, + photo_sets::fullsize, + photo_sets::base, + photo_sets::thumbnail, + )); + Ok(query + .load::(conn)? + .into_iter() + .map(|p| p.into()) + .collect::>()) } diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs index 9eb92d8..fb59bf6 100644 --- a/dichroism/src/schema.rs +++ b/dichroism/src/schema.rs @@ -1,17 +1,10 @@ table! { photo_sets (id) { id -> Integer, - base -> Integer, - fullsize -> Integer, - thumbnail -> Integer, - original -> Integer, - } -} - -table! { - photos (id) { - id -> Integer, - path -> Text, + base -> Text, + fullsize -> Text, + thumbnail -> Text, + original -> Text, } } @@ -31,6 +24,5 @@ joinable!(products -> photo_sets (photo_set)); allow_tables_to_appear_in_same_query!( photo_sets, - photos, products, ); -- cgit v1.2.3