summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-22 22:09:46 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-22 22:09:46 -0400
commit5cb0869b94fc8b9aff564055af9a60046de7b028 (patch)
tree022cd909467d10747e9aa36d9455d47ea90b6c97
parent99dabd3f2f81ffcf0b6f2b59e13ebb4502b2ccac (diff)
downloadtheglassyladies-5cb0869b94fc8b9aff564055af9a60046de7b028.tar.xz
theglassyladies-5cb0869b94fc8b9aff564055af9a60046de7b028.zip
Skipped separate photo table; working handler for get all products
(lacking cats)
-rw-r--r--dichroism/migrations/.gitkeep0
-rw-r--r--dichroism/migrations/2020-10-09-210205_photos/down.sql1
-rw-r--r--dichroism/migrations/2020-10-09-210205_photos/up.sql5
-rw-r--r--dichroism/migrations/2020-10-09-210247_photo_sets/up.sql12
-rw-r--r--dichroism/migrations/2020-10-23-010629_photo_sets/down.sql (renamed from dichroism/migrations/2020-10-09-210247_photo_sets/down.sql)0
-rw-r--r--dichroism/migrations/2020-10-23-010629_photo_sets/up.sql8
-rw-r--r--dichroism/migrations/2020-10-23-010929_products/down.sql (renamed from dichroism/migrations/2020-10-21-221149_products/down.sql)0
-rw-r--r--dichroism/migrations/2020-10-23-010929_products/up.sql (renamed from dichroism/migrations/2020-10-21-221149_products/up.sql)0
-rw-r--r--dichroism/src/entities/mod.rs41
-rw-r--r--dichroism/src/entities/photo.rs5
-rw-r--r--dichroism/src/entities/photo_set.rs8
-rw-r--r--dichroism/src/entities/product.rs11
-rw-r--r--dichroism/src/error.rs1
-rw-r--r--dichroism/src/handlers.rs13
-rw-r--r--dichroism/src/models/mod.rs6
-rw-r--r--dichroism/src/models/new_photo.rs6
-rw-r--r--dichroism/src/models/new_photo_data.rs95
-rw-r--r--dichroism/src/models/new_photo_set.rs8
-rw-r--r--dichroism/src/models/photo.rs4
-rw-r--r--dichroism/src/models/photo_set.rs25
-rw-r--r--dichroism/src/product_repo.rs21
-rw-r--r--dichroism/src/schema.rs16
22 files changed, 85 insertions, 201 deletions
diff --git a/dichroism/migrations/.gitkeep b/dichroism/migrations/.gitkeep
deleted file mode 100644
index e69de29..0000000
--- a/dichroism/migrations/.gitkeep
+++ /dev/null
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/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-09-210247_photo_sets/down.sql b/dichroism/migrations/2020-10-23-010629_photo_sets/down.sql
index d02736a..d02736a 100644
--- a/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql
+++ b/dichroism/migrations/2020-10-23-010629_photo_sets/down.sql
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-21-221149_products/down.sql b/dichroism/migrations/2020-10-23-010929_products/down.sql
index f800c5c..f800c5c 100644
--- a/dichroism/migrations/2020-10-21-221149_products/down.sql
+++ b/dichroism/migrations/2020-10-23-010929_products/down.sql
diff --git a/dichroism/migrations/2020-10-21-221149_products/up.sql b/dichroism/migrations/2020-10-23-010929_products/up.sql
index 9e9c696..9e9c696 100644
--- a/dichroism/migrations/2020-10-21-221149_products/up.sql
+++ b/dichroism/migrations/2020-10-23-010929_products/up.sql
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<models::Product> 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<DbPool>) -> Result<HttpResponse, Error> {
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<Product> = 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<Regex> = Lazy::new(|| {
- Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)")
- .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<Self> {
- 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<NewPhotoSet> {
- 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<NewPhoto> {
- 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<Self> {
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<Self> {
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<Vec<models::Product>, Error> {
- use crate::schema::products::dsl::*;
- let results = products.load::<entities::Product>(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::<entities::Product>(conn)?
+ .into_iter()
+ .map(|p| p.into())
+ .collect::<Vec<models::Product>>())
}
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,
);