public async Task <IActionResult> AddGroupMembers(string groupRef, [FromBody] JArray memberRefs) { if (memberRefs.Count == 0) { throw Errors.GenericBadRequest; } var system = await ResolveSystem("@me"); var group = await ResolveGroup(groupRef); if (group == null) { throw Errors.GroupNotFound; } if (group.System != system.Id) { throw Errors.NotOwnGroupError; } var members = new List <MemberId>(); foreach (var JmemberRef in memberRefs) { var memberRef = JmemberRef.Value <string>(); var member = await ResolveMember(memberRef, cache : true); // todo: have a list of these errors instead of immediately throwing if (member == null) { throw Errors.MemberNotFoundWithRef(memberRef); } if (member.System != system.Id) { throw Errors.NotOwnMemberErrorWithRef(memberRef); } members.Add(member.Id); } var existingMembers = await _repo.GetGroupMembers(group.Id).Select(x => x.Id).ToListAsync(); members = members.Where(x => !existingMembers.Contains(x)).ToList(); if (members.Count > 0) { await _repo.AddMembersToGroup(group.Id, members); } return(NoContent()); }
public async Task <IActionResult> OverwriteGroupMembers(string groupRef, [FromBody] JArray memberRefs) { var system = await ResolveSystem("@me"); var group = await ResolveGroup(groupRef); if (group == null) { throw Errors.GroupNotFound; } if (group.System != system.Id) { throw Errors.NotOwnGroupError; } var members = new List <MemberId>(); foreach (var JmemberRef in memberRefs) { var memberRef = JmemberRef.Value <string>(); var member = await ResolveMember(memberRef, cache : true); if (member == null) { throw Errors.MemberNotFoundWithRef(memberRef); } if (member.System != system.Id) { throw Errors.NotOwnMemberErrorWithRef(memberRef); } members.Add(member.Id); } await _repo.ClearGroupMembers(group.Id); if (members.Count > 0) { await _repo.AddMembersToGroup(group.Id, members); } return(NoContent()); }
public async Task <IActionResult> RemoveGroupMembers(string groupRef, [FromBody] JArray memberRefs) { if (memberRefs.Count == 0) { throw Errors.GenericBadRequest; } var system = await ResolveSystem("@me"); var group = await ResolveGroup(groupRef); if (group == null) { throw Errors.GroupNotFound; } if (group.System != system.Id) { throw Errors.NotOwnGroupError; } var members = new List <MemberId>(); foreach (var JmemberRef in memberRefs) { var memberRef = JmemberRef.Value <string>(); var member = await ResolveMember(memberRef); if (member == null) { throw Errors.MemberNotFoundWithRef(memberRef); } if (member.System != system.Id) { throw Errors.NotOwnMemberErrorWithRef(memberRef); } members.Add(member.Id); } await _repo.RemoveMembersFromGroup(group.Id, members); return(NoContent()); }