// CAR VALIDATIONS public string CarPlateValidation(string carPlate) { DatabaseRequests request = new DatabaseRequests(); string errorMessage; if (string.IsNullOrWhiteSpace(carPlate)) { errorMessage = "This field is required"; } else if (!Regex.IsMatch(carPlate, @"[A-Za-z]{2}\s[0-9]{2}\s[A-Za-z]{3}")) { errorMessage = "Please use the correct format. E.g. AB 12 ABC"; } else { errorMessage = request.GetCarIDByPlate(carPlate) == 0 ? "This Car Plate doesn't exist in the database" : ""; } return(errorMessage); }
public string ClientIDValidation(string clientID) { DatabaseRequests request = new DatabaseRequests(); string errorMessage; int ID; if (string.IsNullOrWhiteSpace(clientID)) { errorMessage = "This field is required"; } else if (!int.TryParse(clientID, out ID)) { errorMessage = "Please use only numbers for the Client ID"; } else { errorMessage = (request.GetClientID(clientID) == 0 ? "This Client ID doesn't exist" : ""); } return(errorMessage); }
// NEW CAR RENT VALIDATIONS public Tuple <List <string>, bool> ValidateCarRent(string plate, string clientID, string startDate, string endDate, string city) { Reservation reservation = new Reservation(); Car car = new Car(); Customer customer = new Customer(); DatabaseRequests request = new DatabaseRequests(); List <string> errors = new List <string>(); string plateError = ""; string IDError = ""; string startDateError = ""; string endDateError = ""; string cityError = ""; string overlapDates = ""; // Plate check plateError = CarPlateValidation(plate); // ClientID check IDError = ClientIDValidation(clientID); // Start-End Date check startDateError = DateValidation(startDate); if (startDateError == "") { startDateError = CompareDateValidation(DateTime.Today, DateTime.Parse(startDate)); if (startDateError == "") { endDateError = DateValidation(endDate); if (endDateError == "") { endDateError = CompareDateValidation(DateTime.Parse(startDate), DateTime.Parse(endDate)); if (endDateError != "") { endDateError += " the start date"; } } } else { startDateError += " today"; } } // Date Overlap check if (startDateError == "" && endDateError == "" && plateError == "") { plateError = request.CheckDateOverlap(DateTime.Parse(startDate), DateTime.Parse(endDate), request.GetCarIDByPlate(plate)); } // City check cityError = LocationValidation(city); // Return true if all the data is correct/no error message if (plateError == "" && IDError == "" && startDateError == "" && endDateError == "" && cityError == "" && overlapDates == "") { return(Tuple.Create(errors, true)); } errors.Add(plateError); errors.Add(IDError); errors.Add(startDateError); errors.Add(endDateError); errors.Add(cityError); return(Tuple.Create(errors, false)); }
public string GetRentForUpdate(string plate, string clientID, string startDate, CarRent rent) { if (plate != null || clientID != "0") { Car car = new Car(); Customer customer = new Customer(); Validations validation = new Validations(); DatabaseRequests request = new DatabaseRequests(); string IDError = ""; string plateError = ""; if (plate != null) { plateError = validation.CarPlateValidation(plate); } else { plateError = "This field is required"; } if (clientID != "0") { IDError = validation.ClientIDValidation(clientID); } else { IDError = "This field is required"; } if ((plateError == "" && IDError == "This field is required") || (IDError == "" && plateError == "This field is required") || (plateError == "" && IDError == "")) { DatabaseConnection connection = new DatabaseConnection(); List <Reservation> res = new List <Reservation>(); string getRent = ""; string lookFor = ""; string dates = ""; if (plateError == "" && IDError == "" && startDate != null) { getRent = "SELECT CarID, CustomerID, StartDate, EndDate, Location FROM Reservations WHERE CustomerID = @CustomerID AND CarID = @CarID AND StartDate = @StartDate AND ReservStatsID = 1"; lookFor = "plateAndIDAndDate"; } else if (plateError == "" && IDError == "") { getRent = "SELECT CarID, CustomerID, StartDate, EndDate, Location FROM Reservations WHERE CustomerID = @CustomerID AND CarID = @CarID AND ReservStatsID = 1"; lookFor = "plateAndID"; } else if (plateError == "") { getRent = "SELECT CarID, CustomerID, StartDate, EndDate, Location FROM Reservations WHERE CarID = @CarID AND ReservStatsID = 1"; lookFor = "plate"; } else if (IDError == "") { getRent = "SELECT CarID, CustomerID, StartDate, EndDate, Location FROM Reservations WHERE CustomerID = @CustomerID AND ReservStatsID = 1"; lookFor = "ID"; } SqlCommand command = new SqlCommand(getRent); if (lookFor == "plateAndID" && plateError == "" && IDError == "") { car.CarID = GetCarIDByPlate(plate); command.Parameters.AddWithValue("CarID", car.CarID); command.Parameters.AddWithValue("CustomerID", int.Parse(clientID)); } else if (lookFor == "plate" && plateError == "") { car.CarID = GetCarIDByPlate(plate); command.Parameters.AddWithValue("CarID", car.CarID); } else if (lookFor == "ID" && IDError == "") { command.Parameters.AddWithValue("CustomerID", int.Parse(clientID)); } else if (lookFor == "plateAndIDAndDate" && plateError == "" && IDError == "") { car.CarID = GetCarIDByPlate(plate); command.Parameters.AddWithValue("CarID", car.CarID); command.Parameters.AddWithValue("CustomerID", int.Parse(clientID)); command.Parameters.AddWithValue("StartDate", DateTime.Parse(startDate)); } res = connection.GetRentForUpdate(command); IDError = ""; plateError = ""; string errorMessage = ""; if (lookFor == "plate") { if (res.Count > 1) { errorMessage = "This car is rented more than 1 time. Please add the Client ID field and press Search again"; } else if (res.Count == 0) { errorMessage = "There is no active rent for this car"; } } else if (lookFor == "ID") { if (res.Count > 1) { errorMessage = "This client has multiple active rents. Please add the Car Plate and press Search again"; } else if (res.Count == 0) { errorMessage = "There is no active rent for this customer"; } } else if (lookFor == "plateAndID") { if (res.Count > 1) { for (int i = 0; i < res.Count; i++) { dates += "\n" + res[i].StartDate.ToShortDateString(); } errorMessage = "This car is rented for this customer on the following start dates :" + dates + "\nPlease select one of these dates as a start date before pressing Search."; } else if (res.Count == 0) { errorMessage = "There is no active rent for this customer/car"; } } else { //StartDateTimePicker.CustomFormat = " "; //StartDateTimePicker.Checked = false; errorMessage = "There isn't a rent for this client and car on this date. Please press Search again using only the Car Plate and Client ID"; } if (res.Count == 1) { //StartDateTimePicker.Checked = true; //StartDateTimePicker.CustomFormat = "yyyy-MM-dd"; //EndDateTimePicker.CustomFormat = "yyyy-MM-dd"; rent.Reservation.CustomerID = res[0].CustomerID; rent.Car.Plate = request.GetCarPlateByID(res[0].CarID); rent.StartDate = res[0].StartDate.ToShortDateString(); rent.EndDate = res[0].EndDate.ToShortDateString(); rent.Reservation.Location = res[0].Location; } else { return(errorMessage); } } return(IDError + "/" + plateError); //IDErrorLabel.Text = IDError; //PlateErrorLabel.Text = plateError; } else { return("Please fill the Car Plate field or the Client ID field before pressing Search"); } }