private bool Save(ResortVehicle resortVehicle) { bool result = false; try { if (resortVehicle.Id == 0) // a vehicle is being created. { _resortVehicleManager.AddVehicle(resortVehicle); result = true; ClearFields(); } else // vehicle is being updated { _resortVehicleManager.UpdateVehicle(_resortVehicle, resortVehicle); // ... synchronize _vehicle (old vehicle) with new database copy _resortVehicle = resortVehicle; result = true; } var caller = (FrmBrowseShuttleVehicles)_callingWindow; // ... update calling window // register calling forms here for live updates caller.RefreshShuttleVehiclesDatagrid(); } catch (Exception ex) { var errorStr = _resortVehicle == null ? "Error Adding Vehicle\n" : "Error Updating Vehicle\n"; MessageBox.Show(errorStr + ex.Message); } return(result); }
/// <summary author="Francis Mingomba" created="2019/04/03"> /// Checks employee privileges and deactivates vehicles /// if employee has privileges to do it /// </summary> /// <param name="resortVehicle">resort vehicle</param> /// <param name="employee">employee performing operation</param> public void DeactivateVehicle(ResortVehicle resortVehicle, Employee employee = null) { if (!employee.HasRoles(out string errorStr, "Admin")) { throw new ApplicationException(errorStr); } // make sure vehicle is not active if (!resortVehicle.Active) { throw new ApplicationException("Vehicle already inactive"); } // make sure vehicle is not in use if (resortVehicle.ResortVehicleStatusId == new ResortVehicleStatus().InUse) { throw new ApplicationException("Vehicle currently in use"); } try { _resortVehicleAccessor.DeactivateVehicle(resortVehicle.Id); } catch (Exception ex) { ExceptionLogManager.getInstance().LogException(ex); throw ex; } }
/// <summary author="Francis Mingomba" created="2019/04/03"> /// Checks employee privileges and activates vehicles /// if employee has privileges to do it /// </summary> /// <param name="resortVehicle">resort vehicle</param> /// <param name="employee">employee performing operation</param> public void ActivateVehicle(ResortVehicle resortVehicle, Employee employee = null) { try { if (resortVehicle == null) { throw new ApplicationException("Vehicle cannot be null"); } if (!employee.HasRoles(out string errorStr, "Admin")) { throw new ApplicationException(errorStr); } var newVehicle = resortVehicle.DeepClone(); newVehicle.Active = true; newVehicle.DeactivationDate = null; _resortVehicleAccessor.UpdateVehicle(resortVehicle, newVehicle); } catch (Exception ex) { ExceptionLogManager.getInstance().LogException(ex); throw ex; } }
private void BtnActivate_OnClick(object sender, RoutedEventArgs e) { try { _resortVehicleManager.ActivateVehicle(_resortVehicle, _employee); // ... synchronize _vehicle (old vehicle) with new database copy _resortVehicle = _resortVehicleManager.RetrieveVehicleById(_resortVehicle.Id); var caller = (FrmBrowseShuttleVehicles)_callingWindow; caller.RefreshShuttleVehiclesDatagrid(); tbStatus.Text = _resortVehicle.ResortVehicleStatusId; EnableFields(); gridDeactivationDate.Visibility = Visibility.Collapsed; MessageBox.Show("Vehicle activated successfully"); } catch (Exception ex) { MessageBox.Show("Error occured while activating vehicle\n" + ex.Message); } }
public FrmCheckoutPrompt(FrmResortVehicleCheckout caller, ResortVehicle vehicle) { _resortVehicle = vehicle; _caller = caller; InitializeComponent(); PopulatePrompt(); }
/// <summary author="Francis Mingomba" created="2019/04/03"> /// Adds vehicle to database /// </summary> /// <param name="resortVehicle">resort vehicle</param> public void AddVehicle(ResortVehicle resortVehicle) { try { this.MeetsValidationCriteria(resortVehicle, GetResortVehicleValidationCriteria()); _resortVehicleAccessor.AddVehicle(resortVehicle); } catch (Exception ex) { ExceptionLogManager.getInstance().LogException(ex); throw ex; } }
/// <summary author="Francis Mingomba" created="2019/04/03"> /// Performs validation /// Updates vehicle in database /// </summary> /// <param name="oldResortVehicle">old resort vehicle (database copy)</param> /// <param name="newResortVehicle">new resort vehicle (updated copy)</param> public void UpdateVehicle(ResortVehicle oldResortVehicle, ResortVehicle newResortVehicle) { try { this.MeetsValidationCriteria(newResortVehicle, GetResortVehicleValidationCriteria()); _resortVehicleAccessor.UpdateVehicle(oldResortVehicle, newResortVehicle); } catch (Exception ex) { ExceptionLogManager.getInstance().LogException(ex); throw ex; } }
/// <summary> /// Francis Mingomba /// Created: 2019/04/03 /// /// </summary> /// <param name="callingWindow">Ref to class invoking form</param> /// <param name="employee">current employee</param> /// <param name="resortVehicle">Vehicle passed in (optional)</param> /// <param name="editMode">enable/disable editMode (optional)</param> public FrmManageShuttleVehicle(object callingWindow, Employee employee, ResortVehicle resortVehicle = null, bool editMode = false) { _resortVehicleManager = new ResortVehicleManager(); _callingWindow = callingWindow; _resortVehicle = resortVehicle; _employee = employee; _isEditMode = editMode; InitializeComponent(); SetupWindow(); }
private void BtnSave_Click(object sender, RoutedEventArgs e) { if (ValidateFields()) { try { var vehicle = new ResortVehicle { Id = _resortVehicle?.Id ?? 0, Make = txtMake.Text, Model = txtModel.Text, Year = int.Parse(txtYear.Text), License = txtLicense.Text, Mileage = int.Parse(txtMileage.Text), Capacity = int.Parse(txtCapacity.Text), Color = cmbColor.Text, PurchaseDate = txtPurchaseDate.SelectedDate, Description = txtDescription.Text, Active = _resortVehicle?.Active ?? true, DeactivationDate = _resortVehicle?.DeactivationDate, Available = _resortVehicle?.Available ?? true, ResortVehicleStatusId = _resortVehicle?.ResortVehicleStatusId ?? new ResortVehicleStatus().Available, ResortPropertyId = int.Parse(cmbResortProperty.Text) }; if (Save(vehicle)) { MessageBox.Show(vehicle.Id == 0 ? "Added Successfully" : "Updated Successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show(_errorText); _errorText = ""; } }
/// <summary> /// Francis Mingomba /// Created: 2019/04/03 /// /// Adds a new vehicle to the /// database /// </summary> /// <param name="resortVehicle">resort vehicle object</param> /// <returns>new vehicle id</returns> public int AddVehicle(ResortVehicle resortVehicle) { int id; var conn = DBConnection.GetDbConnection(); const string cmdText = @"sp_create_vehicle"; var cmd = new SqlCommand(cmdText, conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("@Make", resortVehicle.Make); cmd.Parameters.AddWithValue("@Model", resortVehicle.Model); cmd.Parameters.AddWithValue("@Year", resortVehicle.Year); cmd.Parameters.AddWithValue("@License", resortVehicle.License); cmd.Parameters.AddWithValue("@Mileage", resortVehicle.Mileage); cmd.Parameters.AddWithValue("@Capacity", resortVehicle.Capacity); cmd.Parameters.AddWithValue("@Color", resortVehicle.Color); cmd.Parameters.AddWithValue("@PurchaseDate", resortVehicle.PurchaseDate); cmd.Parameters.AddWithValue("@Description", resortVehicle.Description); cmd.Parameters.AddWithValue("@Active", resortVehicle.Active); cmd.Parameters.AddWithValue("@DeactivationDate", (object)resortVehicle.DeactivationDate ?? DBNull.Value); cmd.Parameters.AddWithValue("@Available", resortVehicle.Available); cmd.Parameters.AddWithValue("@ResortVehicleStatusId", resortVehicle.ResortVehicleStatusId); cmd.Parameters.AddWithValue("@ResortPropertyId", resortVehicle.ResortPropertyId); try { conn.Open(); id = Convert.ToInt32(cmd.ExecuteScalar()); } finally { conn.Close(); } return(id); }
public void Setup() { _resortVehicleManager = new ResortVehicleManager(new MockResortVehicleAccessor()); _goodResortVehicle = new ResortVehicle() { Id = 0, Make = "", Model = "", Year = 1990, License = "IA 111", Mileage = 0, Capacity = 0, Color = "", PurchaseDate = DateTime.Now, Description = "", Active = true, DeactivationDate = null, Available = true, ResortVehicleStatusId = "", ResortPropertyId = 0 }; int userId = 0; var roles = new List <Role>() { new Role() { RoleID = _roles.Admin.ToString() } }; _goodUser = new Employee() { EmployeeID = userId, FirstName = "firstName", LastName = "lastName", EmployeeRoles = roles }; }
/// <summary author="Francis Mingomba" created="2019/04/03"> /// Deletes resort vehicle from database. /// This function checks employee privileges /// before functionality is granted /// </summary> /// <param name="resortVehicle"></param> /// <param name="employee"></param> public void DeleteVehicle(ResortVehicle resortVehicle, Employee employee = null) { try { // make sure vehicle is inactive if (resortVehicle.Active) { throw new ApplicationException("Vehicle is active. Deactivate first"); } if (!employee.HasRoles(out string errorStr, "Admin")) { throw new ApplicationException(errorStr); } _resortVehicleAccessor.DeleteVehicle(resortVehicle.Id); } catch (Exception ex) { ExceptionLogManager.getInstance().LogException(ex); throw ex; } }
/// <summary> /// Throws no exceptions /// Intended to do nothing /// </summary> /// <param name="oldResortVehicle"></param> /// <param name="newResortVehicle"></param> public void UpdateVehicle(ResortVehicle oldResortVehicle, ResortVehicle newResortVehicle) { // do nothing }
/// <summary> /// To not have mock drive unit test /// return zero at all times /// </summary> /// <param name="resortVehicle"></param> /// <returns>0</returns> public int AddVehicle(ResortVehicle resortVehicle) { return(0); }
/// <summary> /// Francis Mingomba /// Created: 2019/04/03 /// /// Updates vehicle contents /// </summary> /// <param name="oldResortVehicle">old resort vehicle (mirror to database copy)</param> /// <param name="newResortVehicle">new updated resort vehicle</param> public void UpdateVehicle(ResortVehicle oldResortVehicle, ResortVehicle newResortVehicle) { var conn = DBConnection.GetDbConnection(); const string cmdText = @"sp_update_vehicle"; var cmd = new SqlCommand(cmdText, conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("@VehicleId", oldResortVehicle.Id); cmd.Parameters.AddWithValue("@DeactivationDate", (object)newResortVehicle.DeactivationDate ?? DBNull.Value); cmd.Parameters.AddWithValue("@OldMake", oldResortVehicle.Make); cmd.Parameters.AddWithValue("@OldModel", oldResortVehicle.Model); cmd.Parameters.AddWithValue("@OldYear", oldResortVehicle.Year); cmd.Parameters.AddWithValue("@OldLicense", oldResortVehicle.License); cmd.Parameters.AddWithValue("@OldMileage", oldResortVehicle.Mileage); cmd.Parameters.AddWithValue("@OldCapacity", oldResortVehicle.Capacity); cmd.Parameters.AddWithValue("@OldColor", oldResortVehicle.Color); cmd.Parameters.AddWithValue("@OldPurchaseDate", oldResortVehicle.PurchaseDate); cmd.Parameters.AddWithValue("@OldDescription", oldResortVehicle.Description); cmd.Parameters.AddWithValue("@OldActive", oldResortVehicle.Active); cmd.Parameters.AddWithValue("@OldAvailable", oldResortVehicle.Available); cmd.Parameters.AddWithValue("@OldResortVehicleStatusId", oldResortVehicle.ResortVehicleStatusId); cmd.Parameters.AddWithValue("@OldResortPropertyId", oldResortVehicle.ResortPropertyId); cmd.Parameters.AddWithValue("@NewMake", newResortVehicle.Make); cmd.Parameters.AddWithValue("@NewModel", newResortVehicle.Model); cmd.Parameters.AddWithValue("@NewYear", newResortVehicle.Year); cmd.Parameters.AddWithValue("@NewLicense", newResortVehicle.License); cmd.Parameters.AddWithValue("@NewMileage", newResortVehicle.Mileage); cmd.Parameters.AddWithValue("@NewCapacity", newResortVehicle.Capacity); cmd.Parameters.AddWithValue("@NewColor", newResortVehicle.Color); cmd.Parameters.AddWithValue("@NewPurchaseDate", newResortVehicle.PurchaseDate); cmd.Parameters.AddWithValue("@NewDescription", newResortVehicle.Description); cmd.Parameters.AddWithValue("@NewActive", newResortVehicle.Active); cmd.Parameters.AddWithValue("@NewAvailable", newResortVehicle.Available); cmd.Parameters.AddWithValue("@NewResortVehicleStatusId", newResortVehicle.ResortVehicleStatusId); cmd.Parameters.AddWithValue("@NewResortPropertyId", newResortVehicle.ResortPropertyId); try { conn.Open(); int result = cmd.ExecuteNonQuery(); if (result == 0) { // .. vehicle wasn't found or old and database copy did not match throw new ApplicationException("Database: Vehicle not updated"); } else if (result > 1) { // .. protection against change in expected stored stored procedure behaviour throw new ApplicationException("Fatal Error: More than one vehicle updated"); } } finally { conn.Close(); } }