Ejemplo n.º 1
0
        private async void btn_addInstructor_Click(object sender, RoutedEventArgs e)
        {
            string errorList = "";

            if (cmb_gender.SelectedIndex == -1)
            {
                errorList += "Select a gender.\n";
            }
            if (lblBlock_CV.Text != "...." && !System.IO.File.Exists(lblBlock_CV.Text))
            {
                errorList += "The selected CV does not exist.\n";
            }
            if (num_age.Value == null)
            {
                errorList += "Numeric values cannot be null.\n";
            }
            if (txt_address.Text == "")
            {
                errorList += "Address cannot be empty.\n";
            }
            if (txt_instructorName.Text == "")
            {
                errorList += "Instructor name cannot be empty.\n";
            }
            if (txt_qualifier.Text == "")
            {
                errorList += "Qualifier cannot be empty.\n";
            }
            if (txt_phone.Text == "")
            {
                errorList += "Phone field cannot be empty.\n";
            }

            foreach (char letter in txt_phone.Text)
            {
                if (!char.IsDigit(letter))
                {
                    errorList += "Phone can only consist of numbers!\n";
                    break;
                }
            }

            if (errorList != "")
            {
                await this.ShowMessageAsync("Check the following!", errorList);

                return;
            }

            if (Globals.Instructors.ToList().Exists(x => x.Value.Name == txt_instructorName.Text || x.Value.Phone == txt_phone.Text))
            {
                if (EditedInstructor == null || (EditedInstructor != null && EditedInstructor.Name != txt_instructorName.Text))
                {
                    if (await this.ShowMessageAsync("Are you sure", "There exist an instructor with the same name/phone, are you sure you want to continue", MessageDialogStyle.AffirmativeAndNegative) == MessageDialogResult.Negative)
                    {
                        return;
                    }
                }
            }

            try
            {
                var newIns = new Instructor()
                {
                    Name      = txt_instructorName.Text,
                    Address   = txt_address.Text,
                    Phone     = txt_phone.Text,
                    Gender    = cmb_gender.SelectedIndex,
                    Qualifier = txt_qualifier.Text,
                    Age       = Convert.ToInt32(num_age.Value),
                };

                if (System.IO.File.Exists(lblBlock_CV.Text))
                {
                    newIns.CV = System.IO.File.ReadAllBytes(lblBlock_CV.Text);
                }

                //foreach (CheckedObject item in courses_listView.Items)
                //{
                //    if (item.Checked)
                //    {
                //        newIns.TeachingCourses.Add(item.Class);
                //    }
                //}

                if (EditedInstructor == null)
                {
                    Instructors.AddInstructor(newIns);
                }
                else
                {
                    newIns.Id = EditedInstructor.Id;
                    Instructors.EditInstructor(newIns);
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Globals.RefreshReferenceInformation();
        }