Beispiel #1
0
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            string firstName = AddFirstNameTB.Text.Trim();
            string lastName  = AddLastNameTB.Text.Trim();
            string fen       = AddFENTB.Text.Trim();

            if (commander.IsInputInvalid(firstName, "First name") || commander.IsInputInvalid(lastName, "Last name"))
            {
                MessageBox.Show($"Invalid name value.");
                return;
            }
            if (!commander.LettersInputOnly(firstName))
            {
                MessageBox.Show($"First name must consist of letter only.");
                return;
            }
            if (!commander.LettersInputOnly(lastName))
            {
                MessageBox.Show($"Last name must consist of letter only.");
                return;
            }
            if (fen.Length > 0 && fen.Length < 8)
            {
                MessageBox.Show($"FEN must consist of 8-10 letters or digits or must be empty.");
                return;
            }
            if (!commander.LettersAndDigitsInputOnly(fen))
            {
                MessageBox.Show($"FEN must consist of letters or digits only, {fen} is not valid FEN number");
                return;
            }
            var duplicatedClient = db.Clients.SingleOrDefault(x =>
                                                              x.client_first_name.ToLower() == firstName.ToLower() &&
                                                              x.client_name.ToLower() == lastName.ToLower() &&
                                                              x.firm_evidence_number.ToLower() == fen.ToLower());

            if (duplicatedClient != null)
            {
                MessageBox.Show($"Client already exists.\nID: {duplicatedClient.client_id}, {duplicatedClient.client_first_name} {duplicatedClient.client_name}, FEN: {duplicatedClient.firm_evidence_number}");
                return;
            }
            Clients newClient = new Clients();

            newClient.client_first_name    = firstName;
            newClient.client_name          = lastName;
            newClient.firm_evidence_number = fen;
            db.Clients.Add(newClient);
            db.SaveChanges();
            MessageBox.Show($"Successfully created client:\nID: {newClient.client_id}, {newClient.client_first_name} {newClient.client_name}, FEN: {newClient.firm_evidence_number}");
            AddFirstNameTB.Text = "First name";
            AddLastNameTB.Text  = "Last name";
            AddFENTB.Text       = "FEN";
        }
        private void Find_Click(object sender, RoutedEventArgs e)
        {
            string firstName = FindFirstNameTB.Text.Trim();
            string lastName  = FindLastNameTB.Text.Trim();

            if (commander.IsInputInvalid(firstName, "First name") || commander.IsInputInvalid(lastName, "Last name") || !commander.LettersInputOnly(firstName) || !commander.LettersInputOnly(lastName))
            {
                MessageBox.Show("Invalid first name or last name values.");
                return;
            }
            var foundEmployees = db.Employees.Where(x => x.employee_first_name.ToLower() == firstName.ToLower() && x.employee_name.ToLower() == lastName.ToLower());

            if (foundEmployees.Count() == 0)
            {
                MessageBox.Show($"No employees named {firstName} {lastName} found.");
                return;
            }
            StringBuilder sb = new StringBuilder();

            foreach (var emp in foundEmployees)
            {
                sb.Append($"ID: {emp.employee_id}, {emp.employee_first_name} {emp.employee_name}\n");
            }
            MessageBox.Show($"Employees found:\n{sb}");
            FindFirstNameTB.Text = "First name";
            FindLastNameTB.Text  = "Last name";
        }