diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-10-07 09:37:41 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-10-07 09:37:41 -0400 |
commit | 89722ebd6dcdc7067277050a431fbb7b9ea1dcf5 (patch) | |
tree | a23a5029b94e7ddec8d8ecdae0438f353cede71a /dichroism/src/image_repo.rs | |
parent | 743ae22168b1fcdf2e1e54bcadbb1bb3fce3370d (diff) | |
download | theglassyladies-89722ebd6dcdc7067277050a431fbb7b9ea1dcf5.tar.xz theglassyladies-89722ebd6dcdc7067277050a431fbb7b9ea1dcf5.zip |
sorted out image repo, error handling
Diffstat (limited to 'dichroism/src/image_repo.rs')
-rw-r--r-- | dichroism/src/image_repo.rs | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/dichroism/src/image_repo.rs b/dichroism/src/image_repo.rs index c36f94e..e0f420b 100644 --- a/dichroism/src/image_repo.rs +++ b/dichroism/src/image_repo.rs @@ -1,4 +1,4 @@ -use super::models::ProductImg; +use super::models::{NewProductImg, ProductImg}; use diesel::prelude::*; use diesel::result::Error; @@ -10,8 +10,33 @@ pub fn read_images(conn: &DBConn) -> Result<Vec<ProductImg>, Error> { Ok(results) } -pub fn create_image() { - todo!() +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() { |