summaryrefslogtreecommitdiff
path: root/dichroism
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-11-11 20:07:12 -0500
committerAdam T. Carpenter <atc@53hor.net>2020-11-11 20:07:12 -0500
commit7381a7033231e6454a37fd64b1f3de4e8d59355f (patch)
tree7ae54976c2d036e0fe7cb199a7f6facdd09bbb6d /dichroism
parentc5280144de096c274f185fade287ccd63b954ceb (diff)
downloadtheglassyladies-7381a7033231e6454a37fd64b1f3de4e8d59355f.tar.xz
theglassyladies-7381a7033231e6454a37fd64b1f3de4e8d59355f.zip
Kind of flailing with the UI; lots of API bugfixes though.
Diffstat (limited to 'dichroism')
-rw-r--r--dichroism/migrations/2020-10-23-010929_products/up.sql5
-rw-r--r--dichroism/migrations/2020-11-07-130734_categories/down.sql1
-rw-r--r--dichroism/migrations/2020-11-07-130734_categories/up.sql5
-rw-r--r--dichroism/src/handlers.rs6
-rw-r--r--dichroism/src/repo/entities/product.rs4
-rw-r--r--dichroism/src/repo/entities/product_form.rs6
-rw-r--r--dichroism/src/repo/photo_set_repo.rs26
-rw-r--r--dichroism/src/repo/product_repo.rs60
-rw-r--r--dichroism/src/schema.rs11
9 files changed, 53 insertions, 71 deletions
diff --git a/dichroism/migrations/2020-10-23-010929_products/up.sql b/dichroism/migrations/2020-10-23-010929_products/up.sql
index 72a7750..36e3a82 100644
--- a/dichroism/migrations/2020-10-23-010929_products/up.sql
+++ b/dichroism/migrations/2020-10-23-010929_products/up.sql
@@ -6,8 +6,7 @@ CREATE TABLE "products" (
"name" TEXT NOT NULL,
"description" TEXT NOT NULL,
"featured" INTEGER NOT NULL,
- "category" INTEGER NOT NULL,
+ "category" TEXT NOT NULL,
PRIMARY KEY("id"),
- FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id"),
- FOREIGN KEY("category") REFERENCES "categories"("id")
+ FOREIGN KEY("photo_set") REFERENCES "photo_sets"("id")
)
diff --git a/dichroism/migrations/2020-11-07-130734_categories/down.sql b/dichroism/migrations/2020-11-07-130734_categories/down.sql
deleted file mode 100644
index 68ea5a7..0000000
--- a/dichroism/migrations/2020-11-07-130734_categories/down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE "categories"
diff --git a/dichroism/migrations/2020-11-07-130734_categories/up.sql b/dichroism/migrations/2020-11-07-130734_categories/up.sql
deleted file mode 100644
index 1bdd50d..0000000
--- a/dichroism/migrations/2020-11-07-130734_categories/up.sql
+++ /dev/null
@@ -1,5 +0,0 @@
-CREATE TABLE "categories" (
- "id" INTEGER NOT NULL,
- "path" TEXT NOT NULL UNIQUE,
- PRIMARY KEY("id")
-)
diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs
index ccaf347..8dd0f3b 100644
--- a/dichroism/src/handlers.rs
+++ b/dichroism/src/handlers.rs
@@ -8,7 +8,6 @@ use actix_web::{get, patch, post, web, Error, HttpResponse, Responder};
use futures::{StreamExt, TryStreamExt};
fn to_internal_error(e: impl std::error::Error) -> HttpResponse {
- eprintln!("{}", e);
HttpResponse::InternalServerError().body(e.to_string())
}
@@ -120,10 +119,7 @@ async fn post_photo(
// create new photo_set
let photo_set = web::block(move || image_service::generate_photo_set(&data))
.await
- .map_err(|e| {
- eprintln!("{}", e.to_string());
- HttpResponse::InternalServerError().body(e.to_string())
- })?;
+ .map_err(to_internal_error)?;
let conn = pool.get().map_err(to_internal_error)?;
let photo_set = web::block(move || photo_set_repo::store(&conn, photo_set))
.await
diff --git a/dichroism/src/repo/entities/product.rs b/dichroism/src/repo/entities/product.rs
index 81f3d9e..c89495a 100644
--- a/dichroism/src/repo/entities/product.rs
+++ b/dichroism/src/repo/entities/product.rs
@@ -9,12 +9,12 @@ pub struct Product {
pub quantity: i32,
pub cents: i32,
pub featured: i32,
+ pub category: String,
pub photo_set_id: i32,
pub original: String,
pub fullsize: String,
pub base: String,
pub thumbnail: String,
- pub category: String,
}
impl Into<models::Product> for Product {
@@ -25,7 +25,7 @@ impl Into<models::Product> for Product {
description: self.description,
quantity: self.quantity,
cents: self.cents,
- featured: self.featured != 0, // TODO: is this safe?
+ featured: self.featured != 0,
category: self.category,
photo_set: models::PhotoSet {
id: Some(self.photo_set_id),
diff --git a/dichroism/src/repo/entities/product_form.rs b/dichroism/src/repo/entities/product_form.rs
index 0d6e452..b3447eb 100644
--- a/dichroism/src/repo/entities/product_form.rs
+++ b/dichroism/src/repo/entities/product_form.rs
@@ -1,7 +1,7 @@
use crate::models::*;
use crate::schema::products;
-#[derive(Insertable, AsChangeset)]
+#[derive(Debug, Insertable, AsChangeset)]
#[table_name = "products"]
pub struct ProductForm {
pub id: i32,
@@ -11,6 +11,7 @@ pub struct ProductForm {
pub cents: i32,
pub featured: i32,
pub photo_set: i32,
+ pub category: String,
}
impl From<Product> for ProductForm {
@@ -22,7 +23,8 @@ impl From<Product> for ProductForm {
quantity: p.quantity,
cents: p.cents,
featured: p.featured as i32,
- photo_set: p.photo_set.id.unwrap_or(-1), // TODO: ?
+ photo_set: p.photo_set.id.unwrap_or(-1),
+ category: p.category,
}
}
}
diff --git a/dichroism/src/repo/photo_set_repo.rs b/dichroism/src/repo/photo_set_repo.rs
index 339ea91..56ca962 100644
--- a/dichroism/src/repo/photo_set_repo.rs
+++ b/dichroism/src/repo/photo_set_repo.rs
@@ -5,13 +5,15 @@ use diesel::{insert_into, prelude::*, result::Error, update};
pub fn store(conn: &DBConn, mut photo_set: models::PhotoSet) -> Result<models::PhotoSet, Error> {
use crate::schema::photo_sets::dsl::*;
- if photo_set.id.is_some() {
+ if let Some(photo_set_id) = photo_set.id {
// update
let form = PhotoSetForm::from(photo_set.clone());
- update(photo_sets).set(&form).execute(conn)?;
+ update(photo_sets.filter(id.eq(photo_set_id)))
+ .set(&form)
+ .execute(conn)?;
} else {
// insert
- photo_set.id = Some(find_next_id(conn)?);
+ photo_set.id = Some(find_next_id(conn));
let form = PhotoSetForm::from(photo_set.clone());
insert_into(photo_sets).values(&form).execute(conn)?;
}
@@ -26,17 +28,17 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::PhotoSet>, Error>
let photo_set = query.first::<PhotoSet>(conn).map(|p| p.into());
match photo_set {
Ok(p) => Ok(Some(p)),
- Err(e) => {
- if e == Error::NotFound {
- Ok(None)
- } else {
- Err(e)
- }
- }
+ Err(Error::NotFound) => Ok(None),
+ Err(e) => Err(e),
}
}
-fn find_next_id(conn: &DBConn) -> Result<i32, Error> {
+fn find_next_id(conn: &DBConn) -> i32 {
use crate::schema::photo_sets::dsl::*;
- Ok(photo_sets.select(id).order(id.desc()).first::<i32>(conn)? + 1)
+ let last_id = photo_sets
+ .select(id)
+ .order(id.desc())
+ .first::<i32>(conn)
+ .unwrap_or(0);
+ last_id + 1
}
diff --git a/dichroism/src/repo/product_repo.rs b/dichroism/src/repo/product_repo.rs
index d7dff65..487f96e 100644
--- a/dichroism/src/repo/product_repo.rs
+++ b/dichroism/src/repo/product_repo.rs
@@ -5,13 +5,15 @@ use diesel::{insert_into, prelude::*, result::Error, update};
pub fn store(conn: &DBConn, mut product: models::Product) -> Result<models::Product, Error> {
use crate::schema::products::dsl::*;
- if product.id.is_some() {
+ if let Some(product_id) = product.id {
// update
let form = ProductForm::from(product.clone());
- update(products).set(&form).execute(conn)?;
+ update(products.filter(id.eq(product_id)))
+ .set(&form)
+ .execute(conn)?;
} else {
// insert
- product.id = Some(find_next_id(conn)?);
+ product.id = Some(find_next_id(conn));
let form = ProductForm::from(product.clone());
insert_into(products).values(&form).execute(conn)?;
}
@@ -21,23 +23,20 @@ pub fn store(conn: &DBConn, mut product: models::Product) -> Result<models::Prod
pub fn find_all(conn: &DBConn) -> Result<Vec<models::Product>, Error> {
use crate::schema::*;
- let query = products::table
- .inner_join(photo_sets::table)
- .inner_join(categories::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,
- categories::path,
- ));
+ let query = products::table.inner_join(photo_sets::table).select((
+ products::id,
+ products::name,
+ products::description,
+ products::quantity,
+ products::cents,
+ products::featured,
+ products::category,
+ photo_sets::id,
+ photo_sets::original,
+ photo_sets::fullsize,
+ photo_sets::base,
+ photo_sets::thumbnail,
+ ));
Ok(query
.load::<Product>(conn)?
.into_iter()
@@ -49,7 +48,6 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::Product>, Error>
use crate::schema::*;
let query = products::table
.inner_join(photo_sets::table)
- .inner_join(categories::table)
.filter(products::id.eq(dbid))
.select((
products::id,
@@ -58,27 +56,27 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::Product>, Error>
products::quantity,
products::cents,
products::featured,
+ products::category,
photo_sets::id,
photo_sets::original,
photo_sets::fullsize,
photo_sets::base,
photo_sets::thumbnail,
- categories::path,
));
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)
- }
- }
+ Err(Error::NotFound) => Ok(None),
+ Err(e) => Err(e),
}
}
-fn find_next_id(conn: &DBConn) -> Result<i32, Error> {
+fn find_next_id(conn: &DBConn) -> i32 {
use crate::schema::products::dsl::*;
- Ok(products.select(id).order(id.desc()).first::<i32>(conn)? + 1)
+ let last_id = products
+ .select(id)
+ .order(id.desc())
+ .first::<i32>(conn)
+ .unwrap_or(0);
+ last_id + 1
}
diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs
index 45501cf..2d9e14a 100644
--- a/dichroism/src/schema.rs
+++ b/dichroism/src/schema.rs
@@ -1,11 +1,4 @@
table! {
- categories (id) {
- id -> Integer,
- path -> Text,
- }
-}
-
-table! {
photo_sets (id) {
id -> Integer,
base -> Text,
@@ -24,15 +17,13 @@ table! {
name -> Text,
description -> Text,
featured -> Integer,
- category -> Integer,
+ category -> Text,
}
}
-joinable!(products -> categories (category));
joinable!(products -> photo_sets (photo_set));
allow_tables_to_appear_in_same_query!(
- categories,
photo_sets,
products,
);