diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-11-02 20:36:18 -0500 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-11-02 20:36:18 -0500 |
commit | dcc96d0b349583e5d6a0f25ae1f7a3ffa3769788 (patch) | |
tree | 98a9cc9d322e309c05db400f4c536164bee4daac /dichroism/src/repo/photo_set_repo.rs | |
parent | 9480317011b57d3be7b903048f4a85d02979c7c7 (diff) | |
download | theglassyladies-dcc96d0b349583e5d6a0f25ae1f7a3ffa3769788.tar.xz theglassyladies-dcc96d0b349583e5d6a0f25ae1f7a3ffa3769788.zip |
swapped json payload url encoded images for multipart form data
Diffstat (limited to 'dichroism/src/repo/photo_set_repo.rs')
-rw-r--r-- | dichroism/src/repo/photo_set_repo.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/dichroism/src/repo/photo_set_repo.rs b/dichroism/src/repo/photo_set_repo.rs new file mode 100644 index 0000000..339ea91 --- /dev/null +++ b/dichroism/src/repo/photo_set_repo.rs @@ -0,0 +1,42 @@ +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 photo_set.id.is_some() { + // update + let form = PhotoSetForm::from(photo_set.clone()); + update(photo_sets).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(e) => { + if e == Error::NotFound { + Ok(None) + } else { + Err(e) + } + } + } +} + +fn find_next_id(conn: &DBConn) -> Result<i32, Error> { + use crate::schema::photo_sets::dsl::*; + Ok(photo_sets.select(id).order(id.desc()).first::<i32>(conn)? + 1) +} |