public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); Role role = null; if (!this.ModelState.IsValid) { AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context); var authenticatingActions = authenticatingActionsRepository.GetAll(); List<AuthenticatingActionsVM> authenticatingActionsViewModel = new List<AuthenticatingActionsVM>(); foreach (var action in authenticatingActions) { authenticatingActionsViewModel.Add(new AuthenticatingActionsVM { ID = action.ID, Name = action.Name, IsAssigned = false }); } ViewBag.AuthenticatingActions = authenticatingActionsViewModel; return View(model); } using (UnitOfWork unitOfWork = new UnitOfWork(context)) { try { var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork); var rolesRepository = new RolesRepository(unitOfWork); if (model.ID > 0) { role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault(); } else { role = new Role(); role.AuthenticatingActions = new List<AuthenticatingAction>(); } role.Name = model.Name; UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository); rolesRepository.Save(role); PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.RollBack(); throw ex; } } return RedirectToAction("Index", "Roles"); }
public ActionResult EditUser(UsersEditUserVM model, string[] assignedRoles) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); ModelState.Remove("DateOut"); UsersRepository usersRepositoryContext = new UsersRepository(context); if (model.Email != null && usersRepositoryContext.GetAll().Any(u => u.Email == model.Email) && model.ID != usersRepositoryContext.GetAll(filter: u => u.Email == model.Email).FirstOrDefault().ID) { ModelState.AddModelError("Email", "* email already exists"); } if (model.ID <= 0 && string.IsNullOrEmpty(model.Password)) { this.ModelState.AddModelError("Password", "* password required"); } if (!this.ModelState.IsValid) { RolesRepository rolesRepository = new RolesRepository(context); var allRoles = rolesRepository.GetAll(); List<AssignedRolesVM> assignedRolesViewModel = new List<AssignedRolesVM>(); foreach (var role in allRoles) { assignedRolesViewModel.Add(new AssignedRolesVM { ID = role.ID, Name = role.Name, IsAssigned = false }); } ViewBag.Roles = assignedRolesViewModel; return View(model); } TryUpdateModel(model); User user = null; using (UnitOfWork unitOfWork = new UnitOfWork(context)) { try { UsersRepository usersRepositoryUnitOfWork = new UsersRepository(unitOfWork); RolesRepository rolesRepository = new RolesRepository(unitOfWork); if (model.ID > 0) { user = usersRepositoryUnitOfWork.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault(); user.PersonalNumber = model.PersonalNumber; } else { user = new User(); user.Roles = new List<Role>(); user.PersonalNumber = usersRepositoryUnitOfWork.GetAll().LastOrDefault().PersonalNumber + 1; } user.Password = (model.Password != null) && (model.Password.Trim() != String.Empty) ? model.Password.Trim() : user.Password; user.FirstName = model.FirstName; user.Email = model.Email; user.Address = model.Address; user.LastName = model.LastName; user.Birthday = model.Birthday; user.DateIn = model.DateIn; user.DateOut = model.DateOut != null ? model.DateOut : null; UpdateUserRoles(assignedRoles, user, rolesRepository); usersRepositoryUnitOfWork.Save(user); PopulateAssignedRoles(user, rolesRepository); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.RollBack(); throw ex; } } return RedirectToAction("Index", "Users"); }