public bool LeaveGroup(GroupLeaveRAO rao) { using (var ctx = new ApplicationDbContext()) { var group = ctx.Groups.FirstOrDefault(g => g.GroupInviteKey == rao.GroupInviteKey); var groupMemberCount = ctx.GroupMembers.Where(gm => gm.GroupId == rao.GroupId).Count(); if (group == null) { return(false); } else if (group.OwnerId == _userId && groupMemberCount > 1) { return(false); } int changes = 0; var groupMember = ctx.GroupMembers.FirstOrDefault(gm => gm.MemberId == _userId && gm.GroupId == rao.GroupId); ctx.GroupMembers.Remove(groupMember); changes++; if (groupMemberCount == 1) { ctx.Groups.Remove(group); changes++; } return(ctx.SaveChanges() == changes); } }
public ActionResult Leave(int id) { var svc = GetGroupService(); var group = svc.GetGroupInfo(id); var rao = new GroupLeaveRAO { GroupId = group.GroupId, GroupInviteKey = group.GroupInviteKey }; if (svc.LeaveGroup(rao)) { return(RedirectToAction("MyGroups")); } TempData["FailResult"] = "Cannot leave group."; return(RedirectToAction("Index", new { id })); }
public IHttpActionResult LeaveGroup(GroupLeaveDTO dto) { var service = GetGroupService(); var rao = new GroupLeaveRAO { GroupId = dto.GroupId, GroupInviteKey = dto.GroupInviteKey }; if (service.LeaveGroup(rao)) { return(Ok()); } else { return(BadRequest()); } }