public List <UserViewModel> GetSortedUsers(string role) { //Instance of the ManageRoles class to get the user's role ManageRoles man = new ManageRoles(); //List of view models to return List <UserViewModel> viewModel = new List <UserViewModel>(); //A list of all the users in the database List <ApplicationUser> users = _db.Users.ToList(); //Get all the users from the list with the right role and adding them to te view model foreach (var tmp in users) { if (man.UserIsInRole(tmp.Id, role)) { viewModel.Add(new UserViewModel() { Id = tmp.Id, FullName = tmp.FullName, SSN = tmp.SSN, Email = tmp.Email, UserRole = man.GetUserRole(tmp.Email) }); } ; } //Return the view model return(viewModel); }
public UserViewModel GetUserBySSN(string userSSN) { //Instance of the ManageRoles class to get the user's role ManageRoles man = new ManageRoles(); //Get the right user from the database var user = _db.Users.Where(x => x.SSN == userSSN).FirstOrDefault(); //if the user exists add him to a view model and return it, otherwise throw exception if (user != null) { var viewModel = new UserViewModel { FullName = user.FullName, SSN = user.SSN, Email = user.Email, UserRole = man.GetUserRole(user.Email) }; return(viewModel); } else { throw new ArgumentNullException(); } }
public void EditUserBySSN(UserViewModel user) { //Instance of the ManageRoles class to get the user's role ManageRoles man = new ManageRoles(); //Find the right user var model = _db.Users.Where(x => x.SSN == user.SSN).FirstOrDefault(); //If the user dosn't exist throw an exception otherwise change the user information if (model == null) { //TODO: kasta villu throw new ArgumentNullException(); } else { model.FullName = user.FullName; model.SSN = user.SSN; model.Email = user.Email; if (man.GetUserRole(model.Email) != null) { man.ClearUserRoles(model.Id); } man.AddUserToRole(model.Id, user.UserRole); } try { _db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var error in ex.EntityValidationErrors) { Console.WriteLine("===================="); Console.WriteLine("Entity {0} in state {1} has validation errors:", error.Entry.Entity.GetType().Name, error.Entry.State); foreach (var ve in error.ValidationErrors) { Console.WriteLine("\tProperty: {0}, Error: {1}", ve.PropertyName, ve.ErrorMessage); } Console.WriteLine(); } throw; } }