From 77cf63c06d2b5513ca2cb135a87157e0b940772b Mon Sep 17 00:00:00 2001 From: janost Date: Sun, 14 Oct 2018 14:55:00 +0200 Subject: [PATCH] Folder::save() should return QueryResult instead of bool --- src/api/core/ciphers.rs | 14 +++++++++----- src/api/core/folders.rs | 8 ++++++-- src/db/models/folder.rs | 10 +++------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 5d1d73cd..8c9cd6a4 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -229,11 +229,15 @@ fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbC let data: ImportData = data.into_inner().data; // Read and create the folders - let folders: Vec<_> = data.Folders.into_iter().map(|folder| { - let mut folder = Folder::new(headers.user.uuid.clone(), folder.Name); - folder.save(&conn); - folder - }).collect(); + let mut folders: Vec<_> = Vec::new(); + for folder in data.Folders.into_iter() { + let mut new_folder = Folder::new(headers.user.uuid.clone(), folder.Name); + if new_folder.save(&conn).is_err() { + err!("Failed importing folders") + } else { + folders.push(new_folder); + } + } // Read the relations between folders and ciphers use std::collections::HashMap; diff --git a/src/api/core/folders.rs b/src/api/core/folders.rs index 6532a7ef..91114605 100644 --- a/src/api/core/folders.rs +++ b/src/api/core/folders.rs @@ -47,7 +47,9 @@ fn post_folders(data: JsonUpcase, headers: Headers, conn: DbConn, ws let mut folder = Folder::new(headers.user.uuid.clone(), data.Name); - folder.save(&conn); + if folder.save(&conn).is_err() { + err!("Failed to save folder") + } ws.send_folder_update(UpdateType::SyncFolderCreate, &folder); Ok(Json(folder.to_json())) @@ -73,7 +75,9 @@ fn put_folder(uuid: String, data: JsonUpcase, headers: Headers, conn folder.name = data.Name; - folder.save(&conn); + if folder.save(&conn).is_err() { + err!("Failed to save folder") + } ws.send_folder_update(UpdateType::SyncFolderUpdate, &folder); Ok(Json(folder.to_json())) diff --git a/src/db/models/folder.rs b/src/db/models/folder.rs index d50912ed..2808b929 100644 --- a/src/db/models/folder.rs +++ b/src/db/models/folder.rs @@ -70,16 +70,12 @@ use db::schema::{folders, folders_ciphers}; /// Database methods impl Folder { - pub fn save(&mut self, conn: &DbConn) -> bool { + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { User::update_uuid_revision(&self.user_uuid, conn); self.updated_at = Utc::now().naive_utc(); - match diesel::replace_into(folders::table) - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + diesel::replace_into(folders::table) + .values(&*self).execute(&**conn).and(Ok(())) } pub fn delete(&self, conn: &DbConn) -> QueryResult<()> {