From 00c71f6baff136a88805ca2e3f9ef72453ac9f35 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Fri, 23 Oct 2020 08:10:13 -0400 Subject: moved entities under repo; photos now hold onto just the filename --- dichroism/src/config.rs | 2 +- dichroism/src/dtos/mod.rs | 6 +++--- dichroism/src/entities/mod.rs | 36 ------------------------------------ dichroism/src/handlers.rs | 4 ++-- dichroism/src/main.rs | 3 +-- dichroism/src/models/photo.rs | 15 +++++++++------ dichroism/src/product_repo.rs | 27 --------------------------- dichroism/src/repo/entities/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ dichroism/src/repo/mod.rs | 28 ++++++++++++++++++++++++++++ 9 files changed, 80 insertions(+), 77 deletions(-) delete mode 100644 dichroism/src/entities/mod.rs delete mode 100644 dichroism/src/product_repo.rs create mode 100644 dichroism/src/repo/entities/mod.rs create mode 100644 dichroism/src/repo/mod.rs (limited to 'dichroism') diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs index 0dbc10e..2031ecc 100644 --- a/dichroism/src/config.rs +++ b/dichroism/src/config.rs @@ -10,7 +10,7 @@ use toml::from_str; pub static CONFIG_INSTANCE: Lazy = Lazy::new(|| { Config::from_toml().unwrap_or_else(|e| { - eprintln!("Error: {}", e.to_string()); + eprintln!("Error parsing config: {}", e.to_string()); std::process::exit(1); }) }); diff --git a/dichroism/src/dtos/mod.rs b/dichroism/src/dtos/mod.rs index 76b04c7..d7e1ab5 100644 --- a/dichroism/src/dtos/mod.rs +++ b/dichroism/src/dtos/mod.rs @@ -62,9 +62,9 @@ impl From for Product { quantity: p.quantity, featured: p.featured, category: p.category, - photo_fullsize: p.photo_set.fullsize.path, - photo_base: p.photo_set.base.path, - photo_thumbnail: p.photo_set.thumbnail.path, + photo_fullsize: p.photo_set.fullsize.filename, + photo_base: p.photo_set.base.filename, + photo_thumbnail: p.photo_set.thumbnail.filename, } } } diff --git a/dichroism/src/entities/mod.rs b/dichroism/src/entities/mod.rs deleted file mode 100644 index a127ef1..0000000 --- a/dichroism/src/entities/mod.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::models; -use crate::schema::products; - -#[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/handlers.rs b/dichroism/src/handlers.rs index 23d2039..b4b117c 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -1,5 +1,5 @@ use crate::dtos::*; -use crate::product_repo; +use crate::repo; use crate::types::DbPool; use actix_web::{get, patch, post, web, Error, HttpResponse, Responder}; @@ -11,7 +11,7 @@ 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: Vec = web::block(move || product_repo::read_products(&conn)) + let products: Vec = web::block(move || repo::read_products(&conn)) .await .map_err(|e| { eprintln!("{}", e); diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 5ecd9a4..c255717 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -14,11 +14,10 @@ use result::Result; mod config; mod constants; mod dtos; -mod entities; mod error; mod handlers; mod models; -mod product_repo; +mod repo; mod result; mod schema; mod types; diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs index 6069085..8c1435e 100644 --- a/dichroism/src/models/photo.rs +++ b/dichroism/src/models/photo.rs @@ -7,12 +7,12 @@ use uuid::Uuid; #[derive(Debug, Queryable, Serialize, Clone)] pub struct Photo { - pub path: String, + pub filename: String, } impl Photo { - pub fn from_path(path: String) -> Self { - Self { path } + pub fn from_filename(filename: String) -> Self { + Self { filename } } pub fn from_image(image: &DynamicImage) -> Result { @@ -24,8 +24,11 @@ impl Photo { path.set_extension("jpg"); image.save(&path)?; - Ok(Self { - path: path.to_str().ok_or(DichroismError::ImageWrite)?.to_string(), - }) + let filename = path + .file_name() + .ok_or(DichroismError::ImageWrite)? + .to_str() + .ok_or(DichroismError::ImageWrite)?; + Ok(Self::from_filename(String::from(filename))) } } diff --git a/dichroism/src/product_repo.rs b/dichroism/src/product_repo.rs deleted file mode 100644 index 6f40b20..0000000 --- a/dichroism/src/product_repo.rs +++ /dev/null @@ -1,27 +0,0 @@ -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> { - 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/repo/entities/mod.rs b/dichroism/src/repo/entities/mod.rs new file mode 100644 index 0000000..2cff899 --- /dev/null +++ b/dichroism/src/repo/entities/mod.rs @@ -0,0 +1,36 @@ +use crate::models; +use crate::schema::products; + +#[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_filename(self.original), + models::Photo::from_filename(self.fullsize), + models::Photo::from_filename(self.base), + models::Photo::from_filename(self.thumbnail), + ), + } + } +} diff --git a/dichroism/src/repo/mod.rs b/dichroism/src/repo/mod.rs new file mode 100644 index 0000000..0df8aad --- /dev/null +++ b/dichroism/src/repo/mod.rs @@ -0,0 +1,28 @@ +use crate::models; +use crate::schema::*; +use diesel::prelude::*; +use diesel::result::Error; + +mod entities; + +type DBConn = SqliteConnection; + +pub fn read_products(conn: &DBConn) -> Result, Error> { + 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::>()) +} -- cgit v1.2.3