protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); Database.SetInitializer<HMContext>(null); var domainUserProvider = NinjectWebCommon.Kernel.GetService(typeof(IDomainUserProvider)) as IDomainUserProvider; var hmUnitOfWork = NinjectWebCommon.Kernel.GetService(typeof(IHMUnitOfWork)) as IHMUnitOfWork; var adminADName = ConfigurationManager.AppSettings["AdminADName"]; var adminUserRole = hmUnitOfWork.RoleRepository.FindBy(x => x.Name == "AdminUser").SingleOrDefault(); var normalUserRole = hmUnitOfWork.RoleRepository.FindBy(x => x.Name == "NormalUser").SingleOrDefault(); foreach (var domainUser in domainUserProvider.GetAllUsers()) { var employee = hmUnitOfWork.EmployeeRepository.FindBy(x => x.ADName == domainUser.Name).SingleOrDefault(); if (employee == null) { employee = new Employee { FirstName = domainUser.Name, LastName = domainUser.Name, ADName = domainUser.Name, EmailAddress = domainUser.EmailAddress, RoleId = domainUser.Name == adminADName ? adminUserRole.Id : normalUserRole.Id }; hmUnitOfWork.EmployeeRepository.Add(employee); var holidayInformation = new HolidayInformation { DaysAvailable = 21, Year = DateTime.Now.Year, Employee = employee }; hmUnitOfWork.HolidayInformationRepository.Add(holidayInformation); } } hmUnitOfWork.Save(); }
public virtual ActionResult EditEmployee(EmployeeViewModel employeeVM) { if (!ModelState.IsValid) { return Json(new { success = false, message = "Data is not valid." }); } var employee = _hmUnitOfWork.EmployeeRepository.GetSingle(employeeVM.Id); if (employee == null) { return Json(new { success = false, message = "Employee is not found." }); } employee.FirstName = employeeVM.FirstName; employee.LastName = employeeVM.LastName; employee.EmailAddress = employeeVM.EmailAddress; employee.HireDate = employeeVM.HireDate; employee.TeamId = employeeVM.TeamId; var currentHolidayInformation = employee.HolidayInformations.SingleOrDefault(x => x.Year == DateTime.Now.Year); if (currentHolidayInformation == null) { currentHolidayInformation = new HolidayInformation { DaysAvailable = employeeVM.DaysAvailable.Value, Employee = employee }; _hmUnitOfWork.HolidayInformationRepository.Add(currentHolidayInformation); } else currentHolidayInformation.DaysAvailable = employeeVM.DaysAvailable.Value; _hmUnitOfWork.Save(); return Json(new { success = true }); }