From fe473b9e75342c76a2002f9a24bff4b218e91aae Mon Sep 17 00:00:00 2001 From: Baelyk Date: Wed, 29 Aug 2018 08:22:19 -0500 Subject: [PATCH] `Attachment::save()` returns Result instead of bool (#161) Returning a result instead of a bool as per #6 --- src/api/core/ciphers.rs | 13 ++++++++----- src/db/models/attachment.rs | 12 +++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index fe6caf46..f7d74d86 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -375,11 +375,11 @@ fn put_cipher_share_seleted(data: JsonUpcase, headers: if data.Ciphers.len() == 0 { err!("You must select at least one cipher.") } - + if data.CollectionIds.len() == 0 { err!("You must select at least one collection.") } - + for cipher in data.Ciphers.iter() { match cipher.Id { Some(ref id) => cipher_ids.push(id.to_string()), @@ -388,7 +388,7 @@ fn put_cipher_share_seleted(data: JsonUpcase, headers: } let attachments = Attachment::find_by_ciphers(cipher_ids, &conn); - + if attachments.len() > 0 { err!("Ciphers should not have any attachments.") } @@ -492,7 +492,10 @@ fn post_attachment(uuid: String, data: Data, content_type: &ContentType, headers }; let attachment = Attachment::new(file_name, cipher.uuid.clone(), name, size); - attachment.save(&conn); + match attachment.save(&conn) { + Ok(()) => (), + Err(_) => println!("Error: failed to save attachment") + }; }).expect("Error processing multipart data"); Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) @@ -654,7 +657,7 @@ fn delete_all(data: JsonUpcase, headers: Headers, conn: DbConn) -> for f in Folder::find_by_user(&user.uuid, &conn) { if f.delete(&conn).is_err() { err!("Failed deleting folder") - } + } } Ok(()) diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index 66d5b723..9c8eeefd 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -53,13 +53,11 @@ use db::schema::attachments; /// Database methods impl Attachment { - pub fn save(&self, conn: &DbConn) -> bool { - match diesel::replace_into(attachments::table) + pub fn save(&self, conn: &DbConn) -> QueryResult<()> { + diesel::replace_into(attachments::table) .values(self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + .execute(&**conn) + .and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { @@ -67,7 +65,7 @@ impl Attachment { use std::{thread, time}; let mut retries = 10; - + loop { match diesel::delete( attachments::table.filter(