summaryrefslogtreecommitdiff
path: root/dichroism/src/repo/mod.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-31 10:14:31 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-31 10:14:31 -0400
commit3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8 (patch)
tree3b712bc31a6db0a16ddfd4a02529d8b2497a5c54 /dichroism/src/repo/mod.rs
parent00c71f6baff136a88805ca2e3f9ef72453ac9f35 (diff)
downloadtheglassyladies-3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8.tar.xz
theglassyladies-3e1eadbbfdca1b2c0cb32ba4c8e1160a60e0ccb8.zip
All basic functionality implemented.
Diffstat (limited to 'dichroism/src/repo/mod.rs')
-rw-r--r--dichroism/src/repo/mod.rs88
1 files changed, 85 insertions, 3 deletions
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<Vec<models::Product>, Error> {
+pub fn store_photo_set(
+ conn: &DBConn,
+ mut photo_set: models::PhotoSet,
+) -> Result<models::PhotoSet, Error> {
+ 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<models::Product, Error> {
+ 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<Vec<models::Product>, 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<Vec<models::Product>, 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<Vec<models::Product>, Error> {
.map(|p| p.into())
.collect::<Vec<models::Product>>())
}
+
+pub fn find(conn: &DBConn, id: i32) -> Result<Option<models::Product>, 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::<Product>(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<i32, Error> {
+ use crate::schema::products::dsl::*;
+ Ok(products.select(id).order(id.desc()).first::<i32>(conn)? + 1)
+}
+
+fn find_next_photo_set_id(conn: &DBConn) -> Result<i32, Error> {
+ use crate::schema::photo_sets::dsl::*;
+ Ok(photo_sets.select(id).order(id.desc()).first::<i32>(conn)? + 1)
+}