private void UpdateEmployeeLocations(string[] selectedLocations, EmployeeModel employeeToUpdate) { if (selectedLocations == null) { employeeToUpdate.EmployeeLocations = new List <EmployeeLocations>(); return; } var selectedLocationsHS = new HashSet <string>(selectedLocations); if (employeeToUpdate.EmployeeLocations != null) { var employeeLocations = new HashSet <int> (employeeToUpdate.EmployeeLocations.Select(c => c.Location.LocationID)); foreach (var location in _context.Locations) { if (selectedLocationsHS.Contains(location.LocationID.ToString())) { if (!employeeLocations.Contains(location.LocationID)) { employeeToUpdate.EmployeeLocations.Add(new EmployeeLocations { EmployeeModelID = employeeToUpdate.ID, LocationID = location.LocationID, Employee = employeeToUpdate, Location = location }); } } else { if (employeeLocations.Contains(location.LocationID)) { EmployeeLocations locationToRemove = employeeToUpdate.EmployeeLocations.FirstOrDefault(i => i.LocationID == location.LocationID); _context.Remove(locationToRemove); } } } } else { employeeToUpdate.EmployeeLocations = new List <EmployeeLocations>(); foreach (var location in _context.Locations) { if (selectedLocationsHS.Contains(location.LocationID.ToString())) { employeeToUpdate.EmployeeLocations.Add(new EmployeeLocations { EmployeeModelID = employeeToUpdate.ID, Employee = employeeToUpdate, LocationID = location.LocationID, Location = location }); } } } }
private static List <EmployeeLocations> SetEmployeeLocations() { int c = 1; foreach (EmployeeModel employee in employees) { EmployeeLocations el = new EmployeeLocations { EmployeeLocationsID = c, LocationID = 1, EmployeeModelID = employee.ID }; employeeLocations.Add(el); c++; } return(employeeLocations); }
public List <EmployeeLocations> SetEmployeeLocations(ApplicationDbContext context) { employees = context.EmployeeModel.ToList(); locations = context.Locations.ToList(); foreach (EmployeeModel employee in employees) { int t = 3; int range = locations.Count() - 1; for (int i = 0; i < t; i++) { int id = RandomID(range) + 1; EmployeeLocations el = new EmployeeLocations { LocationID = id, EmployeeModelID = employee.ID }; employeeLocations.Add(el); } counts.Clear(); } return(employeeLocations); }
/// <summary> /// Update employee locations /// </summary> /// <param name="selectedLocations">Locations selected by user</param> /// <param name="employeeToUpdate">Employee Model for updating</param> private void UpdateEmployeeLocations(string[] selectedLocations, EmployeeModel employeeToUpdate) { // of none selected, set to empty list and return if (selectedLocations == null) { employeeToUpdate.EmployeeLocations = new List<EmployeeLocations>(); return; } var selectedLocationsHS = new HashSet<string>(selectedLocations); // get hashed set of selected locations // if no existing locations create new list and set if (employeeToUpdate.EmployeeLocations != null) { // create hash set to store locations in var employeeLocations = new HashSet<int> (employeeToUpdate.EmployeeLocations.Select(c => c.Location.LocationID)); // go trough each location foreach (var location in _context.Locations) { // if location was selected add to employee locations if (selectedLocationsHS.Contains(location.LocationID.ToString())) { if (!employeeLocations.Contains(location.LocationID)) { // add locations employeeToUpdate.EmployeeLocations.Add(new EmployeeLocations { EmployeeModelID = employeeToUpdate.ID, LocationID = location.LocationID, Employee = employeeToUpdate, Location = location }); } } else { // remove location if not selected if (employeeLocations.Contains(location.LocationID)) { EmployeeLocations locationToRemove = employeeToUpdate.EmployeeLocations.FirstOrDefault(i => i.LocationID == location.LocationID); _context.Remove(locationToRemove); } } } } else { // cteate list of employee locations employeeToUpdate.EmployeeLocations = new List<EmployeeLocations>(); // go trough each location foreach (var location in _context.Locations) { // if location is selected add to users locations if (selectedLocationsHS.Contains(location.LocationID.ToString())) { employeeToUpdate.EmployeeLocations.Add(new EmployeeLocations { EmployeeModelID = employeeToUpdate.ID, Employee = employeeToUpdate, LocationID = location.LocationID, Location = location }); } } } }
public async Task<IActionResult> Create([Bind("ID,Name,LastName,DoB,Gender,EMail,PhoneNumber,Admin,LoginID,Experience,ProfileImage,SupervisorID")] EmployeeModel employeeModel, string[] selectedSkills, string[] selectedFunctions, string[] selectedLocations, EmployeeViewModel model) { // check if user is loggen in and user is an admin. if (signInManager.IsSignedIn(User) && _context.EmployeeModel.Single(x => x.EMail == User.Identity.Name).Admin) { //employeeModel = setSupervisor(employeeModel); if (selectedSkills != null) { // create list of skills employeeModel.EmployeeSkills = new List<EmployeeSkill>(); // assign selected skills foreach (var skill in selectedSkills) { // create employee skill and add to list var skillToAdd = new EmployeeSkill { EmployeeModelID = employeeModel.ID, Employee = employeeModel, SkillID = int.Parse(skill), Skill = _context.Skill.Single(s => s.SkillID == int.Parse(skill)) }; //add to list employeeModel.EmployeeSkills.Add(skillToAdd); } } // only if no list if selected functions is given if (selectedFunctions != null) { // get list of functions employeeModel.EmployeeFunctions = new List<EmployeeFunction>(); // populate selected functions foreach (var function in selectedFunctions) { var functionToAdd = new EmployeeFunction { EmployeeID = employeeModel.ID, Employee = employeeModel, FunctionID = int.Parse(function), Function = _context.Function.Single(s => s.FunctionID == int.Parse(function)) }; employeeModel.EmployeeFunctions.Add(functionToAdd); // add to list } } // only if no list if selected locations is given if (selectedLocations != null) { // get locations of employee employeeModel.EmployeeLocations = new List<EmployeeLocations>(); // populate selected locations foreach (var location in selectedLocations) { var locationToAdd = new EmployeeLocations { EmployeeModelID = employeeModel.ID, Employee = employeeModel, LocationID = int.Parse(location), Location = _context.Locations.Single(s => s.LocationID == int.Parse(location)) }; employeeModel.EmployeeLocations.Add(locationToAdd); // add to list } } string uniqueFileName = null; // will store generated filename // If the Photo property on the incoming model object is not null, then the user // has selected an image to upload. if (model.ProfileImage != null) { // The image must be uploaded to the images folder in wwwroot // To get the path of the wwwroot folder we are using the inject // HostingEnvironment service provided by ASP.NET Core string uploadsFolder = Path.Combine(_env.WebRootPath, "images"); // To make sure the file name is unique we are appending a new // GUID value and and an underscore to the file name uniqueFileName = Guid.NewGuid().ToString() + "_" + model.ProfileImage.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder model.ProfileImage.CopyTo(new FileStream(filePath, FileMode.Create)); } employeeModel.ProfileImage = uniqueFileName; // set profile image to uploaded filename if (ModelState.IsValid) { _context.Add(employeeModel); await _context.SaveChangesAsync(); //redirect admin to register page return Redirect("/Identity/Account/Register?email=" + employeeModel.EMail); //return RedirectToAction(nameof(Index)); } // call populate functions PopulateSupervisorsDropDownList(employeeModel.SupervisorID); PopulateAssignedFunctions(employeeModel); PopulateAssignedSkills(employeeModel); PopulateAssignedLocations(employeeModel); return View(employeeModel); } else { return Forbid(); // user is either not logged nor an admin, show forbidden page. } }