diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-10-20 09:50:08 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-10-20 09:50:08 -0400 |
commit | 76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e (patch) | |
tree | dfc108120f067880b708a64ef6567879f4bb3284 /dichroism/src/photo_repo.rs | |
parent | d3a28fde46bb06f084c74904fa8849b40e5f8c87 (diff) | |
download | theglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.tar.xz theglassyladies-76a782599b4ecc4ecb9b0ce7acc6420ed9e1ec8e.zip |
back to basics -- working on the most-required functionality first, like handlers
Diffstat (limited to 'dichroism/src/photo_repo.rs')
-rw-r--r-- | dichroism/src/photo_repo.rs | 41 |
1 files changed, 41 insertions, 0 deletions
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!() +} |