private void UpdateShiftSkills(string[] selectedSkills, Shift shiftToUpdate) { if (selectedSkills == null) { shiftToUpdate.ShiftSkills = new List <ShiftSkills>(); return; } var selectedSkillsHS = new HashSet <string>(selectedSkills); if (shiftToUpdate.ShiftSkills != null) { var shiftSkills = new HashSet <int> (shiftToUpdate.ShiftSkills.Select(c => c.Skill.SkillID)); foreach (var skill in _context.Skill) { if (selectedSkillsHS.Contains(skill.SkillID.ToString())) { if (!shiftSkills.Contains(skill.SkillID)) { shiftToUpdate.ShiftSkills.Add(new ShiftSkills { ShiftID = shiftToUpdate.ShiftID, SkillID = skill.SkillID }); } } else { if (shiftSkills.Contains(skill.SkillID)) { ShiftSkills skillToRemove = shiftToUpdate.ShiftSkills.FirstOrDefault(i => i.SkillID == skill.SkillID); _context.Remove(skillToRemove); } } } } else { shiftToUpdate.ShiftSkills = new List <ShiftSkills>(); foreach (var skill in _context.Skill) { if (selectedSkillsHS.Contains(skill.SkillID.ToString())) { shiftToUpdate.ShiftSkills.Add(new ShiftSkills { ShiftID = shiftToUpdate.ShiftID, Shift = shiftToUpdate, SkillID = skill.SkillID, Skill = skill }); } } } }
private static ICollection <ShiftSkills> SetShiftSkills() { List <ShiftSkills> shiftSkills = new List <ShiftSkills>(); int c = 0; foreach (Skill sk in skills) { ShiftSkills ss = new ShiftSkills { ShiftskillsID = c, Skill = sk, SkillID = sk.SkillID, ShiftID = 1, }; shiftSkills.Add(ss); c++; } return(shiftSkills); }
public List <ShiftSkills> SetShiftSkills(ApplicationDbContext context) { shifts = context.Shift.ToList(); skills = context.Skill.ToList(); foreach (Shift shift in shifts) { int t = 2; int range = skills.Count() - 1; for (int i = 0; i < t; i++) { int id = RandomID(range) + 1; ShiftSkills ss = new ShiftSkills { SkillID = id, ShiftID = shift.ShiftID }; shiftSkills.Add(ss); } counts.Clear(); } return(shiftSkills); }
private async static void AddShift(Shift shift, int[] selectedSkills, int[] selectedFunctions, int[] selectedFunctionsMax, ApplicationDbContext _context) { if (selectedSkills != null) { shift.ShiftSkills = new List <ShiftSkills>(); foreach (var skill in selectedSkills) { var skillToAdd = new ShiftSkills { ShiftID = shift.ShiftID, Shift = shift, SkillID = skill, Skill = _context.Skill.Single(s => s.SkillID == skill) }; shift.ShiftSkills.Add(skillToAdd); } } if (selectedFunctions != null) { shift.ShiftFunctions = new List <ShiftFunction>(); foreach (var function in selectedFunctions) { int functionID = function; int maxCnt = functionID - 1; var functionToAdd = new ShiftFunction { ShiftID = shift.ShiftID, Shift = shift, FunctionID = functionID, Function = _context.Function.Single(f => f.FunctionID == function), MaxEmployees = selectedFunctionsMax[maxCnt] }; shift.ShiftFunctions.Add(functionToAdd); } } _context.Add(shift); await _context.SaveChangesAsync(); if (shift.Weekly) { ICollection <Shift> children = CreateChildren(shift, _context); foreach (Shift child in children) { _context.Add(child); child.ShiftFunctions = new List <ShiftFunction>(); foreach (ShiftFunction sf in shift.ShiftFunctions) { ShiftFunction shiftFunction = new ShiftFunction { Function = sf.Function, FunctionID = sf.FunctionID, MaxEmployees = sf.MaxEmployees, ShiftID = child.ShiftID }; child.ShiftFunctions.Add(shiftFunction); } child.ShiftSkills = new List <ShiftSkills>(); foreach (ShiftSkills ss in shift.ShiftSkills) { ShiftSkills shiftSkills = new ShiftSkills { Skill = ss.Skill, SkillID = ss.SkillID, ShiftID = child.ShiftID }; child.ShiftSkills.Add(shiftSkills); } } await _context.SaveChangesAsync(); shift.ShiftChildren = children; } await _context.SaveChangesAsync(); }
public async Task <IActionResult> Create([Bind("Start,End,Weekly,LocationID,Location")] Shift shift, string[] selectedSkills, string[] selectedFunctions, int[] selectedFunctionsMax) { if (User.Identity.IsAuthenticated && _context.EmployeeModel.ToList().Find(x => x.EMail == User.Identity.Name).Admin) { if (selectedSkills != null) { shift.ShiftSkills = new List <ShiftSkills>(); foreach (var skill in selectedSkills) { var skillToAdd = new ShiftSkills { ShiftID = shift.ShiftID, Shift = shift, SkillID = int.Parse(skill), Skill = _context.Skill.Single(s => s.SkillID == int.Parse(skill)) }; shift.ShiftSkills.Add(skillToAdd); } } if (selectedFunctions != null) { shift.ShiftFunctions = new List <ShiftFunction>(); foreach (var function in selectedFunctions) { int functionID = int.Parse(function); int maxCnt = functionID - 1; var functionToAdd = new ShiftFunction { ShiftID = shift.ShiftID, Shift = shift, FunctionID = functionID, Function = _context.Function.Single(f => f.FunctionID == int.Parse(function)), MaxEmployees = selectedFunctionsMax[maxCnt] }; shift.ShiftFunctions.Add(functionToAdd); } } if (ModelState.IsValid) { _context.Add(shift); await _context.SaveChangesAsync(); if (shift.Weekly) { ICollection <Shift> children = CreateChildren(shift); foreach (Shift child in children) { _context.Add(child); child.ShiftFunctions = new List <ShiftFunction>(); foreach (ShiftFunction sf in shift.ShiftFunctions) { ShiftFunction shiftFunction = new ShiftFunction { Function = sf.Function, FunctionID = sf.FunctionID, MaxEmployees = sf.MaxEmployees, ShiftID = child.ShiftID }; child.ShiftFunctions.Add(shiftFunction); } child.ShiftSkills = new List <ShiftSkills>(); foreach (ShiftSkills ss in shift.ShiftSkills) { ShiftSkills shiftSkills = new ShiftSkills { Skill = ss.Skill, SkillID = ss.SkillID, ShiftID = child.ShiftID }; child.ShiftSkills.Add(shiftSkills); } } await _context.SaveChangesAsync(); shift.ShiftChildren = children; } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateLocationsDropDownList(shift); return(View(shift)); } else { return(Forbid()); } }