Example #1
0
        private void buttonAddNewUser_Click(object sender, EventArgs e)
        {
            try
            {
                string newUsername       = textBoxNewUsername.Text.Trim();
                string selectedRole      = dropdownRoles.Text;
                int    selectedRoleValue = (int)dropdownRoles.SelectedValue;

                if (!string.IsNullOrEmpty(newUsername))
                {
                    if (_db.Users.Where(u => u.UserName == newUsername).FirstOrDefault() == null)
                    {
                        User newUser = new User
                        {
                            UserName = newUsername,
                            Password = Utils.HashPassword(),
                            IsActive = true
                        };

                        _db.Users.Add(newUser);
                        _db.SaveChanges();

                        _db.UserRoles.Add(new UserRole
                        {
                            UserID = newUser.ID,
                            RoleID = selectedRoleValue
                        });

                        _db.SaveChanges();

                        _manageUserForm.PopulateUsersGrid();
                        MessageBox.Show($"A new account has been created for {newUsername} with role: {selectedRole}");
                    }
                    else
                    {
                        MessageBox.Show($"There is already a username: {newUsername}. Please provide another username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Please provide a valid input for username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #2
0
        private void buttonResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                int  userID       = (int)dataGridViewManageUsers.SelectedRows[0].Cells["UserID"].Value;
                User selectedUser = _db.Users.Where(u => u.ID == userID).FirstOrDefault();

                if (selectedUser != null)
                {
                    selectedUser.Password = Utils.HashPassword();
                }

                _db.SaveChanges();

                MessageBox.Show($"{selectedUser.UserName}'s password has been reset");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void buttonReset_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxResetPassword.Text == textBoxConfirmPassword.Text)
                {
                    User user = _db.Users.Where(u => u.ID == _user.ID).First();
                    user.Password = Utils.HashPassword(textBoxConfirmPassword.Text);

                    _db.SaveChanges();

                    MessageBox.Show("You have reset your password successfully", "Password Reset", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("The passwords provided do not match. Please try again.", "Password Reset", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #4
0
        private void buttonCheckinCar_Click(object sender, EventArgs e)
        {
            try
            {
                // get values from form controls
                string custName     = dropdownCustName.Text;
                int    custID       = (int)dropdownCustName.SelectedValue;
                string carToReturn  = dropdownCarToReturn.Text;
                var    dateReturned = dateTimePickerDateReturned.Value;

                // declare local variables
                string errorMessage = string.Empty;

                // form control validation
                if (string.IsNullOrWhiteSpace(custName))
                {
                    errorMessage += "Error: Please select a valid input for customer name \n\r";
                }

                if (string.IsNullOrWhiteSpace(carToReturn))
                {
                    errorMessage += "Error: Please select a valid input for car to return \n\r";
                }

                if (dateReturned == null)
                {
                    errorMessage += "Error: Please enter valid date \n\r";
                }

                // form control business validation
                if (!string.IsNullOrWhiteSpace(custName) && !string.IsNullOrWhiteSpace(carToReturn) && dateReturned != null)
                {
                    CarRentalRecord selectedCarRecord = _db.CarRentalRecords.First(c => c.ID == custID);

                    if (selectedCarRecord.DateToReturn == null)
                    {
                        throw new NoNullAllowedException();
                    }
                    else if (selectedCarRecord.DateToReturn != null && dateReturned.Date < selectedCarRecord.DateToReturn.Value.Date)
                    {
                        errorMessage += $"Error: date returned: {dateReturned.Date} cannot be less than date to return: {selectedCarRecord.DateToReturn.Value.Date} \n\r";
                    }
                }

                // display error message or save valid input
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    CarRentalRecord selectedCarRecord = _db.CarRentalRecords.First(c => c.ID == custID);
                    selectedCarRecord.DateReturned     = selectedCarRecord.DateToReturn.Value;
                    selectedCarRecord.CarRecord.OnLoan = false;

                    _db.SaveChanges();

                    MessageBox.Show($"Customer Name: {custName} \n\r" + $"Car: {carToReturn} \n\r" + $"Date Returned: {dateReturned.Date} \n\r");

                    // refresh dropdowns
                    PopulateCustNameAndCarToReturnDropdowns();
                }
            }
            catch (NoNullAllowedException nex)
            {
                MessageBox.Show($"Error: {nex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #5
0
        private void buttonAddRentalRecord_Click(object sender, EventArgs e)
        {
            try
            {
                // get values from form controls
                string custName     = textBoxCustName.Text.Trim();
                string carType      = dropdownCarRented.Text;
                var    dateRented   = dateTimePickerDateRented.Value;
                var    dateToReturn = dateTimePickerDateToReturn.Value;
                Decimal.TryParse(textBoxCarCost.Text.Trim(), out decimal carRentalCost);

                // declare local variables
                string errorMessage = string.Empty;

                // form control validation
                if (string.IsNullOrWhiteSpace(custName))
                {
                    errorMessage += "Error: Please enter input value for Customer Name \n\r";
                }

                if (carRentalCost == 0)
                {
                    errorMessage += "Error: Please enter input value for Car Rental Cost \n\r";
                }

                if ((dateRented == null || dateToReturn == null) || dateRented.Date > dateToReturn.Date)
                {
                    errorMessage += "Error: Please enter valid date(s) \n\r";
                }

                if (string.IsNullOrWhiteSpace(carType))
                {
                    errorMessage += "Error: Please enter input value for Car Type \n\r";
                }

                // display error message or save valid input
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    CarRentalRecord newCarRentalRecord = new CarRentalRecord
                    {
                        CustomerName = custName,
                        Cost         = carRentalCost,
                        DateRented   = dateRented,
                        DateToReturn = dateToReturn,
                        CarID        = (int)dropdownCarRented.SelectedValue,
                    };

                    _db.CarRentalRecords.Add(newCarRentalRecord);

                    CarRecord carRented = _db.CarRecords.Where(c => c.ID == (int)dropdownCarRented.SelectedValue).First();
                    carRented.OnLoan = true;

                    _db.SaveChanges();

                    MessageBox.Show($"Customer Name: {custName} \n\r" + $"Date Rented: {dateRented.Date} \n\r" +
                                    $"Date To Return: {dateToReturn.Date} \n\r" + $"Cost: {carRentalCost} \n\r" + $"Car: {carType} \n\r");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }