public async Task <UserGroupAC> GetGroupByIdAsync(string userId, int groupId) { var groups = await _context.UserGroupMappers.Where(u => u.UserID.Equals(userId)).Where(u => u.GroupID == groupId).Include(u => u.Groups).Include(u => u.Groups.UserGroupMappers).ThenInclude(u => u.Users).Where(g => g.Groups.IsDeleted == false).FirstOrDefaultAsync(); var selectedGroup = new UserGroupAC(); foreach (var group in groups.Groups.UserGroupMappers) { selectedGroup.GroupID = groups.GroupID; selectedGroup.GroupName = groups.Groups.Name; selectedGroup.CreatedByID = groups.Groups.CreatedBy; selectedGroup.CreatedBy = _userManager.FindByIdAsync(group.Groups.CreatedBy).Result.FullName; selectedGroup.DateCreated = groups.Groups.DateCreated; selectedGroup.Category = groups.Groups.Category; selectedGroup.Note = groups.Groups.Note; selectedGroup.SimplifyDebts = groups.Groups.SimplifyDebts; selectedGroup.GroupUsers = new List <UserAC>(); foreach (var user in group.Groups.UserGroupMappers) { selectedGroup.GroupUsers.Add(new UserAC { Id = user.UserID, FullName = user.Users.FullName, UserName = user.Users.UserName }); } } return(selectedGroup); }
public async Task <IEnumerable <UserGroupAC> > GetAllGroupsAsync(string userId) { var groups = await _context.UserGroupMappers.Where(u => u.UserID.Equals(userId)).Include(u => u.Groups).Where(g => g.Groups.IsDeleted == false).Include(u => u.Groups.UserGroupMappers).ThenInclude(u => u.Users).ToListAsync(); var groupList = new List <UserGroupAC>(); foreach (var group in groups) { var createdBy = _userManager.FindByIdAsync(group.Groups.CreatedBy); var groupAC = new UserGroupAC { GroupID = group.GroupID, GroupName = group.Groups.Name, CreatedBy = createdBy.Result.FullName, DateCreated = group.Groups.DateCreated, Category = group.Groups.Category, Note = group.Groups.Note, SimplifyDebts = group.Groups.SimplifyDebts, GroupUsers = new List <UserAC>() }; foreach (var user in group.Groups.UserGroupMappers) { groupAC.GroupUsers.Add(new UserAC { Id = user.UserID, FullName = user.Users.FullName, UserName = user.Users.UserName }); } groupList.Add(groupAC); } return(groupList); }
public async Task <Boolean> EditGroupAsync(UserGroupAC group) { DomainModel.Models.Group groupToEdit = await _context.Groups.Where(g => g.ID == group.GroupID).FirstOrDefaultAsync(); List <UserGroupMapper> userGroup = await _context.UserGroupMappers.Include(u => u.Users).Where(g => g.GroupID == group.GroupID).ToListAsync(); var allUsers = await _context.Users.ToListAsync(); var newUserGroupMapper = new List <UserGroupMapper>(); foreach (var users in group.GroupUsers) { newUserGroupMapper.Add(new UserGroupMapper { GroupID = group.GroupID, UserID = allUsers.Where(u => u.UserName.Equals(users.UserName)).Select(u => u.Id).FirstOrDefault() }); } if (groupToEdit != null) { groupToEdit.Name = group.GroupName; groupToEdit.Category = group.Category; groupToEdit.SimplifyDebts = group.SimplifyDebts; groupToEdit.Note = group.Note; groupToEdit.UserGroupMappers = newUserGroupMapper; _context.Groups.Update(groupToEdit); return(true); } return(false); }
public async Task <IActionResult> EditGroupAsync([FromBody] UserGroupAC group) { var returnResult = await unitOfWork.Group.EditGroupAsync(group); if (returnResult) { await unitOfWork.SaveAsync(); return(Ok(group)); } return(NotFound("Group not found")); }
public override void OnActionExecuting(ActionExecutingContext context) { string controllerName = (string)context.RouteData.Values["Controller"]; string actionName = (string)context.RouteData.Values["Action"]; string currentUserId = _userManager.FindByNameAsync(context.HttpContext.User.Identity.Name).Result.Id; if (controllerName.Equals("Group")) { List <Group> groupList = _context.Groups.ToList(); if (actionName.Equals("EditGroupAsync")) { UserGroupAC model = context.ActionArguments["group"] as UserGroupAC; if (currentUserId.Equals(model.CreatedByID)) { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Accepted; } else { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; context.Result = new EmptyResult(); } } else if (actionName.Equals("DeleteGroupAsync")) { int groupId = (int)context.ActionArguments["groupId"]; if ((groupList.Where(g => g.ID == groupId).Select(g => g.CreatedBy).FirstOrDefault()).Equals(currentUserId)) { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Accepted; } else { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; context.Result = new EmptyResult(); } } } else if (controllerName.Equals("Expense")) { if (actionName.Equals("EditExpenseAsync")) { int groupId = (int)context.ActionArguments["groupId"]; IndividualExpenseAC expense = context.ActionArguments["expense"] as IndividualExpenseAC; if (expense.Payer.Any(p => p.UserID.Equals(currentUserId))) { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Accepted; } else { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; context.Result = new EmptyResult(); } } else if (actionName.Equals("RemoveExpenseAsync")) { int groupId = (int)context.ActionArguments["groupId"]; int expenseId = (int)context.ActionArguments["expenseId"]; List <string> payersId = _context.UserExpenseMappers.Where(e => e.ExpenseID == expenseId).Select(e => e.FromUser).ToList(); if (payersId.Contains(currentUserId)) { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Accepted; } else { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; context.Result = new EmptyResult(); } } else if (actionName.Equals("SettlementAsync")) { SettlementAC settlement = context.ActionArguments["settlement"] as SettlementAC; if (settlement.Payee.Equals(currentUserId) || settlement.Payer.Equals(currentUserId)) { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Accepted; } else { context.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized; context.Result = new EmptyResult(); } } } base.OnActionExecuting(context); }