From dcc96d0b349583e5d6a0f25ae1f7a3ffa3769788 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Mon, 2 Nov 2020 20:36:18 -0500 Subject: swapped json payload url encoded images for multipart form data --- dichroism/src/repo/mod.rs | 109 +---------------------------------- dichroism/src/repo/photo_set_repo.rs | 42 ++++++++++++++ dichroism/src/repo/product_repo.rs | 78 +++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 106 deletions(-) create mode 100644 dichroism/src/repo/photo_set_repo.rs create mode 100644 dichroism/src/repo/product_repo.rs (limited to 'dichroism/src/repo') diff --git a/dichroism/src/repo/mod.rs b/dichroism/src/repo/mod.rs index 6dc245a..75a063c 100644 --- a/dichroism/src/repo/mod.rs +++ b/dichroism/src/repo/mod.rs @@ -1,110 +1,7 @@ -use crate::models; -use diesel::insert_into; -use diesel::prelude::*; -use diesel::result::Error; -use diesel::update; -use entities::*; +use diesel::SqliteConnection; pub mod entities; +pub mod photo_set_repo; +pub mod product_repo; type DBConn = SqliteConnection; - -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, - products::description, - products::quantity, - products::cents, - products::featured, - photo_sets::id, - photo_sets::original, - photo_sets::fullsize, - photo_sets::base, - photo_sets::thumbnail, - )); - Ok(query - .load::(conn)? - .into_iter() - .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) -} diff --git a/dichroism/src/repo/photo_set_repo.rs b/dichroism/src/repo/photo_set_repo.rs new file mode 100644 index 0000000..339ea91 --- /dev/null +++ b/dichroism/src/repo/photo_set_repo.rs @@ -0,0 +1,42 @@ +use super::entities::*; +use super::DBConn; +use crate::models; +use diesel::{insert_into, prelude::*, result::Error, update}; + +pub fn store(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_id(conn)?); + let form = PhotoSetForm::from(photo_set.clone()); + insert_into(photo_sets).values(&form).execute(conn)?; + } + Ok(photo_set) +} + +pub fn find(conn: &DBConn, dbid: i32) -> Result, Error> { + use crate::schema::photo_sets::dsl::*; + let query = photo_sets + .filter(id.eq(dbid)) + .select((id, original, fullsize, base, thumbnail)); + let photo_set = query.first::(conn).map(|p| p.into()); + match photo_set { + Ok(p) => Ok(Some(p)), + Err(e) => { + if e == Error::NotFound { + Ok(None) + } else { + Err(e) + } + } + } +} + +fn find_next_id(conn: &DBConn) -> Result { + use crate::schema::photo_sets::dsl::*; + Ok(photo_sets.select(id).order(id.desc()).first::(conn)? + 1) +} diff --git a/dichroism/src/repo/product_repo.rs b/dichroism/src/repo/product_repo.rs new file mode 100644 index 0000000..7b3aaac --- /dev/null +++ b/dichroism/src/repo/product_repo.rs @@ -0,0 +1,78 @@ +use super::entities::*; +use super::DBConn; +use crate::models; +use diesel::{insert_into, prelude::*, result::Error, update}; + +pub fn store(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_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, + products::description, + products::quantity, + products::cents, + products::featured, + photo_sets::id, + photo_sets::original, + photo_sets::fullsize, + photo_sets::base, + photo_sets::thumbnail, + )); + Ok(query + .load::(conn)? + .into_iter() + .map(|p| p.into()) + .collect::>()) +} + +pub fn find(conn: &DBConn, dbid: i32) -> Result, Error> { + use crate::schema::*; + let query = products::table + .inner_join(photo_sets::table) + .filter(products::id.eq(dbid)) + .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_id(conn: &DBConn) -> Result { + use crate::schema::products::dsl::*; + Ok(products.select(id).order(id.desc()).first::(conn)? + 1) +} -- cgit v1.2.3