Exemplo n.º 1
0
        public ActionResult Create(BookingViewModel bookingView)
        {
            if (Session["LoggedCustomerId"] == null)
            {
                return(RedirectToAction("Login", "Login"));
            }

            if (ModelState.IsValid)
            {
                if (int.TryParse(Session["LoggedCustomerId"].ToString(), out int id))
                {
                    Customer customer   = db.GetCustomer(id);
                    Booking  newBooking = new Booking(bookingView.StartDate, bookingView.EndDate, new List <Tool>(), customer, "Reserveret", bookingView.TotalPrice);
                    foreach (int i in bookingView.SelectedTools)
                    {
                        var tool = db.Tools.Find(i);
                        db.Entry(tool).Collection(t => t.Bookings).Load();
                        newBooking.Tools.Add(tool);
                        tool.Bookings.Add(newBooking);
                    }
                    customer.Bookings.Add(newBooking);
                    db.Bookings.Add(newBooking);
                    db.SaveChanges();
                    return(View("BookingInformation", newBooking));
                }
                else
                {
                    return(View(bookingView));
                }
            }
            else
            {
                return(View(bookingView));
            }
        }
 private void OnReturnedClicked(object sender, RoutedEventArgs e)
 {
     if (_viewModel.SelectedBooking != null)
     {
         _dbContext.Bookings.Attach(_viewModel.SelectedBooking);
         _viewModel.SelectedBooking.Status = Booking.BookingStatus.Returned;
         _dbContext.SaveChanges();
         _viewModel.HandoutBtnIsEnabled = false;
         _viewModel.ReturnBtnIsEnabled  = false;
         //TODO : Update bookings list so it does not display the returned booking;
         //_dbContext.Entry(_viewModel.SelectedCustomer)
         //    .Collection(c => c.Bookings)
         //    .Query().Where(b => b.Status != Booking.BookingStatus.Returned)
         //    .Include(b => b.Tool)
         //    .Load();
     }
 }
Exemplo n.º 3
0
        public ActionResult ConfirmReservation(BookingViewModel model)
        {
            var booking =
                new Booking
            {
                Customer   = dbContext.Customers.Find(model.CustomerId),
                Tool       = dbContext.Tools.Find(model.Tool.Id),
                PickUpDate = model.PickUpDate,
                RentPeriod = model.RentDays,
                Status     = Booking.BookingStatus.Reserved
            };

            dbContext.Bookings.Add(booking);
            dbContext.SaveChanges();
            return(View("Confirmation", booking));
        }
        //------------------------------ 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();
            }
        }
Exemplo n.º 5
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();
                }
            }
        }
Exemplo n.º 6
0
        //------------------------------ EVENTHANDLERS --------------------------------------------------------

        /**
         * This method has two functions, either it shows the elements needed to
         * change the status or it changes and saves the status of the booking.
         */
        private void EditStatusButton_Click(object sender, RoutedEventArgs e)
        {
            // Hide InfoLabel.
            InfoLabel.Visibility = Visibility.Collapsed;

            // Get EditButtons text to see, if it is the edit or
            // the save functionality that should be executed.
            string text = EditButton.Content.ToString();

            // The edit functionality executes.
            if (text == "Rediger Status")
            {
                ShowEditStatusElements();
            }

            // The save functionality executes.
            if (text == "Gem Status")
            {
                // Get current status.
                string current = Status.Content.ToString();

                // Get selected item from ComboBox. Probably not the prettiest solution, but it works.
                string selectedStatus = Combo.SelectedItem.ToString(); // System.Windows.Controls.ComboBoxItem: Udleveret
                string status         = selectedStatus.Split()[1];     // Udleveret

                // Only change and save if there are any changes to make.
                if (status != current)
                {
                    // If a user tries to change a status from "Udleveret"
                    // to "Reserveret", an error is returned.
                    if (current == "Udleveret" && status == "Reserveret")
                    {
                        // Set and show InfoLabel as Error.
                        SetLabelRedText(InfoLabel, "Udlejningen er udleveret og kan derfor ikke reserveres.");
                    }
                    // If status is not the same as current and current
                    // is not "Udleveret", the status can be set.
                    else if (status == "Reserveret" || status == "Udleveret" || status == "Tilbageleveret")
                    {
                        // Save changes to database.
                        _booking.Status = status;
                        _context.SaveChanges();

                        // Update this datacontext.
                        DataContext = null;
                        DataContext = _booking;

                        // Update BookingsListBox in MainWindow.
                        List <Booking> bookings = new List <Booking>(_booking.Customer.Bookings);
                        _context.SortBookings(bookings);
                        _bookingListBox.ItemsSource = null;
                        _bookingListBox.ItemsSource = bookings;

                        SetLabelGreenText(InfoLabel, "Ændringen blev gemt.");
                    }
                }
                else
                {
                    SetLabelRedText(InfoLabel, "Der blev ikke gemt nogen ændringer.");
                }

                HideEditStatusElements();
            }
        }