diff options
Diffstat (limited to 'dichroism/src')
27 files changed, 0 insertions, 773 deletions
diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs deleted file mode 100644 index 2031ecc..0000000 --- a/dichroism/src/config.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::constants::DEFAULT_CONFIG; -use crate::result::Result; -use once_cell::sync::Lazy; -use serde::Deserialize; -use std::env::var; -use std::fs::File; -use std::io::prelude::*; -use std::net::SocketAddr; -use toml::from_str; - -pub static CONFIG_INSTANCE: Lazy<Config> = Lazy::new(|| { - Config::from_toml().unwrap_or_else(|e| { - eprintln!("Error parsing config: {}", e.to_string()); - std::process::exit(1); - }) -}); - -#[derive(Debug, Clone, Deserialize)] -pub struct Config { - pub db_url: String, - pub img_root: String, - pub bind_addr: SocketAddr, -} - -impl Config { - pub fn from_toml() -> Result<Self> { - let path = var("DICHROISM_CONFIG").unwrap_or_else(|_| String::from(DEFAULT_CONFIG)); - let mut config = String::new(); - File::open(path)?.read_to_string(&mut config)?; - Ok(from_str(&config)?) - } -} diff --git a/dichroism/src/constants.rs b/dichroism/src/constants.rs deleted file mode 100644 index 72a8de9..0000000 --- a/dichroism/src/constants.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub const DEFAULT_CONFIG: &str = "./Dichroism.toml"; -pub const PHOTO_FULLSIZE_XY: u32 = 1000; -pub const PHOTO_BASE_XY: u32 = 640; -pub const PHOTO_THUMBNAIL_XY: u32 = 300; diff --git a/dichroism/src/dtos/mod.rs b/dichroism/src/dtos/mod.rs deleted file mode 100644 index a3e27e5..0000000 --- a/dichroism/src/dtos/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod photo_set_get; -mod product_get; -mod product_patch; -mod product_post; - -pub use photo_set_get::*; -pub use product_get::*; -pub use product_patch::*; -pub use product_post::*; diff --git a/dichroism/src/dtos/photo_set_get.rs b/dichroism/src/dtos/photo_set_get.rs deleted file mode 100644 index b2e8d7b..0000000 --- a/dichroism/src/dtos/photo_set_get.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::models::PhotoSet; - -#[derive(Debug, Serialize)] -pub struct PhotoSetGet { - pub id: i32, - pub fullsize: String, - pub base: String, - pub thumbnail: String, -} - -impl From<PhotoSet> for PhotoSetGet { - fn from(p: PhotoSet) -> Self { - Self { - id: p.id.unwrap_or(-1), - fullsize: p.fullsize.id, - base: p.base.id, - thumbnail: p.thumbnail.id, - } - } -} diff --git a/dichroism/src/dtos/product_get.rs b/dichroism/src/dtos/product_get.rs deleted file mode 100644 index aa52f19..0000000 --- a/dichroism/src/dtos/product_get.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::models::Product; - -#[derive(Debug, Serialize)] -pub struct ProductGet { - pub id: i32, - pub name: String, - pub description: String, - pub cents: i32, - pub quantity: i32, - pub featured: bool, - pub photo_base: String, - pub photo_fullsize: String, - pub photo_thumbnail: String, - pub category: String, -} - -impl From<Product> for ProductGet { - fn from(p: Product) -> Self { - Self { - id: p.id.unwrap_or(-1), - name: p.name, - description: p.description, - cents: p.cents, - quantity: p.quantity, - featured: p.featured, - category: p.category, - photo_fullsize: p.photo_set.fullsize.id, - photo_base: p.photo_set.base.id, - photo_thumbnail: p.photo_set.thumbnail.id, - } - } -} diff --git a/dichroism/src/dtos/product_patch.rs b/dichroism/src/dtos/product_patch.rs deleted file mode 100644 index f231469..0000000 --- a/dichroism/src/dtos/product_patch.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::models::Product; - -#[derive(Debug, Deserialize)] -pub struct ProductPatch { - pub id: i32, - pub name: Option<String>, - pub quantity: Option<i32>, - pub cents: Option<i32>, - pub description: Option<String>, - pub featured: Option<bool>, - pub category_path: Option<String>, - pub photo_set: Option<i32>, -} - -impl ProductPatch { - pub fn patch(self, product: &mut Product) { - if let Some(name) = self.name { - product.name = name; - } - if let Some(category) = self.category_path { - product.category = category; - } - if let Some(description) = self.description { - product.description = description; - } - - product.quantity = self.quantity.unwrap_or(product.quantity); - product.cents = self.cents.unwrap_or(product.cents); - product.featured = self.featured.unwrap_or(product.featured); - } -} diff --git a/dichroism/src/dtos/product_post.rs b/dichroism/src/dtos/product_post.rs deleted file mode 100644 index c04b211..0000000 --- a/dichroism/src/dtos/product_post.rs +++ /dev/null @@ -1,11 +0,0 @@ -#[derive(Debug, Deserialize)] -pub struct ProductPost { - pub id: Option<i32>, - pub name: String, - pub quantity: i32, - pub cents: i32, - pub description: String, - pub featured: bool, - pub category_path: String, - pub photo_set: i32, -} diff --git a/dichroism/src/error.rs b/dichroism/src/error.rs deleted file mode 100644 index 0cdd269..0000000 --- a/dichroism/src/error.rs +++ /dev/null @@ -1,25 +0,0 @@ -#[derive(Debug)] -pub enum DichroismError { - UriDataExtract(String), - ImageWrite(String), -} - -impl std::error::Error for DichroismError {} - -impl std::fmt::Display for DichroismError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self) - } -} - -impl From<image::ImageError> for DichroismError { - fn from(e: image::ImageError) -> Self { - Self::ImageWrite(e.to_string()) - } -} - -impl From<base64::DecodeError> for DichroismError { - fn from(e: base64::DecodeError) -> Self { - Self::UriDataExtract(e.to_string()) - } -} diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs deleted file mode 100644 index b9420af..0000000 --- a/dichroism/src/handlers.rs +++ /dev/null @@ -1,131 +0,0 @@ -use crate::dtos::*; -use crate::image_service; -use crate::models::Product; -use crate::repo::{photo_set_repo, product_repo}; -use crate::types::DbPool; -use actix_multipart::Multipart; -use actix_web::{get, patch, post, web, Error, HttpResponse, Responder}; -use futures::{StreamExt, TryStreamExt}; - -fn to_internal_error(e: impl std::error::Error) -> HttpResponse { - HttpResponse::InternalServerError().body(e.to_string()) -} - -#[get("/")] -async fn hello() -> impl Responder { - HttpResponse::Ok().body("Hey, this is an API!") -} - -#[get("/products")] -async fn get_products(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> { - let conn = pool.get().map_err(to_internal_error)?; - let products: Vec<ProductGet> = web::block(move || product_repo::find_all(&conn)) - .await - .map_err(to_internal_error)? - .into_iter() - .map(|p| p.into()) - .collect(); - Ok(HttpResponse::Ok().json(products)) -} - -#[patch("/products")] -async fn patch_product( - pool: web::Data<DbPool>, - web::Json(patch): web::Json<ProductPatch>, -) -> Result<HttpResponse, Error> { - let id = patch.id; - - // get product referenced by patch - let conn = pool.get().map_err(to_internal_error)?; - let mut product = web::block(move || product_repo::find(&conn, id)) - .await - .map_err(to_internal_error)? - .ok_or_else(|| HttpResponse::NotFound().finish())?; - - // get photo set referenced by patch - if let Some(id) = patch.photo_set { - let conn = pool.get().map_err(to_internal_error)?; - let photo_set = web::block(move || photo_set_repo::find(&conn, id)) - .await - .map_err(to_internal_error)? - .ok_or_else(|| HttpResponse::NotFound().body("Photo set not found"))?; - product.photo_set = photo_set; - } - - // update product fields - patch.patch(&mut product); - - // store the updated product - let conn = pool.get().map_err(to_internal_error)?; - let product = web::block(move || product_repo::store(&conn, product)) - .await - .map_err(to_internal_error)?; - - Ok(HttpResponse::Ok().json(ProductGet::from(product))) -} - -#[post("/products")] -async fn post_product( - pool: web::Data<DbPool>, - web::Json(post): web::Json<ProductPost>, -) -> Result<HttpResponse, Error> { - // find associated photo set - let photo_set = post.photo_set; - let conn = pool.get().map_err(to_internal_error)?; - let photo_set = web::block(move || photo_set_repo::find(&conn, photo_set)) - .await - .map_err(to_internal_error)? - .ok_or_else(|| HttpResponse::NotFound().body("Photo set not found."))?; - - // create new product - let product = Product { - id: None, - name: post.name, - quantity: post.quantity, - cents: post.cents, - description: post.description, - featured: post.featured, - category: post.category_path, - photo_set, - }; - - // store product - let conn = pool.get().map_err(to_internal_error)?; - let product = web::block(move || product_repo::store(&conn, product)) - .await - .map_err(to_internal_error)?; - - Ok(HttpResponse::Ok().json::<ProductGet>(product.into())) -} - -#[post("/photos")] -async fn post_photo( - pool: web::Data<DbPool>, - mut payload: Multipart, -) -> Result<HttpResponse, Error> { - let mut responses: Vec<PhotoSetGet> = Vec::new(); - - while let Some(mut field) = payload - .try_next() - .await - .map_err(|e| HttpResponse::BadRequest().body(e.to_string()))? - { - // grab all bytes - let mut data: Vec<u8> = Vec::new(); - while let Some(chunk) = field.next().await { - data.extend(chunk?); - } - - // create new photo_set - let photo_set = web::block(move || image_service::generate_photo_set(data)) - .await - .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 - .map_err(to_internal_error)?; - responses.push(photo_set.into()); - } - - Ok(HttpResponse::Ok().json(responses)) -} diff --git a/dichroism/src/image_service.rs b/dichroism/src/image_service.rs deleted file mode 100644 index 0d2ed14..0000000 --- a/dichroism/src/image_service.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::config::CONFIG_INSTANCE; -use crate::constants::{PHOTO_BASE_XY, PHOTO_FULLSIZE_XY, PHOTO_THUMBNAIL_XY}; -use crate::error::DichroismError; -use crate::models::{Photo, PhotoSet}; -use exif::Reader; -use image::imageops::FilterType; -use image::DynamicImage; -use image::GenericImageView; -use std::io::{BufReader, Cursor}; -use std::path::PathBuf; -use uuid::Uuid; - -pub fn generate_photo_set(data: Vec<u8>) -> Result<PhotoSet, DichroismError> { - // load image data from bytes - let mut original = image::load_from_memory(&data)?; - - // read EXIF tags to check for required rotation - let exifreader = Reader::new(); - let mut reader = BufReader::new(Cursor::new(data)); - let exif = exifreader.read_from_container(&mut reader).unwrap(); - if let Some(orient) = exif - .get_field(exif::Tag::Orientation, exif::In::PRIMARY) - .map(|o| o.value.get_uint(0)) - .flatten() - { - // if a tag was present, rotate original image data as necessary - match orient { - 8 => original = original.rotate270(), - 3 => original = original.rotate180(), - 6 => original = original.rotate90(), - _ => {} - } - } - - let fullsize = original.resize(PHOTO_FULLSIZE_XY, PHOTO_FULLSIZE_XY, FilterType::Lanczos3); - let base = original.resize(PHOTO_BASE_XY, PHOTO_BASE_XY, FilterType::Lanczos3); - - let (width, height) = original.dimensions(); - let thumbnail = if width > height { - let offset = (width - height) / 2; - original.crop_imm(offset, 0, width - offset * 2, height) - } else { - let offset = (height - width) / 2; - original.crop_imm(0, offset, width, height - offset * 2) - } - .resize(PHOTO_THUMBNAIL_XY, PHOTO_THUMBNAIL_XY, FilterType::Lanczos3); - - Ok(PhotoSet { - id: None, - original: generate_photo(&original)?, - fullsize: generate_photo(&fullsize)?, - base: generate_photo(&base)?, - thumbnail: generate_photo(&thumbnail)?, - }) -} - -fn generate_photo(image: &DynamicImage) -> Result<Photo, DichroismError> { - let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) - .to_hyphenated() - .to_string(); - let mut path = PathBuf::from(&CONFIG_INSTANCE.img_root); - path.push(base_name); - path.set_extension("jpg"); - image.save(&path)?; - - let id = path - .file_name() - .ok_or_else(|| { - DichroismError::ImageWrite("Error extracting filename from path".to_string()) - })? - .to_str() - .ok_or_else(|| { - DichroismError::ImageWrite("Error converting filename to slice".to_string()) - })?; - Ok(Photo::new(String::from(id))) -} diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs deleted file mode 100644 index 6c76f0a..0000000 --- a/dichroism/src/main.rs +++ /dev/null @@ -1,66 +0,0 @@ -#[macro_use] -extern crate serde; -#[macro_use] -extern crate diesel; - -use actix_cors::Cors; -use actix_web::{middleware::Logger, App, HttpServer}; -use config::CONFIG_INSTANCE as CONFIG; -use diesel::prelude::SqliteConnection; -use diesel::r2d2::ConnectionManager; -use diesel::r2d2::Pool; -use listenfd::ListenFd; -use result::Result; - -mod config; -mod constants; -mod dtos; -mod error; -mod handlers; -mod image_service; -mod models; -mod repo; -mod result; -mod schema; -mod types; - -#[actix_web::main] -async fn main() -> Result<()> { - // Init logging - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); - - // Init DB connection pool - let manager = ConnectionManager::<SqliteConnection>::new(&CONFIG.db_url); - let pool = Pool::builder().build(manager)?; - - // Init application server - let mut server = HttpServer::new(move || { - // Init CORS policy - let cors = Cors::permissive(); - - App::new() - .data(pool.clone()) - .wrap(cors) - .wrap(Logger::default()) - .service(handlers::hello) - .service(handlers::get_products) - .service(handlers::patch_product) - .service(handlers::post_product) - .service(handlers::post_photo) - }); - - // If using listenfd, bind to it instead of the configured address to allow for cargo watch - // auto-reloading - let mut listenfd = ListenFd::from_env(); - server = if let Some(l) = listenfd - .take_tcp_listener(0) - .expect("Unable to grab TCP listener!") - { - server.listen(l)? - } else { - server.bind(CONFIG.bind_addr)? - }; - - Ok(server.run().await?) -} diff --git a/dichroism/src/models/dbid.rs b/dichroism/src/models/dbid.rs deleted file mode 100644 index 80eee58..0000000 --- a/dichroism/src/models/dbid.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub enum Dbid { - Stored(i32), - NotStored, -} diff --git a/dichroism/src/models/mod.rs b/dichroism/src/models/mod.rs deleted file mode 100644 index 529a9bc..0000000 --- a/dichroism/src/models/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod photo; -mod photo_set; -mod product; - -pub use photo::*; -pub use photo_set::*; -pub use product::*; diff --git a/dichroism/src/models/photo.rs b/dichroism/src/models/photo.rs deleted file mode 100644 index 086d387..0000000 --- a/dichroism/src/models/photo.rs +++ /dev/null @@ -1,10 +0,0 @@ -#[derive(Debug, Clone)] -pub struct Photo { - pub id: String, -} - -impl Photo { - pub fn new(id: String) -> Self { - Self { id } - } -} diff --git a/dichroism/src/models/photo_set.rs b/dichroism/src/models/photo_set.rs deleted file mode 100644 index f2c7677..0000000 --- a/dichroism/src/models/photo_set.rs +++ /dev/null @@ -1,10 +0,0 @@ -use super::Photo; - -#[derive(Debug, Clone)] -pub struct PhotoSet { - pub id: Option<i32>, - pub original: Photo, // original, just for safe-keeping - pub fullsize: Photo, // full-size, "zoomed" view - pub base: Photo, // basic viewing - pub thumbnail: Photo, // tiny, square thumbnail -} diff --git a/dichroism/src/models/product.rs b/dichroism/src/models/product.rs deleted file mode 100644 index 4a3d782..0000000 --- a/dichroism/src/models/product.rs +++ /dev/null @@ -1,13 +0,0 @@ -use super::PhotoSet; - -#[derive(Debug, Clone)] -pub struct Product { - pub id: Option<i32>, - pub name: String, - pub description: String, - pub cents: i32, - pub quantity: i32, - pub featured: bool, - pub photo_set: PhotoSet, - pub category: String, -} diff --git a/dichroism/src/repo/entities/mod.rs b/dichroism/src/repo/entities/mod.rs deleted file mode 100644 index 288f1e3..0000000 --- a/dichroism/src/repo/entities/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod photo_set; -mod photo_set_form; -mod product; -mod product_form; - -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 deleted file mode 100644 index 6e48e12..0000000 --- a/dichroism/src/repo/entities/photo_set.rs +++ /dev/null @@ -1,22 +0,0 @@ -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<models::PhotoSet> 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 deleted file mode 100644 index 611c8f0..0000000 --- a/dichroism/src/repo/entities/photo_set_form.rs +++ /dev/null @@ -1,24 +0,0 @@ -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<PhotoSet> 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 deleted file mode 100644 index c89495a..0000000 --- a/dichroism/src/repo/entities/product.rs +++ /dev/null @@ -1,39 +0,0 @@ -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 category: String, - pub photo_set_id: i32, - pub original: String, - pub fullsize: String, - pub base: String, - pub thumbnail: String, -} - -impl Into<models::Product> 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, - category: self.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 deleted file mode 100644 index b3447eb..0000000 --- a/dichroism/src/repo/entities/product_form.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::models::*; -use crate::schema::products; - -#[derive(Debug, 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, - pub category: String, -} - -impl From<Product> 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), - category: p.category, - } - } -} diff --git a/dichroism/src/repo/mod.rs b/dichroism/src/repo/mod.rs deleted file mode 100644 index 75a063c..0000000 --- a/dichroism/src/repo/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -use diesel::SqliteConnection; - -pub mod entities; -pub mod photo_set_repo; -pub mod product_repo; - -type DBConn = SqliteConnection; diff --git a/dichroism/src/repo/photo_set_repo.rs b/dichroism/src/repo/photo_set_repo.rs deleted file mode 100644 index 56ca962..0000000 --- a/dichroism/src/repo/photo_set_repo.rs +++ /dev/null @@ -1,44 +0,0 @@ -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<models::PhotoSet, Error> { - use crate::schema::photo_sets::dsl::*; - if let Some(photo_set_id) = photo_set.id { - // update - let form = PhotoSetForm::from(photo_set.clone()); - update(photo_sets.filter(id.eq(photo_set_id))) - .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<Option<models::PhotoSet>, 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::<PhotoSet>(conn).map(|p| p.into()); - match photo_set { - Ok(p) => Ok(Some(p)), - Err(Error::NotFound) => Ok(None), - Err(e) => Err(e), - } -} - -fn find_next_id(conn: &DBConn) -> i32 { - use crate::schema::photo_sets::dsl::*; - 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 deleted file mode 100644 index 487f96e..0000000 --- a/dichroism/src/repo/product_repo.rs +++ /dev/null @@ -1,82 +0,0 @@ -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<models::Product, Error> { - use crate::schema::products::dsl::*; - if let Some(product_id) = product.id { - // update - let form = ProductForm::from(product.clone()); - update(products.filter(id.eq(product_id))) - .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<Vec<models::Product>, 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, - products::category, - photo_sets::id, - photo_sets::original, - photo_sets::fullsize, - photo_sets::base, - photo_sets::thumbnail, - )); - Ok(query - .load::<Product>(conn)? - .into_iter() - .map(|p| p.into()) - .collect::<Vec<models::Product>>()) -} - -pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::Product>, 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, - products::category, - 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(Error::NotFound) => Ok(None), - Err(e) => Err(e), - } -} - -fn find_next_id(conn: &DBConn) -> i32 { - use crate::schema::products::dsl::*; - let last_id = products - .select(id) - .order(id.desc()) - .first::<i32>(conn) - .unwrap_or(0); - last_id + 1 -} diff --git a/dichroism/src/result.rs b/dichroism/src/result.rs deleted file mode 100644 index 708451d..0000000 --- a/dichroism/src/result.rs +++ /dev/null @@ -1 +0,0 @@ -pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs deleted file mode 100644 index 2d9e14a..0000000 --- a/dichroism/src/schema.rs +++ /dev/null @@ -1,29 +0,0 @@ -table! { - photo_sets (id) { - id -> Integer, - base -> Text, - fullsize -> Text, - thumbnail -> Text, - original -> Text, - } -} - -table! { - products (id) { - id -> Integer, - photo_set -> Integer, - cents -> Integer, - quantity -> Integer, - name -> Text, - description -> Text, - featured -> Integer, - category -> Text, - } -} - -joinable!(products -> photo_sets (photo_set)); - -allow_tables_to_appear_in_same_query!( - photo_sets, - products, -); diff --git a/dichroism/src/types.rs b/dichroism/src/types.rs deleted file mode 100644 index 91aef95..0000000 --- a/dichroism/src/types.rs +++ /dev/null @@ -1,5 +0,0 @@ -use diesel::r2d2::ConnectionManager; -use diesel::r2d2::Pool; -use diesel::SqliteConnection; - -pub type DbPool = Pool<ConnectionManager<SqliteConnection>>; |