Esempio n. 1
0
        private async void OkButton_Click(object sender, RoutedEventArgs e)
        {
            string firstName   = FirstNameTextBox.Text.Trim();
            string lastName    = LastNameTextBox.Text.Trim();
            string email       = EmailTextBox.Text.Trim();
            string phoneNumber = PhoneNumberTextBox.Text.Trim().Replace(" ", "");

            StringBuilder errorMsg = new StringBuilder();

            if (firstName.Equals("") || !InputValidator.IsValidName(firstName))
            {
                errorMsg.Append("Invalid first name. \n");
            }

            if (lastName.Equals("") || !InputValidator.IsValidName(lastName))
            {
                errorMsg.Append("Invalid last name. \n");
            }

            if (email.Equals("") || !InputValidator.IsValidEmail(email))
            {
                errorMsg.Append("Invalid email. \n");
            }

            if (phoneNumber.Equals("") || !InputValidator.IsValidPhoneNumber(phoneNumber))
            {
                errorMsg.Append("Invalid phone number. \n");
            }

            //if the error message is not empty
            if (errorMsg.Length > 0)
            {
                System.Windows.MessageBox.Show(errorMsg.ToString());
                return;
            }

            if (await this.ApiAccess.CreateCustomer(new Customer
            {
                FirstName = firstName,
                LastName = lastName,
                Email = email,
                PhoneNumber = phoneNumber
            }))
            {
                System.Windows.MessageBox.Show("Successfully created!");
            }
            else
            {
                System.Windows.MessageBox.Show("Customer creation failed.");
            }
            this.Close();
        }
        private async void OkButton_Click(object sender, RoutedEventArgs e)
        {
            string        firstName   = FirstNameTextBox.Text.Trim();
            string        lastName    = LastNameTextBox.Text.Trim();
            string        email       = EmailTextBox.Text.Trim();
            string        phoneNumber = PhoneNumberTextBox.Text.Trim().Replace(" ", "");
            StringBuilder errorMsg    = new StringBuilder();

            if (firstName.Equals("") || !InputValidator.IsValidName(firstName))
            {
                errorMsg.Append("Invalid first name. \n");
            }

            if (lastName.Equals("") || !InputValidator.IsValidName(lastName))
            {
                errorMsg.Append("Invalid last name. \n");
            }

            if (email.Equals("") || !InputValidator.IsValidEmail(email))
            {
                errorMsg.Append("Invalid email. \n");
            }

            if (phoneNumber.Equals("") || !InputValidator.IsValidPhoneNumber(phoneNumber))
            {
                errorMsg.Append("Invalid phone number. \n");
            }

            //if the error message is not empty
            if (errorMsg.Length > 0)
            {
                System.Windows.MessageBox.Show(errorMsg.ToString());
                return;
            }

            //only proceed after validation
            this.CustomerToEdit.FirstName   = firstName;
            this.CustomerToEdit.LastName    = lastName;
            this.CustomerToEdit.Email       = email;
            this.CustomerToEdit.PhoneNumber = phoneNumber;
            if (await this.ApiAccess.UpdateCustomerById(this.CustomerToEdit.CustomerId, this.CustomerToEdit))
            {
                System.Windows.MessageBox.Show("Successfully updated.");
            }
            else
            {
                System.Windows.MessageBox.Show("Record not updated.");
            }
            this.Close();
        }