Browse Source

collection endpoint support for groups implemented

pull/2667/head
MFijak 3 years ago
committed by Maximilian Fijak
parent
commit
11cc5a79b7
  1. 41
      src/api/core/organizations.rs
  2. 28
      src/db/models/group.rs

41
src/api/core/organizations.rs

@ -98,10 +98,19 @@ struct OrganizationUpdateData {
Name: String,
}
#[derive(Deserialize, Debug)]
#[derive(Deserialize)]
#[allow(non_snake_case)]
struct NewCollectionData {
Name: String,
Groups: Vec<NewCollectionGroupData>,
}
#[derive(Deserialize)]
#[allow(non_snake_case)]
struct NewCollectionGroupData {
HidePasswords: bool,
Id: String,
ReadOnly: bool,
}
#[derive(Deserialize)]
@ -338,6 +347,13 @@ async fn post_organization_collection_update(
collection.name = data.Name;
collection.save(&conn).await?;
CollectionGroup::delete_all_by_collection(&col_id, &conn).await?;
for group in data.Groups {
CollectionGroup::new(col_id.clone(), group.Id, group.ReadOnly, group.HidePasswords)
.save(&conn).await?;
}
Ok(Json(collection.to_json()))
}
@ -433,7 +449,16 @@ async fn get_org_collection_detail(
err!("Collection is not owned by organization")
}
Ok(Json(collection.to_json()))
let groups: Vec<Value> = CollectionGroup::find_by_collection(&collection.uuid, &conn).await
.iter()
.map(|collection_group| SelectionReadOnly::to_collection_group_details_read_only(collection_group).to_json())
.collect();
let mut json_object = collection.to_json();
json_object["Groups"] = json!(groups);
json_object["Object"] = json!("collectionGroupDetails");
Ok(Json(json_object))
}
}
}
@ -1570,7 +1595,7 @@ impl SelectionReadOnly {
)
}
pub fn to_selection_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly {
pub fn to_group_details_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly {
SelectionReadOnly {
Id: collection_group.collections_uuid.clone(),
ReadOnly: collection_group.read_only,
@ -1578,6 +1603,14 @@ impl SelectionReadOnly {
}
}
pub fn to_collection_group_details_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly {
SelectionReadOnly {
Id: collection_group.groups_uuid.clone(),
ReadOnly: collection_group.read_only,
HidePasswords: collection_group.hide_passwords
}
}
pub fn to_json (&self) -> Value {
json!(self)
}
@ -1644,7 +1677,7 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea
let collection_groups = CollectionGroup::find_by_group(&group_id, &conn).await
.iter()
.map(|entry| SelectionReadOnly::to_selection_read_only(entry).to_json())
.map(|entry| SelectionReadOnly::to_group_details_read_only(entry).to_json())
.collect::<Value>();
Ok(Json(json!({

28
src/db/models/group.rs

@ -294,6 +294,17 @@ impl CollectionGroup {
}}
}
pub async fn find_by_collection(collection_uuid: &str, conn: &DbConn) -> Vec<Self> {
db_run! { conn: {
collection_groups::table
.filter(collection_groups::collections_uuid.eq(collection_uuid))
.select(collection_groups::all_columns)
.load::<CollectionGroupDb>(conn)
.expect("Error loading collection groups")
.from_db()
}}
}
pub async fn delete(&self, conn: &DbConn) -> EmptyResult {
let group_users = GroupUser::find_by_group(&self.groups_uuid, conn).await;
for group_user in group_users {
@ -322,6 +333,23 @@ impl CollectionGroup {
.map_res("Error deleting collection group")
}}
}
pub async fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> EmptyResult {
let collection_assigned_to_groups = CollectionGroup::find_by_collection(collection_uuid, conn).await;
for collection_assigned_to_group in collection_assigned_to_groups {
let group_users = GroupUser::find_by_group(&collection_assigned_to_group.groups_uuid, conn).await;
for group_user in group_users {
group_user.update_user_revision(conn).await;
}
}
db_run! { conn: {
diesel::delete(collection_groups::table)
.filter(collection_groups::collections_uuid.eq(collection_uuid))
.execute(conn)
.map_res("Error deleting collection group")
}}
}
}
impl GroupUser {

Loading…
Cancel
Save