public ActionResult Create(UserRoleParameter parameters) { var viewModel = MapFromClientParameters(parameters); var shareViewModel = viewModel.SharedViewModel as DashboardUserRoleShareViewModel; if (shareViewModel != null) { var listRoleFunctionUpdate = shareViewModel.CheckAll ? GetAllRoleFunction() : ProcessMappingFromUserRoleGrid(shareViewModel.UserRoleFunctionData); var listRoleFunctionOld = new List <UserRoleFunction>(); // Check user have edit some value in list role old => delete role old and add role new foreach (var oldItem in listRoleFunctionOld) { if (listRoleFunctionUpdate.Any(o => o.DocumentTypeId == oldItem.DocumentTypeId)) { oldItem.IsDeleted = true; } } //after check user removed, remove item of the list new with conditions has property IsDelete equal true; //Copy listRoleFunctionUpdate var listRoleFunctionUpdateRecheck = listRoleFunctionUpdate.ToList(); foreach (var item in listRoleFunctionUpdateRecheck.Where(item => item.IsDeleted)) { listRoleFunctionUpdate.Remove(item); } var objListFunctionForEntity = new List <UserRoleFunction>(); // Add listUpdate objListFunctionForEntity.AddRange(listRoleFunctionUpdate); // Add list data in old foreach (var itemOld in listRoleFunctionOld.Where(o => !o.IsDeleted)) { var objAdd = new UserRoleFunction { DocumentTypeId = itemOld.DocumentTypeId, SecurityOperationId = itemOld.SecurityOperationId }; objListFunctionForEntity.Add(objAdd); } var entity = shareViewModel.MapTo <UserRole>(); entity.UserRoleFunctions = objListFunctionForEntity; var savedEntity = MasterFileService.Add(entity); MenuExtractData.Instance.RefershListData(); return(Json(new { savedEntity.Id }, JsonRequestBehavior.AllowGet)); } return(Json(new { Id = 0 }, JsonRequestBehavior.AllowGet)); }
public ActionResult Update(UserRoleParameter parameters) { var viewModel = MapFromClientParameters(parameters); byte[] lastModified = null; var shareViewModel = viewModel.SharedViewModel as DashboardUserRoleShareViewModel; if (shareViewModel != null) { var entity = MasterFileService.GetById(shareViewModel.Id); //if (entity.AppRoleName == AppRole.GlobalAdmin.ToString()) //{ // throw new Exception("Not allow to change global admin role function"); //} var listRoleFunctionUpdate = shareViewModel.CheckAll ? GetAllRoleFunction() : ProcessMappingFromUserRoleGrid(shareViewModel.UserRoleFunctionData); var mappedEntity = shareViewModel.MapPropertiesToInstance(entity); var listRoleFunctionOld = mappedEntity.UserRoleFunctions; // Check user have edit some value in list role old => delete role old and add role new foreach (var oldItem in listRoleFunctionOld) { if (listRoleFunctionUpdate.Any(o => o.DocumentTypeId == oldItem.DocumentTypeId)) { oldItem.IsDeleted = true; } } //after check user removed, remove item of the list new with conditions has property IsDelete equal true; //Copy listRoleFunctionUpdate var listRoleFunctionUpdateRecheck = listRoleFunctionUpdate.ToList(); foreach (var item in listRoleFunctionUpdateRecheck.Where(item => item.IsDeleted)) { listRoleFunctionUpdate.Remove(item); } // Add listUpdate mappedEntity.UserRoleFunctions.AddRange(listRoleFunctionUpdate); if (ModelState.IsValid) { mappedEntity = MasterFileService.Update(mappedEntity); lastModified = mappedEntity.LastModified; MenuExtractData.Instance.RefershListData(); } } return(Json(new { Error = string.Empty, Data = new { LastModified = lastModified } }, JsonRequestBehavior.AllowGet)); }