public bool UpdateMenu(MenuViewModel menuViewModel, out string reason) { reason = string.Empty; var menu = _demandDbContext.Menus.FirstOrDefault(item => item.Id == menuViewModel.Id); if (menu == null) { reason = "未查找到该功能菜单"; return(false); } //仅名称变了 才需要判断重复 if (menuViewModel.Name != menu.Name) { var sameNameMenu = _demandDbContext.Menus.FirstOrDefault(item => item.Name == menuViewModel.Name); if (sameNameMenu != null) { reason = "已存在名称:" + sameNameMenu.Name; return(false); } } var newMenu = AutoMapper.Mapper.Map <Menu>(menuViewModel); EntityUpdateHelper.EntityToEntity(newMenu, menu); _demandDbContext.SaveChanges(); return(true); }
public virtual async Task <IActionResult> Create(TE entity) { if (ModelState.IsValid) { await SetOrderIndexIfNeeded(entity); EntityUpdateHelper <TE> .ClearNullRelatedEntities(entity); await Repo.Add(entity); return(RedirectToAction(nameof(Index))); } return(View(entity)); }
public virtual async Task <IActionResult> Edit(string id, FileEntity entity) { if (id != entity.Id) { return(NotFound()); } if (ModelState.IsValid) { var existing = await Repo.GetOne(id); if (existing == null) { return(NotFound()); } var tempFile = JsonConvert.DeserializeObject <FileEntity>(JsonConvert.SerializeObject(existing)); tempFile.Protected = entity.Protected; tempFile.Description = entity.Description; try { var epb = EntityUpdateHelper <FileEntity> .GetEpbFor(tempFile, existing); var updated = await Repo.Patch(epb); } catch (DbUpdateConcurrencyException) { if (!await Repo.Exists(id)) { return(NotFound()); } throw; } return(RedirectToAction(nameof(Index))); } return(View(entity)); }
public bool UpdateRole(RoleViewModel roleViewModel, out string reason) { reason = string.Empty; if (roleViewModel.Id == 1) { reason = "管理员角色不允许修改"; return(false); } var role = _demandDbContext.Roles.Include(item => item.RoleMenus).FirstOrDefault(item => item.Id == roleViewModel.Id); if (role == null) { reason = "未查找到该角色"; return(false); } //仅名称变了 才需要判断重复 if (roleViewModel.Name != role.Name) { var sameNameRole = _demandDbContext.Roles.FirstOrDefault(item => item.Name == roleViewModel.Name); if (sameNameRole != null) { reason = "已存在名称:" + roleViewModel.Name; return(false); } } //foreach (var rolemenu in role.RoleMenus) //{ // _demandDbContext.RoleMenus.Remove(rolemenu); //} role.RoleMenus.Clear(); _demandDbContext.SaveChanges(); var newRole = AutoMapper.Mapper.Map <Role>(roleViewModel); EntityUpdateHelper.EntityToEntity(newRole, role); role.CreateTime = DateTime.Now; _demandDbContext.SaveChanges(); return(true); }
public virtual async Task <IActionResult> Edit(string id, TE entity) { if (id != entity.Id) { return(NotFound()); } if (ModelState.IsValid) { var existing = await Repo.GetOne(id); if (existing == null) { return(NotFound()); } try { var epb = EntityUpdateHelper <TE> .GetEpbFor(entity, existing); var updated = await Repo.Patch(epb); } catch (DbUpdateConcurrencyException) { if (!await EntityExists(entity.Id)) { return(NotFound()); } throw; } return(RedirectToAction(nameof(Index))); } return(View(entity)); }