public async Task <ActionResult <MessageAndStatus> > DeleteUserPermission(int listAggregationId, [FromBody] UserPermissionToListAggregation item) { var user = await _context.Users.Where(a => a.EmailAddress == item.User.EmailAddress).FirstOrDefaultAsync(); if (user == null) { return new MessageAndStatus { Status = "ERROR", Message = "User not exist." } } ; var count = await _context.UserListAggregators.Where(a => a.ListAggregatorId == listAggregationId && a.PermissionLevel == 1).CountAsync(); UserListAggregatorEntity lastAdmin = null; if (count == 1) { lastAdmin = await _context.UserListAggregators.Where(a => a.ListAggregatorId == listAggregationId && a.PermissionLevel == 1).SingleAsync(); } if (count == 1 && user.UserId == lastAdmin.UserId) { return new MessageAndStatus { Status = "ERROR", Message = "Only one Admin left - not delete." } } ; var userListAggr = await _context.UserListAggregators.Where(a => a.User.UserId == item.User.UserId && a.ListAggregatorId == listAggregationId) .FirstOrDefaultAsync(); if (userListAggr == null) { return new MessageAndStatus { Status = "ERROR", Message = "User permission not found." } } ; else { _context.Remove(userListAggr); } await _context.SaveChangesAsync(); await _mediator.Publish(new DataChangedEvent(new int[] { user.UserId })); return(new MessageAndStatus { Status = "OK", Message = "User permission was deleted." }); }
public async Task <ActionResult <MessageAndStatus> > GetInvitationsList(string userName) { var list_Invi_Aggr = from invi in _context.Invitations where invi.EmailAddress == userName join aggr in _context.ListAggregators on invi.ListAggregatorId equals aggr.ListAggregatorId into invi_aggr from aggrOrNull in invi_aggr.DefaultIfEmpty() select new { Invitation = invi, ListAggr = aggrOrNull }; // List<InvitationEntity> toDeleteEntity = new List<InvitationEntity>(); foreach (var item in list_Invi_Aggr) { if (item.ListAggr == null) { _context.Remove(item.Invitation); //toDeleteEntity.Add(item.Invitation); } } await _context.SaveChangesAsync(); var listInviAggr = await list_Invi_Aggr.ToListAsync(); var invitationsList = listInviAggr.Select(a => { var inv = _mapper.Map <Invitation>(a.Invitation); inv.ListAggregatorName = a.ListAggr.ListAggregatorName; return(inv); }).ToList(); return(new MessageAndStatus { Status = "OK", Message = JsonConvert.SerializeObject(invitationsList) }); }