Example #1
0
        // Method to update CohortStaff
        private void UpdateCohortStaffs(int[] selectedStaff, Cohort cohortToUpdate)
        {
            if (selectedStaff == null)
            {
                var unknown = (from s in _context.Staff
                               where s.Name.Equals("(Not Yet Assigned)")
                               select s.Id).ToArray();
                selectedStaff = unknown;
            }

            var selectedStaffHS = new HashSet <int>(selectedStaff);
            var cohortStaffs    = new HashSet <int>
                                      (cohortToUpdate.CohortStaff.Select(c => c.Staff.Id));

            foreach (var staff in _context.Staff)
            {
                if (selectedStaffHS.Contains(staff.Id))
                {
                    if (!cohortStaffs.Contains(staff.Id))
                    {
                        cohortToUpdate.CohortStaff.Add(new CohortStaff {
                            CohortId = cohortToUpdate.Id, StaffId = staff.Id
                        });
                    }
                }
                else
                {
                    if (cohortStaffs.Contains(staff.Id))
                    {
                        CohortStaff staffToRemove = cohortToUpdate.CohortStaff.SingleOrDefault(c => c.StaffId == staff.Id);
                        _context.Remove(staffToRemove);
                    }
                }
            }
        }
Example #2
0
        public async Task <IActionResult> Add(string Id, DateTime StartDate, DateTime DemoDate, int[] selectedTechs, int[] selectedStaffs)
        {
            Cohort cohort = new Cohort();

            cohort.Id        = Id;
            cohort.StartDate = StartDate;
            cohort.DemoDate  = DemoDate;

            if (selectedTechs != null)
            {
                cohort.CohortTech = new List <CohortTech>();
                foreach (var tech in selectedTechs)
                {
                    var techToAdd = new CohortTech {
                        CohortId = cohort.Id, TechId = tech
                    };
                    cohort.CohortTech.Add(techToAdd);
                }
            }
            else
            {
                cohort.CohortTech = new List <CohortTech>();
            }
            if (selectedStaffs != null)
            {
                cohort.CohortStaff = new List <CohortStaff>();
                foreach (var staff in selectedStaffs)
                {
                    var staffToAdd = new CohortStaff {
                        CohortId = cohort.Id, StaffId = staff
                    };
                    cohort.CohortStaff.Add(staffToAdd);
                }
            }
            else
            {
                cohort.CohortStaff = new List <CohortStaff>();
            }


            if (ModelState.IsValid)
            {
                _context.Add(cohort);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Techs"]  = PopulateAssignedTechData(cohort);
            ViewData["Staffs"] = PopulateAssignedTechData(cohort);
            return(View(cohort));
        }