diff options
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() { |