diff options
author | Adam T. Carpenter <atc@53hor.net> | 2020-11-11 20:07:12 -0500 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2020-11-11 20:07:12 -0500 |
commit | 7381a7033231e6454a37fd64b1f3de4e8d59355f (patch) | |
tree | 7ae54976c2d036e0fe7cb199a7f6facdd09bbb6d /dichroism/src/repo/photo_set_repo.rs | |
parent | c5280144de096c274f185fade287ccd63b954ceb (diff) | |
download | theglassyladies-7381a7033231e6454a37fd64b1f3de4e8d59355f.tar.xz theglassyladies-7381a7033231e6454a37fd64b1f3de4e8d59355f.zip |
Kind of flailing with the UI; lots of API bugfixes though.
Diffstat (limited to 'dichroism/src/repo/photo_set_repo.rs')
-rw-r--r-- | dichroism/src/repo/photo_set_repo.rs | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/dichroism/src/repo/photo_set_repo.rs b/dichroism/src/repo/photo_set_repo.rs index 339ea91..56ca962 100644 --- a/dichroism/src/repo/photo_set_repo.rs +++ b/dichroism/src/repo/photo_set_repo.rs @@ -5,13 +5,15 @@ 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() { + if let Some(photo_set_id) = photo_set.id { // update let form = PhotoSetForm::from(photo_set.clone()); - update(photo_sets).set(&form).execute(conn)?; + update(photo_sets.filter(id.eq(photo_set_id))) + .set(&form) + .execute(conn)?; } else { // insert - photo_set.id = Some(find_next_id(conn)?); + photo_set.id = Some(find_next_id(conn)); let form = PhotoSetForm::from(photo_set.clone()); insert_into(photo_sets).values(&form).execute(conn)?; } @@ -26,17 +28,17 @@ pub fn find(conn: &DBConn, dbid: i32) -> Result<Option<models::PhotoSet>, Error> 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) - } - } + Err(Error::NotFound) => Ok(None), + Err(e) => Err(e), } } -fn find_next_id(conn: &DBConn) -> Result<i32, Error> { +fn find_next_id(conn: &DBConn) -> i32 { use crate::schema::photo_sets::dsl::*; - Ok(photo_sets.select(id).order(id.desc()).first::<i32>(conn)? + 1) + let last_id = photo_sets + .select(id) + .order(id.desc()) + .first::<i32>(conn) + .unwrap_or(0); + last_id + 1 } |