public static MenuRoleMapVM GetDTO(MenuRoleMap menuRoleMap) { MenuRoleMapVM menuRoleMapVM = null; if (menuRoleMap != null) { menuRoleMapVM = new MenuRoleMapVM() { Id = menuRoleMap.Id, IsActive = menuRoleMap.IsActive, RoleVM = RoleVM.GetDTO(menuRoleMap.Role), MenuVM = MenuVM.GetDTO(menuRoleMap.Menu) }; } return(menuRoleMapVM); }
public ActionResult UpdateMenus() { MenuVM v1 = new MenuVM() { MenuName = "Home", Controller = "Home", Action = "Index", Order = 1, IsParent = false }; MenuVM v2 = new MenuVM() { MenuName = "Role", Controller = "Tech", Action = "Role", Order = 2, IsParent = false }; MenuVM v3 = new MenuVM() { MenuName = "Menu", Controller = "", Action = "", Order = 3, IsParent = true }; MenuVM v4 = new MenuVM() { MenuName = "View", Controller = "Tech", Action = "Menu", Order = 4, IsParent = false }; v4.MainMenuVM = v3; MenuVM v5 = new MenuVM() { MenuName = "Manage Menu", Controller = "Tech", Action = "ManageMenu", Order = 5, IsParent = false }; v5.MainMenuVM = v3; MenuVM v6 = new MenuVM() { MenuName = "Employees", Controller = "Employee", Action = "Index", Order = 6, IsParent = false }; List <MenuVM> menus = new List <MenuVM>() { v1, v2, v3, v4, v5, v6 }; foreach (var menu in menus) { var newMenu = GetSearchedMenu(menu); if (newMenu == null) { newMenu = new Menu(menu.MenuName, menu.Controller, menu.Action, menu.Order); _menuRepository.Add(newMenu); _menuRepository.Save(); } if (menu.MainMenuVM != null) { var mainMenu = GetSearchedMenu(menu.MainMenuVM); if (mainMenu == null) { mainMenu = new Menu(menu.MainMenuVM.MenuName, menu.MainMenuVM.Controller, menu.MainMenuVM.Action, menu.MainMenuVM.Order); _menuRepository.Add(mainMenu); _menuRepository.Save(); } newMenu.MainMenu = mainMenu; _menuRepository.Save(); } newMenu.Order = menu.Order; newMenu.IsParent = menu.IsParent; _menuRepository.Save(); } var roleList = _roleRepository.Get().ToList(); var menuList = _menuRepository.Get().ToList(); foreach (var role in roleList) { foreach (var menu in menuList) { var criteria = new MenuRoleMapCriteria { RoleId = role.Id, MenuId = menu.Id }; var specification = new MenuRoleMapSpecification(criteria); var menuRoleMap = _menuRoleMapRepository.Find(specification).FirstOrDefault(); if (menuRoleMap == null) { menuRoleMap = new MenuRoleMap(); menuRoleMap.Role = role; menuRoleMap.Menu = menu; _menuRoleMapRepository.Add(menuRoleMap); _menuRoleMapRepository.Save(); } } } return(RedirectToAction("Menu")); }