From 3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 31 Oct 2020 10:14:31 -0400 Subject: All basic functionality implemented. --- dichroism/src/repo/entities/mod.rs | 43 +++---------- dichroism/src/repo/entities/photo_set.rs | 22 +++++++ dichroism/src/repo/entities/photo_set_form.rs | 24 ++++++++ dichroism/src/repo/entities/product.rs | 38 ++++++++++++ dichroism/src/repo/entities/product_form.rs | 28 +++++++++ dichroism/src/repo/mod.rs | 88 ++++++++++++++++++++++++++- 6 files changed, 205 insertions(+), 38 deletions(-) create mode 100644 dichroism/src/repo/entities/photo_set.rs create mode 100644 dichroism/src/repo/entities/photo_set_form.rs create mode 100644 dichroism/src/repo/entities/product.rs create mode 100644 dichroism/src/repo/entities/product_form.rs (limited to 'dichroism/src/repo') diff --git a/dichroism/src/repo/entities/mod.rs b/dichroism/src/repo/entities/mod.rs index 2cff899..288f1e3 100644 --- a/dichroism/src/repo/entities/mod.rs +++ b/dichroism/src/repo/entities/mod.rs @@ -1,36 +1,9 @@ -use crate::models; -use crate::schema::products; +mod photo_set; +mod photo_set_form; +mod product; +mod product_form; -#[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), - ), - } - } -} +pub use photo_set::*; +pub use photo_set_form::*; +pub use product::*; +pub use product_form::*; diff --git a/dichroism/src/repo/entities/photo_set.rs b/dichroism/src/repo/entities/photo_set.rs new file mode 100644 index 0000000..6e48e12 --- /dev/null +++ b/dichroism/src/repo/entities/photo_set.rs @@ -0,0 +1,22 @@ +use crate::models; + +#[derive(Debug, Clone, Queryable)] +pub struct PhotoSet { + pub id: i32, + pub original: String, + pub fullsize: String, + pub base: String, + pub thumbnail: String, +} + +impl Into for PhotoSet { + fn into(self) -> models::PhotoSet { + models::PhotoSet { + id: Some(self.id), + original: models::Photo::new(self.original), + fullsize: models::Photo::new(self.fullsize), + base: models::Photo::new(self.base), + thumbnail: models::Photo::new(self.thumbnail), + } + } +} diff --git a/dichroism/src/repo/entities/photo_set_form.rs b/dichroism/src/repo/entities/photo_set_form.rs new file mode 100644 index 0000000..611c8f0 --- /dev/null +++ b/dichroism/src/repo/entities/photo_set_form.rs @@ -0,0 +1,24 @@ +use crate::models::PhotoSet; +use crate::schema::photo_sets; + +#[derive(Insertable, AsChangeset)] +#[table_name = "photo_sets"] +pub struct PhotoSetForm { + pub id: i32, + pub original: String, + pub fullsize: String, + pub base: String, + pub thumbnail: String, +} + +impl From for PhotoSetForm { + fn from(p: PhotoSet) -> Self { + Self { + id: p.id.unwrap_or(-1), + original: p.original.id, + fullsize: p.fullsize.id, + base: p.base.id, + thumbnail: p.thumbnail.id, + } + } +} diff --git a/dichroism/src/repo/entities/product.rs b/dichroism/src/repo/entities/product.rs new file mode 100644 index 0000000..e6ba223 --- /dev/null +++ b/dichroism/src/repo/entities/product.rs @@ -0,0 +1,38 @@ +use crate::models; +use crate::schema::products; + +#[derive(Debug, Clone, Identifiable, Queryable)] +pub struct Product { + pub id: i32, + pub name: String, + pub description: String, + pub quantity: i32, + pub cents: i32, + pub featured: i32, + pub photo_set_id: 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: Some(self.id), + name: self.name, + description: self.description, + quantity: self.quantity, + cents: self.cents, + featured: self.featured != 0, // TODO: is this safe? + category: String::new(), // TODO: real category + photo_set: models::PhotoSet { + id: Some(self.photo_set_id), + original: models::Photo::new(self.original), + fullsize: models::Photo::new(self.fullsize), + base: models::Photo::new(self.base), + thumbnail: models::Photo::new(self.thumbnail), + }, + } + } +} diff --git a/dichroism/src/repo/entities/product_form.rs b/dichroism/src/repo/entities/product_form.rs new file mode 100644 index 0000000..0d6e452 --- /dev/null +++ b/dichroism/src/repo/entities/product_form.rs @@ -0,0 +1,28 @@ +use crate::models::*; +use crate::schema::products; + +#[derive(Insertable, AsChangeset)] +#[table_name = "products"] +pub struct ProductForm { + pub id: i32, + pub name: String, + pub description: String, + pub quantity: i32, + pub cents: i32, + pub featured: i32, + pub photo_set: i32, +} + +impl From for ProductForm { + fn from(p: Product) -> Self { + Self { + id: p.id.unwrap_or(-1), + name: p.name, + description: p.description, + quantity: p.quantity, + cents: p.cents, + featured: p.featured as i32, + photo_set: p.photo_set.id.unwrap_or(-1), // TODO: ? + } + } +} diff --git a/dichroism/src/repo/mod.rs b/dichroism/src/repo/mod.rs index 0df8aad..6dc245a 100644 --- a/dichroism/src/repo/mod.rs +++ b/dichroism/src/repo/mod.rs @@ -1,13 +1,53 @@ use crate::models; -use crate::schema::*; +use diesel::insert_into; use diesel::prelude::*; use diesel::result::Error; +use diesel::update; +use entities::*; -mod entities; +pub mod entities; type DBConn = SqliteConnection; -pub fn read_products(conn: &DBConn) -> Result, Error> { +pub fn store_photo_set( + conn: &DBConn, + mut photo_set: models::PhotoSet, +) -> Result { + use crate::schema::photo_sets::dsl::*; + if photo_set.id.is_some() { + // update + let form = PhotoSetForm::from(photo_set.clone()); + update(photo_sets).set(&form).execute(conn)?; + } else { + // insert + photo_set.id = Some(find_next_photo_set_id(conn)?); + let form = PhotoSetForm::from(photo_set.clone()); + insert_into(photo_sets).values(&form).execute(conn)?; + } + Ok(photo_set) +} + +pub fn store_product( + conn: &DBConn, + mut product: models::Product, +) -> Result { + use crate::schema::products::dsl::*; + if product.id.is_some() { + // update + let form = ProductForm::from(product.clone()); + update(products).set(&form).execute(conn)?; + } else { + // insert + product.id = Some(find_next_product_id(conn)?); + let form = ProductForm::from(product.clone()); + insert_into(products).values(&form).execute(conn)?; + } + + Ok(product) +} + +pub fn find_all(conn: &DBConn) -> Result, Error> { + use crate::schema::*; let query = products::table.inner_join(photo_sets::table).select(( products::id, products::name, @@ -15,6 +55,7 @@ pub fn read_products(conn: &DBConn) -> Result, Error> { products::quantity, products::cents, products::featured, + photo_sets::id, photo_sets::original, photo_sets::fullsize, photo_sets::base, @@ -26,3 +67,44 @@ pub fn read_products(conn: &DBConn) -> Result, Error> { .map(|p| p.into()) .collect::>()) } + +pub fn find(conn: &DBConn, id: i32) -> Result, Error> { + use crate::schema::*; + let query = products::table + .inner_join(photo_sets::table) + .filter(products::id.eq(id)) + .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 product = query.first::(conn).map(|p| p.into()); + match product { + Ok(p) => Ok(Some(p)), + Err(e) => { + if e == Error::NotFound { + Ok(None) + } else { + Err(e) + } + } + } +} + +fn find_next_product_id(conn: &DBConn) -> Result { + use crate::schema::products::dsl::*; + Ok(products.select(id).order(id.desc()).first::(conn)? + 1) +} + +fn find_next_photo_set_id(conn: &DBConn) -> Result { + use crate::schema::photo_sets::dsl::*; + Ok(photo_sets.select(id).order(id.desc()).first::(conn)? + 1) +} -- cgit v1.2.3