Merge pull request #219 from janost/refactor-organization-save

Organization::save() and UserOrganization::save() should return QueryResult instead of bool
This commit is contained in:
Daniel García 2018-10-14 17:35:30 +02:00 committed by GitHub
commit 22304f4925
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 28 deletions

View file

@ -39,7 +39,9 @@ fn register(data: JsonUpcase<RegisterData>, conn: DbConn) -> EmptyResult {
if Invitation::take(&data.Email, &conn) { if Invitation::take(&data.Email, &conn) {
for mut user_org in UserOrganization::find_invited_by_user(&user.uuid, &conn).iter_mut() { for mut user_org in UserOrganization::find_invited_by_user(&user.uuid, &conn).iter_mut() {
user_org.status = UserOrgStatus::Accepted as i32; 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 user
} else if CONFIG.signups_allowed { } else if CONFIG.signups_allowed {

View file

@ -49,8 +49,13 @@ fn create_organization(headers: Headers, data: JsonUpcase<OrgData>, conn: DbConn
user_org.type_ = UserOrgType::Owner as i32; user_org.type_ = UserOrgType::Owner as i32;
user_org.status = UserOrgStatus::Confirmed as i32; user_org.status = UserOrgStatus::Confirmed as i32;
org.save(&conn); if org.save(&conn).is_err() {
user_org.save(&conn); err!("Failed creating organization")
}
if user_org.save(&conn).is_err() {
err!("Failed to add user to organization")
}
if collection.save(&conn).is_err() { if collection.save(&conn).is_err() {
err!("Failed creating Collection"); err!("Failed creating Collection");
} }
@ -128,9 +133,11 @@ fn post_organization(org_id: String, _headers: OwnerHeaders, data: JsonUpcase<Or
org.name = data.Name; org.name = data.Name;
org.billing_email = data.BillingEmail; org.billing_email = data.BillingEmail;
org.save(&conn);
Ok(Json(org.to_json())) match org.save(&conn) {
Ok(()) => Ok(Json(org.to_json())),
Err(_) => err!("Failed to modify organization")
}
} }
// GET /api/collections?writeOnly=false // GET /api/collections?writeOnly=false
@ -427,7 +434,9 @@ fn send_invite(org_id: String, data: JsonUpcase<InviteData>, 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<Value>,
None => err!("Invalid key provided") None => err!("Invalid key provided")
}; };
user_to_confirm.save(&conn); match user_to_confirm.save(&conn) {
Ok(()) => Ok(()),
Ok(()) Err(_) => err!("Failed to add user to organization")
}
} }
#[get("/organizations/<org_id>/users/<org_user_id>")] #[get("/organizations/<org_id>/users/<org_user_id>")]
@ -551,9 +561,10 @@ fn edit_user(org_id: String, org_user_id: String, data: JsonUpcase<EditUserData>
} }
} }
user_to_edit.save(&conn); match user_to_edit.save(&conn) {
Ok(()) => Ok(()),
Ok(()) Err(_) => err!("Failed to save user data")
}
} }
#[delete("/organizations/<org_id>/users/<org_user_id>")] #[delete("/organizations/<org_id>/users/<org_user_id>")]

View file

@ -137,9 +137,9 @@ use db::schema::{organizations, users_organizations, users_collections, ciphers_
/// Database methods /// Database methods
impl Organization { impl Organization {
pub fn save(&mut self, conn: &DbConn) -> bool { pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
if self.uuid == Organization::VIRTUAL_ID { if self.uuid == Organization::VIRTUAL_ID {
return false return Err(diesel::result::Error::NotFound)
} }
UserOrganization::find_by_org(&self.uuid, conn) UserOrganization::find_by_org(&self.uuid, conn)
@ -148,12 +148,8 @@ impl Organization {
User::update_uuid_revision(&user_org.user_uuid, conn); User::update_uuid_revision(&user_org.user_uuid, conn);
}); });
match diesel::replace_into(organizations::table) diesel::replace_into(organizations::table)
.values(&*self) .values(&*self).execute(&**conn).and(Ok(()))
.execute(&**conn) {
Ok(1) => true, // One row inserted
_ => false,
}
} }
pub fn delete(self, conn: &DbConn) -> QueryResult<()> { 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 { if self.org_uuid == Organization::VIRTUAL_ID {
return false return Err(diesel::result::Error::NotFound)
} }
User::update_uuid_revision(&self.user_uuid, conn); User::update_uuid_revision(&self.user_uuid, conn);
match diesel::replace_into(users_organizations::table) diesel::replace_into(users_organizations::table)
.values(&*self) .values(&*self).execute(&**conn).and(Ok(()))
.execute(&**conn) {
Ok(1) => true, // One row inserted
_ => false,
}
} }
pub fn delete(self, conn: &DbConn) -> QueryResult<()> { pub fn delete(self, conn: &DbConn) -> QueryResult<()> {