summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-20 09:50:08 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-20 09:50:08 -0400
commit76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e (patch)
treedfc108120f067880b708a64ef6567879f4bb3284
parentd3a28fde46bb06f084c74904fa8849b40e5f8c87 (diff)
downloadtheglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.tar.xz
theglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.zip
back to basics -- working on the most-required functionality first, like handlers
-rw-r--r--dichroism/migrations/2020-10-04-235458_create_images/down.sql1
-rw-r--r--dichroism/migrations/2020-10-09-210205_photos/down.sql1
-rw-r--r--dichroism/migrations/2020-10-09-210205_photos/up.sql (renamed from dichroism/migrations/2020-10-04-235458_create_images/up.sql)2
-rw-r--r--dichroism/migrations/2020-10-09-210247_photo_sets/down.sql1
-rw-r--r--dichroism/migrations/2020-10-09-210247_photo_sets/up.sql12
-rw-r--r--dichroism/src/error.rs3
-rw-r--r--dichroism/src/handlers.rs79
-rw-r--r--dichroism/src/image_repo.rs57
-rw-r--r--dichroism/src/main.rs8
-rw-r--r--dichroism/src/models.rs12
-rw-r--r--dichroism/src/models/mod.rs13
-rw-r--r--dichroism/src/models/new_photo.rs6
-rw-r--r--dichroism/src/models/new_photo_data.rs (renamed from dichroism/src/image_api.rs)33
-rw-r--r--dichroism/src/models/new_photo_set.rs8
-rw-r--r--dichroism/src/models/photo.rs5
-rw-r--r--dichroism/src/models/photo_set.rs8
-rw-r--r--dichroism/src/models/product.rs26
-rw-r--r--dichroism/src/photo_repo.rs41
-rw-r--r--dichroism/src/product_api.rs0
-rw-r--r--dichroism/src/product_repo.rs19
-rw-r--r--dichroism/src/schema.rs17
21 files changed, 209 insertions, 143 deletions
diff --git a/dichroism/migrations/2020-10-04-235458_create_images/down.sql b/dichroism/migrations/2020-10-04-235458_create_images/down.sql
deleted file mode 100644
index bc3690f..0000000
--- a/dichroism/migrations/2020-10-04-235458_create_images/down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE "images"
diff --git a/dichroism/migrations/2020-10-09-210205_photos/down.sql b/dichroism/migrations/2020-10-09-210205_photos/down.sql
new file mode 100644
index 0000000..1f9850a
--- /dev/null
+++ b/dichroism/migrations/2020-10-09-210205_photos/down.sql
@@ -0,0 +1 @@
+DROP TABLE "photos"
diff --git a/dichroism/migrations/2020-10-04-235458_create_images/up.sql b/dichroism/migrations/2020-10-09-210205_photos/up.sql
index ba3b4c6..0e74ff9 100644
--- a/dichroism/migrations/2020-10-04-235458_create_images/up.sql
+++ b/dichroism/migrations/2020-10-09-210205_photos/up.sql
@@ -1,4 +1,4 @@
-CREATE TABLE "images" (
+CREATE TABLE "photos" (
"id" INTEGER NOT NULL,
"path" TEXT NOT NULL UNIQUE,
PRIMARY KEY("id")
diff --git a/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql b/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql
new file mode 100644
index 0000000..d02736a
--- /dev/null
+++ b/dichroism/migrations/2020-10-09-210247_photo_sets/down.sql
@@ -0,0 +1 @@
+DROP TABLE "photo_sets"
diff --git a/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql b/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql
new file mode 100644
index 0000000..98e6050
--- /dev/null
+++ b/dichroism/migrations/2020-10-09-210247_photo_sets/up.sql
@@ -0,0 +1,12 @@
+CREATE TABLE "photo_sets" (
+ "id" INTEGER NOT NULL,
+ "base" INTEGER NOT NULL,
+ "fullsize" INTEGER NOT NULL,
+ "thumbnail" INTEGER NOT NULL,
+ "original" INTEGER NOT NULL,
+ FOREIGN KEY("thumbnail") REFERENCES "photos"("id"),
+ FOREIGN KEY("original") REFERENCES "photos"("id"),
+ FOREIGN KEY("base") REFERENCES "photos"("id"),
+ FOREIGN KEY("fullsize") REFERENCES "photos"("id"),
+ PRIMARY KEY("id")
+)
diff --git a/dichroism/src/error.rs b/dichroism/src/error.rs
index 43933d4..534f070 100644
--- a/dichroism/src/error.rs
+++ b/dichroism/src/error.rs
@@ -1,7 +1,7 @@
#[derive(Debug)]
pub enum DichroismError {
UriDataExtract,
- InvalidImageRoot,
+ InvalidPhotoRoot,
ImageWrite,
}
@@ -12,4 +12,3 @@ impl std::fmt::Display for DichroismError {
write!(f, "{}", self)
}
}
-
diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs
index f8f10d3..1f657c2 100644
--- a/dichroism/src/handlers.rs
+++ b/dichroism/src/handlers.rs
@@ -1,53 +1,54 @@
-use super::image_repo;
-use super::product_repo;
+//use super::product_repo;
use super::types::DbPool;
-use crate::config::Config;
-use crate::image_api;
-use actix_web::{get, post, web, Error, HttpResponse, Responder};
+//use crate::config::Config;
+use actix_web::{get, patch, post, web, Error, HttpResponse, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hey, this is an API!")
}
-#[get("/images")]
-async fn get_images(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
- let conn = pool.get().expect("Couldn't get DB connection from pool.");
- let images = web::block(move || image_repo::read_images(&conn))
- .await
- .map_err(|e| {
- eprintln!("{}", e);
- HttpResponse::InternalServerError().finish()
- })?;
- dbg!(&images);
- Ok(HttpResponse::Ok().json(images))
+#[get("/products")]
+async fn get_products(_pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
+ dbg!("got products");
+ Ok(HttpResponse::Ok().finish())
+ //let conn = pool.get().expect("Couldn't get DB connection from pool.");
+ //let products = web::block(move || product_repo::read_products(&conn))
+ // .await
+ // .map_err(|e| {
+ // eprintln!("{}", e);
+ // HttpResponse::InternalServerError().finish()
+ // })?;
+ //Ok(HttpResponse::Ok().json(products))
}
-#[post("/images")]
-async fn create_image(_config: web::Data<Config>, req_body: String) -> impl Responder {
- // let data = match image_api::extract_data(&req_body) {
- // Err(e) => return HttpResponse::BadRequest().body(format!("fail: {}", e.to_string())),
- // Ok(d) => d,
- // };
+#[patch("/products/{id}")]
+async fn update_product(
+ _pool: web::Data<DbPool>,
+ id: web::Path<u32>,
+ updated_product: web::Json<UpdatedProduct>,
+) -> Result<HttpResponse, Error> {
+ dbg!(id, updated_product);
+ Ok(HttpResponse::Ok().finish())
+}
- // if let Err(e) = image_api::generate_images(data) {
- // return HttpResponse::BadRequest().body(format!(
- // "Unable to extract image from data URI: {}",
- // e.to_string()
- // ));
- // }
+#[post("/products")]
+async fn create_product(
+ _pool: web::Data<DbPool>,
+ new_product: web::Json<NewProduct>,
+) -> Result<HttpResponse, Error> {
+ dbg!(new_product);
+ Ok(HttpResponse::Ok().finish())
+}
- HttpResponse::Ok().body("Image created.")
+#[derive(Debug, Deserialize)]
+pub struct NewProduct {
+ pub name: String,
+ pub description: String,
}
-#[get("/products")]
-async fn get_products(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
- let conn = pool.get().expect("Couldn't get DB connection from pool.");
- let products = web::block(move || product_repo::read_products(&conn))
- .await
- .map_err(|e| {
- eprintln!("{}", e);
- HttpResponse::InternalServerError().finish()
- })?;
- Ok(HttpResponse::Ok().json(products))
+#[derive(Debug, Deserialize)]
+pub struct UpdatedProduct {
+ pub name: Option<String>,
+ pub description: Option<String>,
}
diff --git a/dichroism/src/image_repo.rs b/dichroism/src/image_repo.rs
deleted file mode 100644
index e0f420b..0000000
--- a/dichroism/src/image_repo.rs
+++ /dev/null
@@ -1,57 +0,0 @@
-use super::models::{NewProductImg, ProductImg};
-use diesel::prelude::*;
-use diesel::result::Error;
-
-type DBConn = SqliteConnection;
-
-pub fn read_images(conn: &DBConn) -> Result<Vec<ProductImg>, Error> {
- use crate::schema::images::dsl::*;
- let results = images.load::<ProductImg>(conn)?;
- Ok(results)
-}
-
-fn read_images_by_path(conn: &DBConn, path: &str) -> Result<Vec<ProductImg>, Error> {
- use crate::schema::images::dsl::*;
- let results = images.filter(path.eq(path)).load::<ProductImg>(conn)?;
- Ok(results)
-}
-
-pub fn read_image_by_path(conn: &DBConn, path: &str) -> Result<Option<ProductImg>, Error> {
- use crate::schema::images::dsl::*;
- let results = images
- .filter(path.eq(path))
- .limit(1)
- .load::<ProductImg>(conn)?;
- Ok(results.first().cloned())
-}
-
-pub fn read_image_by_id(conn: &DBConn, id: i32) -> Result<Option<ProductImg>, Error> {
- use crate::schema::images::dsl::*;
- let results = images.filter(id.eq(id)).limit(1).load::<ProductImg>(conn)?;
- Ok(results.first().cloned())
-}
-
-pub fn create_image(conn: &DBConn, new_image: NewProductImg) -> Result<Option<ProductImg>, Error> {
- use super::schema::images;
- diesel::insert_into(images::table)
- .values(&new_image)
- .execute(conn)?;
- read_image_by_path(conn, &new_image.path)
-}
-
-pub fn update_image() {
- todo!()
-}
-
-pub fn delete_image() {
- todo!()
-}
-
-#[cfg(test)]
-mod tests {
-
- #[test]
- fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
- Ok(())
- }
-}
diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs
index 57ac652..fb5e2c8 100644
--- a/dichroism/src/main.rs
+++ b/dichroism/src/main.rs
@@ -15,9 +15,8 @@ mod config;
mod constants;
mod error;
mod handlers;
-mod image_api;
-mod image_repo;
mod models;
+mod photo_repo;
mod product_repo;
mod result;
mod schema;
@@ -39,8 +38,9 @@ async fn main() -> Result<()> {
.data(config.clone())
.data(pool.clone())
.service(handlers::hello)
- .service(handlers::create_image)
- .service(handlers::get_images)
+ .service(handlers::get_products)
+ .service(handlers::update_product)
+ .service(handlers::create_product)
});
let mut listenfd = ListenFd::from_env();
diff --git a/dichroism/src/models.rs b/dichroism/src/models.rs
deleted file mode 100644
index 0485237..0000000
--- a/dichroism/src/models.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-#[derive(Debug, Queryable, Serialize, Clone)]
-pub struct ProductImg {
- pub id: i32,
- pub path: String,
-}
-
-use super::schema::images;
-#[table_name = "images"]
-#[derive(Debug, Insertable)]
-pub struct NewProductImg {
- pub path: String,
-}
diff --git a/dichroism/src/models/mod.rs b/dichroism/src/models/mod.rs
new file mode 100644
index 0000000..e0fc3bd
--- /dev/null
+++ b/dichroism/src/models/mod.rs
@@ -0,0 +1,13 @@
+mod new_photo;
+mod new_photo_data;
+mod new_photo_set;
+mod photo;
+mod photo_set;
+mod product;
+
+pub use new_photo::*;
+pub use new_photo_data::*;
+pub use new_photo_set::*;
+pub use photo::*;
+pub use photo_set::*;
+pub use product::*;
diff --git a/dichroism/src/models/new_photo.rs b/dichroism/src/models/new_photo.rs
new file mode 100644
index 0000000..d348e95
--- /dev/null
+++ b/dichroism/src/models/new_photo.rs
@@ -0,0 +1,6 @@
+use crate::schema::photos;
+#[table_name = "photos"]
+#[derive(Debug, Insertable)]
+pub struct NewPhoto {
+ pub path: String,
+}
diff --git a/dichroism/src/image_api.rs b/dichroism/src/models/new_photo_data.rs
index 07b9552..19a6557 100644
--- a/dichroism/src/image_api.rs
+++ b/dichroism/src/models/new_photo_data.rs
@@ -1,3 +1,5 @@
+use super::NewPhoto;
+use super::NewPhotoSet;
use crate::error::DichroismError;
use crate::result::Result;
use base64::decode;
@@ -9,20 +11,19 @@ use std::path::PathBuf;
use uuid::Uuid;
use once_cell::sync::Lazy;
-
static DATA_URI_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)")
.expect("Couldn't parse data URI Regex.")
});
-pub struct NewProductImageData {
+pub struct NewPhotoSetData {
original: DynamicImage, // original, just for safe-keeping
fullsize: DynamicImage, // full-size, "zoomed" view
base: DynamicImage, // basic viewing
thumbnail: DynamicImage, // tiny, square thumbnail
}
-impl NewProductImageData {
+impl NewPhotoSetData {
pub fn from_data_uri(uri: &str) -> Result<Self> {
let data = DATA_URI_RE
.captures(uri)
@@ -44,7 +45,7 @@ impl NewProductImageData {
}
.resize(300, 300, FilterType::Lanczos3);
- Ok(NewProductImageData {
+ Ok(NewPhotoSetData {
original,
fullsize,
base,
@@ -52,8 +53,8 @@ impl NewProductImageData {
})
}
- pub fn commit(self, prefix: &str) -> Result<NewProductImages> {
- Ok(NewProductImages {
+ pub fn commit(self, prefix: &str) -> Result<NewPhotoSet> {
+ Ok(NewPhotoSet {
original: self.commit_single(prefix, &self.original)?,
fullsize: self.commit_single(prefix, &self.fullsize)?,
base: self.commit_single(prefix, &self.base)?,
@@ -61,7 +62,7 @@ impl NewProductImageData {
})
}
- fn commit_single(&self, prefix: &str, image: &DynamicImage) -> Result<String> {
+ fn commit_single(&self, prefix: &str, image: &DynamicImage) -> Result<NewPhoto> {
let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes())
.to_hyphenated()
.to_string();
@@ -70,27 +71,23 @@ impl NewProductImageData {
path.push(base_name);
path.set_extension("jpg");
image.save(&path)?;
- Ok(path.to_str().ok_or(DichroismError::ImageWrite)?.to_string())
- }
-}
-pub struct NewProductImages {
- original: String,
- fullsize: String,
- base: String,
- thumbnail: String,
+ Ok(NewPhoto {
+ path: path.to_str().ok_or(DichroismError::ImageWrite)?.to_string(),
+ })
+ }
}
#[cfg(test)]
mod tests {
use super::*;
- const TEST_DATA_URI: &str = include_str!("unit_test_data/img_data_uri.txt");
- const TEST_DATA_BASE64: &str = include_str!("unit_test_data/test_data_base64.txt");
+ const TEST_DATA_URI: &str = include_str!("../unit_test_data/img_data_uri.txt");
+ const TEST_DATA_BASE64: &str = include_str!("../unit_test_data/test_data_base64.txt");
#[test]
fn test_gen_product_images() {
- NewProductImageData::from_data_uri(TEST_DATA_URI.trim())
+ NewPhotoSetData::from_data_uri(TEST_DATA_URI.trim())
.unwrap()
.commit(".")
.unwrap();
diff --git a/dichroism/src/models/new_photo_set.rs b/dichroism/src/models/new_photo_set.rs
new file mode 100644
index 0000000..e907592
--- /dev/null
+++ b/dichroism/src/models/new_photo_set.rs
@@ -0,0 +1,8 @@
+use super::NewPhoto;
+
+pub struct NewPhotoSet {
+ pub original: NewPhoto,
+ pub fullsize: NewPhoto,
+ pub base: NewPhoto,
+ pub thumbnail: NewPhoto,
+}
diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs
new file mode 100644
index 0000000..2e8b3bf
--- /dev/null
+++ b/dichroism/src/models/photo.rs
@@ -0,0 +1,5 @@
+#[derive(Debug, Queryable, Serialize, Clone)]
+pub struct Photo {
+ pub id: i32,
+ pub path: String,
+}
diff --git a/dichroism/src/models/photo_set.rs b/dichroism/src/models/photo_set.rs
new file mode 100644
index 0000000..2f6c128
--- /dev/null
+++ b/dichroism/src/models/photo_set.rs
@@ -0,0 +1,8 @@
+#[derive(Debug, Queryable, Clone)]
+pub struct PhotoSet {
+ pub id: i32,
+ pub base: i32,
+ pub fullsize: i32,
+ pub thumbnail: i32,
+ pub original: i32,
+}
diff --git a/dichroism/src/models/product.rs b/dichroism/src/models/product.rs
new file mode 100644
index 0000000..9b81b8a
--- /dev/null
+++ b/dichroism/src/models/product.rs
@@ -0,0 +1,26 @@
+#[derive(Debug, Clone, Queryable, Serialize)]
+pub struct Product {
+ pub id: i32,
+ pub name: String,
+ pub quantity: i32,
+ pub cents: i32,
+ pub description: String,
+ pub featured: bool,
+ pub category_path: String,
+ pub photo_set: PhotoSet,
+}
+
+#[derive(Debug, Clone, Serialize)]
+pub struct PhotoSet {
+ pub id: i32,
+ pub base: i32,
+ pub fullsize: i32,
+ pub thumbnail: i32,
+ pub original: i32,
+}
+
+#[derive(Debug, Clone, Serialize)]
+pub struct Photo {
+ pub id: i32,
+ pub path: String,
+}
diff --git a/dichroism/src/photo_repo.rs b/dichroism/src/photo_repo.rs
new file mode 100644
index 0000000..64390c8
--- /dev/null
+++ b/dichroism/src/photo_repo.rs
@@ -0,0 +1,41 @@
+use super::models::*;
+use diesel::prelude::*;
+use diesel::result::Error;
+
+type DBConn = SqliteConnection;
+
+pub fn read_photos(conn: &DBConn) -> Result<Vec<Photo>, Error> {
+ use crate::schema::photos::dsl::*;
+ let results = photos.load::<Photo>(conn)?;
+ Ok(results)
+}
+
+fn read_photos_by_path(conn: &DBConn, path: &str) -> Result<Vec<Photo>, Error> {
+ use crate::schema::photos::dsl::*;
+ let results = photos.filter(path.eq(path)).load::<Photo>(conn)?;
+ Ok(results)
+}
+
+pub fn read_photo_by_path(conn: &DBConn, path: &str) -> Result<Option<Photo>, Error> {
+ use crate::schema::photos::dsl::*;
+ let results = photos.filter(path.eq(path)).limit(1).load::<Photo>(conn)?;
+ Ok(results.first().cloned())
+}
+
+pub fn read_photo_by_id(conn: &DBConn, id: i32) -> Result<Option<Photo>, Error> {
+ use crate::schema::photos::dsl::*;
+ let results = photos.filter(id.eq(id)).limit(1).load::<Photo>(conn)?;
+ Ok(results.first().cloned())
+}
+
+pub fn create_photo(conn: &DBConn, new_photo: NewPhoto) -> Result<Option<Photo>, Error> {
+ use super::schema::photos;
+ diesel::insert_into(photos::table)
+ .values(&new_photo)
+ .execute(conn)?;
+ read_photo_by_path(conn, &new_photo.path)
+}
+
+pub fn update_photo() {
+ todo!()
+}
diff --git a/dichroism/src/product_api.rs b/dichroism/src/product_api.rs
deleted file mode 100644
index e69de29..0000000
--- a/dichroism/src/product_api.rs
+++ /dev/null
diff --git a/dichroism/src/product_repo.rs b/dichroism/src/product_repo.rs
index 67c5d3e..852b930 100644
--- a/dichroism/src/product_repo.rs
+++ b/dichroism/src/product_repo.rs
@@ -1,18 +1,21 @@
-use super::models::ProductImg;
+use super::models::Product;
use diesel::prelude::*;
use diesel::result::Error;
type DBConn = SqliteConnection;
-pub fn read_products(_conn: &DBConn) -> Result<Vec<ProductImg>, Error> {
+pub fn create_product(conn: &DBConn) -> Result<Product, Error> {
todo!()
}
-#[cfg(test)]
-mod tests {
+pub fn read_products(_conn: &DBConn) -> Result<Vec<Product>, Error> {
+ todo!()
+}
+
+pub fn update_product(conn: &DBConn) -> Result<Product, Error> {
+ todo!()
+}
- #[test]
- fn test() -> std::result::Result<(), Box<dyn std::error::Error>> {
- Ok(())
- }
+pub fn delete_product(conn: &DBConn) -> Result<(), Error> {
+ todo!()
}
diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs
index e1c4408..5b038e6 100644
--- a/dichroism/src/schema.rs
+++ b/dichroism/src/schema.rs
@@ -1,6 +1,21 @@
table! {
- images (id) {
+ photo_sets (id) {
+ id -> Integer,
+ base -> Integer,
+ fullsize -> Integer,
+ thumbnail -> Integer,
+ original -> Integer,
+ }
+}
+
+table! {
+ photos (id) {
id -> Integer,
path -> Text,
}
}
+
+allow_tables_to_appear_in_same_query!(
+ photo_sets,
+ photos,
+);