private void UpdateDoctorSpecialties(string[] selectedOptions, Doctor doctorToUpdate)
        {
            if (selectedOptions == null)
            {
                doctorToUpdate.DoctorSpecialties = new List <DoctorSpecialty>();
                return;
            }

            var selectedOptionsHS = new HashSet <string>(selectedOptions);
            var currentOptionsHS  = new HashSet <int>(doctorToUpdate.DoctorSpecialties.Select(b => b.SpecialtyID));

            foreach (var s in _context.Specialties)
            {
                if (selectedOptionsHS.Contains(s.ID.ToString()))
                {
                    if (!currentOptionsHS.Contains(s.ID))
                    {
                        doctorToUpdate.DoctorSpecialties.Add(new DoctorSpecialty
                        {
                            SpecialtyID = s.ID,
                            DoctorID    = doctorToUpdate.ID
                        });
                    }
                }
                else
                {
                    if (currentOptionsHS.Contains(s.ID))
                    {
                        DoctorSpecialty specToRemove = doctorToUpdate.DoctorSpecialties.SingleOrDefault(d => d.SpecialtyID == s.ID);
                        _context.Remove(specToRemove);
                    }
                }
            }
        }
Exemple #2
0
        private void UpdatePatientConditions(string[] selectedOptions, Patient patientToUpdate)
        {
            if (selectedOptions == null)
            {
                patientToUpdate.PatientConditions = new List <PatientCondition>();
                return;
            }

            var selectedOptionsHS = new HashSet <string>(selectedOptions);
            var patientOptionsHS  = new HashSet <int>
                                        (patientToUpdate.PatientConditions.Select(c => c.ConditionID));//IDs of the currently selected conditions

            foreach (var option in _context.Conditions)
            {
                if (selectedOptionsHS.Contains(option.ID.ToString()))
                {
                    if (!patientOptionsHS.Contains(option.ID))
                    {
                        patientToUpdate.PatientConditions.Add(new PatientCondition {
                            PatientID = patientToUpdate.ID, ConditionID = option.ID
                        });
                    }
                }
                else
                {
                    if (patientOptionsHS.Contains(option.ID))
                    {
                        PatientCondition conditionToRemove = patientToUpdate.PatientConditions.SingleOrDefault(c => c.ConditionID == option.ID);
                        _context.Remove(conditionToRemove);
                    }
                }
            }
        }