public JsonData GetUsers(UserFilter filter) { try { using (var db = new DataContext()) { var roles = db.Roles.ToDictionary(x => x.Id); var data = filter.BuildQuery(db.Users).Include(x => x.Roles).ToList().OrderBy(x => x.Updated); if (!data.Any()) return DataHelpers.ReturnJsonData(null, false, "No Data Found", 0); var users = data.Select(myUser => new UserViewModel { UserName = myUser.UserName, FullName = myUser.FullName, Created = myUser.Created, Email = myUser.Email, PhoneNumber = myUser.PhoneNumber, DateOfBirth = myUser.DateOfBirth, Id = myUser.Id, IsActive = myUser.IsActive, Updated = myUser.Updated, Roles = roles.First(x=> x.Key == myUser.Roles.First().RoleId).Value.Name }).Where(x => !x.Roles.Contains("Administrator")).ToList(); return users.Any() ? DataHelpers.ReturnJsonData(users, true, "Loaded successfully", users.Count()) : DataHelpers.ReturnJsonData(users, false, "No Data Found", 0); } } catch (Exception e) { return DataHelpers.ExceptionProcessor(e); } }
public JsonData Get(string userId) { try { var filter = new UserFilter {UserId = userId}; using (var db = new DataContext()) { var roles = db.Roles.ToDictionary(x => x.Id); var data = filter.BuildQuery(db.Users).Include(x => x.Roles).First(); if (data == null) return DataHelpers.ReturnJsonData(null, false, "No Data Found", 0); var activeUser = new UserViewModel { UserName = data.UserName, FullName = data.FullName, Created = data.Created, Email = data.Email, PhoneNumber = data.PhoneNumber, DateOfBirth = data.DateOfBirth, Id = data.Id, IsActive = data.IsActive, Updated = data.Updated, Roles = roles.First(x => x.Key == data.Roles.First().RoleId).Value.Name }; return DataHelpers.ReturnJsonData(activeUser, true, "Loaded successfully", 1); } } catch (Exception e) { return DataHelpers.ExceptionProcessor(e); } }
public JsonData GetUsers(int page, int size) { var filter = new UserFilter { Pager = { Page = page, Size = size } }; return new UserRepo().GetUsers(filter); }