public JsonResult <object> SelectetGroupsDataHandler(PostSelectedGroupDTViewModel param) { try { return(Json((object)PostingService.SelectetGroupsDataHandler(param, User.Identity.GetUserId()))); } catch (Exception ex) { return(Json((object)(new { error = ex.Message }))); } }
public DTResult <PostSelectedGroupViewModel> SelectetGroupsDataHandler(PostSelectedGroupDTViewModel param, string userId) { try { IQueryable <PostSelectedGroupViewModel> groups = null; if (param.PostId > 0) { var post = PostRepository.Find(p => p.Id == param.PostId && p.SocialNetworkId == param.SocialNetworkId && p.UserId == userId && p.Status != EntityStatus.Disabled); if (post != null) { var groupsIds = post.GroupsOfLinks.Select(g => g.Id).ToList(); foreach (var id in groupsIds) { if (!param.GroupsIds.Contains(id)) { var group = post.GroupsOfLinks.FirstOrDefault(w => w.Id == id); if (group != null) { post.GroupsOfLinks.Remove(group); } } } groupsIds = post.GroupsOfLinks.Select(g => g.Id).ToList(); foreach (var id in param.GroupsIds) { if (!groupsIds.Contains(id)) { var group = post.User.GroupsOfLinks.FirstOrDefault(w => w.SocialNetwork.Id == post.SocialNetworkId && w.Status == EntityStatus.Active && w.Id == id); if (group != null) { post.GroupsOfLinks.Add(group); } else { throw new Exception("Group is not found"); } } } PostRepository.Update(post); groupsIds = post.GroupsOfLinks.Select(g => g.Id).ToList(); groups = post.User.GroupsOfLinks.Where(w => w.SocialNetwork.Id == post.SocialNetworkId && w.Status == EntityStatus.Active) .Select(g => new PostSelectedGroupViewModel { Id = g.Id, Name = g.Name, Selection = groupsIds.Contains(g.Id) }).AsQueryable(); } else { throw new Exception("Post is not found"); } } else { var user = UserRepository.Find(u => u.Id == userId && u.Status == EntityStatus.Active); if (user != null) { groups = user.GroupsOfLinks.Where(w => w.SocialNetwork.Id == param.SocialNetworkId && w.Status == EntityStatus.Active) .Select(g => new PostSelectedGroupViewModel { Id = g.Id, Name = g.Name, Selection = false }).AsQueryable(); } else { throw new Exception("Out of permission"); } } List <String> columnFilters = new List <string>(); foreach (var col in param.Columns) { columnFilters.Add(col.Search.Value); } var search = param.Search.Value.ToLower(); var col2 = columnFilters[2]; Expression <Func <PostSelectedGroupViewModel, bool> > predicate = p => (search == null || (p.Name != null && p.Name.ToLower().Contains(search)) && (col2 == null || (p.Name != null && p.Name.ToLower().Contains(col2.ToLower())))); List <PostSelectedGroupViewModel> data = new ResultSet <PostSelectedGroupViewModel>().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, groups, predicate); int count = new ResultSet <PostSelectedGroupViewModel>().Count(param.Search.Value, groups, predicate); DTResult <PostSelectedGroupViewModel> result = new DTResult <PostSelectedGroupViewModel> { draw = param.Draw, data = data.Select(p => new PostSelectedGroupViewModel { Id = p.Id, Name = p.Name, Selection = p.Selection }).ToList(), recordsFiltered = count, recordsTotal = count }; return(result); } catch (Exception ex) { throw ex; } }