From 64f6c60bfd278d78ed6ee9f9b458380c996a73de Mon Sep 17 00:00:00 2001 From: janost Date: Sun, 14 Oct 2018 16:04:23 +0200 Subject: [PATCH] Organization::save() and UserOrganization::save() should return QueryResult instead of bool --- src/api/core/accounts.rs | 4 +++- src/api/core/organizations.rs | 33 ++++++++++++++++++++++----------- src/db/models/organization.rs | 24 ++++++++---------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 7d45492..1f8391c 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -39,7 +39,9 @@ fn register(data: JsonUpcase, conn: DbConn) -> EmptyResult { if Invitation::take(&data.Email, &conn) { for mut user_org in UserOrganization::find_invited_by_user(&user.uuid, &conn).iter_mut() { user_org.status = UserOrgStatus::Accepted as i32; - user_org.save(&conn); + if user_org.save(&conn).is_err() { + err!("Failed to accept user to organization") + } }; user } else if CONFIG.signups_allowed { diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 1a74e6c..a2782fa 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -49,8 +49,13 @@ fn create_organization(headers: Headers, data: JsonUpcase, conn: DbConn user_org.type_ = UserOrgType::Owner as i32; user_org.status = UserOrgStatus::Confirmed as i32; - org.save(&conn); - user_org.save(&conn); + if org.save(&conn).is_err() { + err!("Failed creating organization") + } + if user_org.save(&conn).is_err() { + err!("Failed to add user to organization") + } + if collection.save(&conn).is_err() { err!("Failed creating Collection"); } @@ -128,9 +133,11 @@ fn post_organization(org_id: String, _headers: OwnerHeaders, data: JsonUpcase Ok(Json(org.to_json())), + Err(_) => err!("Failed to modify organization") + } } // GET /api/collections?writeOnly=false @@ -427,7 +434,9 @@ fn send_invite(org_id: String, data: JsonUpcase, headers: AdminHeade } } - new_user.save(&conn); + if new_user.save(&conn).is_err() { + err!("Failed to add user to organization") + } } } @@ -458,9 +467,10 @@ fn confirm_invite(org_id: String, org_user_id: String, data: JsonUpcase, None => err!("Invalid key provided") }; - user_to_confirm.save(&conn); - - Ok(()) + match user_to_confirm.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to add user to organization") + } } #[get("/organizations//users/")] @@ -551,9 +561,10 @@ fn edit_user(org_id: String, org_user_id: String, data: JsonUpcase } } - user_to_edit.save(&conn); - - Ok(()) + match user_to_edit.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save user data") + } } #[delete("/organizations//users/")] diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 3ce29d2..7f55cd8 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -137,9 +137,9 @@ use db::schema::{organizations, users_organizations, users_collections, ciphers_ /// Database methods impl Organization { - pub fn save(&mut self, conn: &DbConn) -> bool { + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { if self.uuid == Organization::VIRTUAL_ID { - return false + return Err(diesel::result::Error::NotFound) } UserOrganization::find_by_org(&self.uuid, conn) @@ -148,12 +148,8 @@ impl Organization { User::update_uuid_revision(&user_org.user_uuid, conn); }); - match diesel::replace_into(organizations::table) - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + diesel::replace_into(organizations::table) + .values(&*self).execute(&**conn).and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { @@ -266,18 +262,14 @@ impl UserOrganization { }) } - pub fn save(&mut self, conn: &DbConn) -> bool { + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { if self.org_uuid == Organization::VIRTUAL_ID { - return false + return Err(diesel::result::Error::NotFound) } User::update_uuid_revision(&self.user_uuid, conn); - match diesel::replace_into(users_organizations::table) - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + diesel::replace_into(users_organizations::table) + .values(&*self).execute(&**conn).and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> {