//------------------------------ EVENTHANDLERS --------------------------------------------------------

        /**
         * Creates a new customer.
         */
        private void AddCustomerButton_Click(object sender, RoutedEventArgs e)
        {
            // Hide all error labels.
            HideInteractionLabels();

            // Initialize new property variables.
            string name     = "";
            string address  = "";
            string email    = "";
            string username = "******";      // Default value.
            string password = "******"; // Default value.

            // Get input from TextBoxes.
            string Name     = TxtBoxName.Text;
            string Address  = TxtBoxAddress.Text;
            string Email    = TxtBoxEmail.Text;
            string Username = TxtBoxUsername.Text;
            string Password = PasswordBox.Password;

            // Counts errors.
            int errorCounter = 0;

            // Check if name is set, validate and set new value or show error.
            if (NameIsSet(Name))
            {
                if (_context.VerifyName(Name))
                {
                    name = Name;
                }
                else
                {
                    // Name is not valid.
                    SetErrorLabel(NameErrorLabel, "Navnet er ugyldigt.");
                    errorCounter++;
                }
            }
            else
            {
                // Name is not set.
                SetErrorLabel(NameErrorLabel, "Indtast venligst et navn.");
                errorCounter++;
            }

            // See if address is set and set new value or show error.
            if (AddressIsSet(Address))
            {
                address = Address;
            }
            else
            {
                // Address is not set.
                SetErrorLabel(AddressErrorLabel, "Indtast venligst en adresse.");
                errorCounter++;
            }

            // See if email is set, validate and set new value or show error.
            if (EmailIsSet(Email))
            {
                if (_context.VerifyEmail(Email))
                {
                    email = Email;
                }
                else
                {
                    // Email is not valid.
                    SetErrorLabel(EmailErrorLabel, "E-mailadressen er ugyldig.");
                    errorCounter++;
                }
            }
            else
            {
                // Email is not set.
                SetErrorLabel(EmailErrorLabel, "Indtast venligst en e-mailadresse.");
                errorCounter++;
            }

            // If both the username and the password are set, validate the values
            // and set new values or show errors.
            if (UsernameIsSet(Username) && PasswordIsSet(Password))
            {
                if (_context.VerifyUsername(Username))
                {
                    username = Username;
                }
                else
                {
                    // The username is too long.
                    SetErrorLabel(UsernameErrorLabel, "Brugernavnet må højest indeholde 25 tegn.");
                    errorCounter++;
                }

                if (_context.VerifyPassword(Password))
                {
                    password = Password;
                }
                else
                {
                    // The password does not meet the length requirements.
                    SetErrorLabel(PasswordErrorLabel, "Kodeordet skal indeholde 8 - 20 tegn.");
                    errorCounter++;
                }
            }
            // One of the login informations is set, but not the other.
            else if (!UsernameIsSet(Username) && PasswordIsSet(Password) || UsernameIsSet(Username) && !PasswordIsSet(Password))
            {
                // Username is not set.
                if (!UsernameIsSet(Username))
                {
                    SetErrorLabel(UsernameErrorLabel, "Opret login ved at udfylde loginfelterne.");
                    errorCounter++;
                }
                // Password is not set.
                else if (!PasswordIsSet(Password))
                {
                    SetErrorLabel(PasswordErrorLabel, "Opret login ved at udfylde loginfelterne.");
                    errorCounter++;
                }
            }

            // If there are no errors, create a new customer, add
            // it to the database, save it and close the window.
            // If successful, show label with confirmation and
            // the new customer on MainWindow.
            if (errorCounter == 0)
            {
                // Create and save new customer.
                Customer customer = new Customer(name, address, email, username, password);
                _context.Customers.Add(customer);
                _context.SaveChanges();

                // Reset search textbox in main.
                ResetSearchTxtBox();

                // Set and show SearchErrorLabel as InfoLabel in main.
                SetLabelGreenText(_mainWinLbl, "En ny kunde blev oprettet.");

                // Set datacontext in customergrid, reset listbox and show EditButton in main.
                _mainWinGrid.DataContext         = customer;
                _mainWinBookingsList.ItemsSource = null;
                _mainWinEditButton.Visibility    = Visibility.Visible;

                // Hide this window.
                this.Hide();
            }
        }
Beispiel #2
0
        /**
         * If button text is "Rediger Kunde", show edit customer elements.
         * If button text is "Gem ændringer", get the customer and explore
         * edit textboxes and passwordbox for relevant changes. If relevant
         * changes have been made, the changes are saved in the database
         * and CustomerInfoGrid is updated to show changes.
         * Otherwise sets InfoLabels text and shows error message.
         */
        private void EditCustomerButton_Click(object sender, RoutedEventArgs e)
        {
            // Set EditErrorLabel to default error content.
            EditErrorLabel.Content = "Fejl.";

            // Hide all error/confirmation labels.
            HideInteractionLabels();

            // Get button text.
            // Checking whether it is the edit or the save
            // functionality that needs to be executed.
            string text = EditButton.Content.ToString();

            // Edit functionality executes.
            if (text == "Rediger Kunde")
            {
                ShowEditCustomerElements();
            }

            // Save functionality executes.
            if (text == "Gem Ændringer")
            {
                // Get id fom binded label.
                string id = CustomerId.Content.ToString();

                // Get customer from database.
                Customer customer = _context.GetCustomerByString(id);

                // Count number of changes.
                int changesCounter = 0;

                // Property changes booleans.
                bool nameBool     = false;
                bool addressBool  = false;
                bool emailBool    = false;
                bool usernameBool = false;
                bool passwordBool = false;

                // Current values.
                string currentName     = CustomerName.Content.ToString();
                string currentAddress  = CustomerAddress.Content.ToString();
                string currentEmail    = CustomerEmail.Content.ToString();
                string currentUsername = CustomerUsername.Content.ToString();
                string currentPassword = CustomerPassword.Content.ToString();

                // Textbox text values.
                string nameValue     = TxtBoxCustomerName.Text;
                string addressValue  = TxtBoxCustomerAddress.Text;
                string emailValue    = TxtBoxCustomerEmail.Text;
                string usernameValue = TxtBoxCustomerUsername.Text;
                string passwordValue = CustomerPassBox.Password;

                /**
                 * Examining textboxes and the passwordbox to see if text has changed
                 * to something useful. If there are changes, the property boolean
                 * is set to true and the changescounter is incremented.
                 */
                if (NameHasChanged(currentName, nameValue))
                {
                    nameBool = true;
                    changesCounter++;
                }

                if (AddressHasChanged(currentAddress, addressValue))
                {
                    addressBool = true;
                    changesCounter++;
                }

                if (EmailHasChanged(currentEmail, emailValue))
                {
                    emailBool = true;
                    changesCounter++;
                }

                if (UsernameHasChanged(currentUsername, usernameValue))
                {
                    usernameBool = true;
                    changesCounter++;
                }

                if (PasswordHasChanged(currentPassword, passwordValue))
                {
                    passwordBool = true;
                    changesCounter++;
                }

                // For each of the properties, if there is a
                // change, verify new value and set customer
                // property if validation was succesful.
                if (changesCounter > 0)
                {
                    // Counts errors.
                    int errorCounter = 0;

                    // If name has changed, verify new name
                    // and set new value or set error.
                    if (nameBool)
                    {
                        if (_context.VerifyName(nameValue))
                        {
                            customer.Name = nameValue;
                        }
                        else
                        {
                            EditErrorLabel.Content = "Navnet er ugyldigt.";
                            errorCounter++;
                        }
                    }

                    // If address has changed, set new value.
                    if (addressBool)
                    {
                        customer.Address = addressValue;
                    }

                    // If email has changed, verify new email
                    // and set new value or set error.
                    if (emailBool)
                    {
                        if (_context.VerifyEmail(emailValue))
                        {
                            customer.Email = emailValue;
                        }
                        else
                        {
                            // If there already is an error above, don't show this one yet.
                            // If there are no errors above this one, show this error.
                            if (IsDefaultError(EditErrorLabel))
                            {
                                EditErrorLabel.Content = "E-mailaddressen er ugyldig.";
                                errorCounter++;
                            }
                        }
                    }

                    // If the customer does not have a login.
                    if (!_context.HasLogin(customer))
                    {
                        // Both username and password have changes.
                        if (usernameBool && passwordBool)
                        {
                            // Verify username and set new value or set error.
                            if (_context.VerifyUsername(usernameValue))
                            {
                                customer.Username = usernameValue;
                            }
                            else
                            {
                                // If there already is an error above, don't show this one yet.
                                // If there are no errors above this one, show this error.
                                if (IsDefaultError(EditErrorLabel))
                                {
                                    EditErrorLabel.Content = "Brugernavnet må højest indeholde 25 tegn.";
                                    errorCounter++;
                                }
                            }

                            // Verify Password and set new value or set error.
                            if (_context.VerifyPassword(passwordValue))
                            {
                                customer.Password = passwordValue;
                            }
                            else
                            {
                                // If there already is an error above, don't show this one yet.
                                // If there are no errors above this one, show this error.
                                if (IsDefaultError(EditErrorLabel))
                                {
                                    EditErrorLabel.Content = "Kodeordet skal indeholde 8 - 20 tegn.";
                                    errorCounter++;
                                }
                            }
                        }

                        // Set errror, if customer does not have a login
                        // and only one of the login fields are filled.
                        if (usernameBool && !passwordBool || !usernameBool && passwordBool)
                        {
                            // If there already is an error above, don't show this one yet.
                            // If there are no errors above this one, show this error.
                            if (IsDefaultError(EditErrorLabel))
                            {
                                EditErrorLabel.Content = "Opret login ved at udfylde loginfelterne.";
                                errorCounter++;
                            }
                        }
                    }

                    // If the customer does have a login.
                    if (_context.HasLogin(customer))
                    {
                        // If username has changed, verify new
                        // username and set new value or set error.
                        if (usernameBool)
                        {
                            if (_context.VerifyUsername(usernameValue))
                            {
                                customer.Username = usernameValue;
                            }
                            else
                            {
                                // If there already is an error above, don't show this one yet.
                                // If there are no errors above this one, show this error.
                                if (IsDefaultError(EditErrorLabel))
                                {
                                    EditErrorLabel.Content = "Brugernavnet må højest indeholde 25 tegn.";
                                    errorCounter++;
                                }
                            }
                        }

                        // If password has changed, verify new password
                        // and set new value or set error.
                        if (passwordBool)
                        {
                            if (_context.VerifyPassword(passwordValue))
                            {
                                customer.Password = passwordValue;
                            }
                            else
                            {
                                // If there already is an error above, don't show this one yet.
                                // If there are no errors above this one, show this error.
                                if (IsDefaultError(EditErrorLabel))
                                {
                                    EditErrorLabel.Content = "Kodeordet skal indeholde 8 - 20 tegn.";
                                    errorCounter++;
                                }
                            }
                        }
                    }

                    // If there are any errors, reset properties and show error.
                    if (errorCounter != 0)
                    {
                        EditErrorLabel.Visibility = Visibility.Visible;
                        SetLabelRedText(InfoLabel, "Ingen ændringer blev gemt.");
                        ShowEditCustomerElements();

                        // Reset customer properties.
                        customer.Name     = currentName;
                        customer.Address  = currentAddress;
                        customer.Email    = currentEmail;
                        customer.Username = currentUsername;
                        customer.Password = currentPassword;
                    }

                    // If there are no errors, save the changes.
                    if (errorCounter == 0)
                    {
                        EditErrorLabel.Visibility = Visibility.Collapsed;
                        HideEditCustomerElements();

                        if (changesCounter == 1)
                        {
                            SetLabelGreenText(InfoLabel, "Ændringen blev gemt.");
                        }
                        else
                        {
                            SetLabelGreenText(InfoLabel, "Ændringerne er blevet gemt.");
                        }

                        // SAVE changes to database.
                        _context.SaveChanges();
                    }

                    // Update CustomerGrid to show new values.
                    CustomerGrid.DataContext = null;
                    CustomerGrid.DataContext = customer;
                }
                else
                {
                    SetLabelRedText(InfoLabel, "Der blev ikke gemt nogen ændringer.");
                    HideEditCustomerElements();
                }
            }
        }