/// <summary> /// Validates if license plate number is correct. /// </summary> /// <returns>Dictionary with key = ture if license plate number is correct, key = false if license plate number is incorrect.</returns> public Dictionary <Boolean, string> ValidateLicensePlatenumber() { Dictionary <Boolean, string> validationResult = new Dictionary <bool, string>(); Boolean valid = true; string errorMsg = ""; if (String.IsNullOrWhiteSpace(LicensePlateNum)) { valid = false; errorMsg += "Numer rejestracyjny samochodu nie może zostać pusty!"; } else if (LicensePlateNum.Trim().Length != 7) { valid = false; errorMsg += $"Wprowadzony numer rejestracyjny samochodu jest nieprawiłowy! ({LicensePlateNum.Trim().Length} znaków zamiast 7)"; } else if (!Char.IsLetter(LicensePlateNum.ToCharArray()[0]) || !Char.IsLetter(LicensePlateNum.ToCharArray()[1])) { valid = false; errorMsg += $"Wprowadzony numer rejestracyjny samochodu jest nieprawiłowy! (Co najmniej 2 pierwsze znaki muszą być literami)"; } validationResult.Add(valid, errorMsg); return(validationResult); }
/// <summary> /// Save new car to database. /// </summary> /// <returns>True if save succeeded, false if save failed.</returns> public bool AddNewCar() { bool success = false; using (SqlConnection sqlConnection = new SqlConnection()) { sqlConnection.ConnectionString = Helpers.connectionString; sqlConnection.Open(); SqlCommand command = new SqlCommand("INSERT INTO samochody (klasa, marka, model, [rok produkcji], [rodzaj napędu], [skrzynia biegów], silnik, status, [koszt wynajęcia], vin, [numer rejestracyjny]) " + $"VALUES(@category, @manufacturer, @model, @productionDate, @driveType, @gearbox, @engine, @status, @cost, @VIN, @licensePlateNum)", sqlConnection); command.Parameters.Add(new SqlParameter("category", Category)); command.Parameters.Add(new SqlParameter("manufacturer", Manufacturer.ToUpper())); command.Parameters.Add(new SqlParameter("model", Model.ToUpper())); command.Parameters.Add(new SqlParameter("productionDate", ProductionDate.ToString("yyyy-MM-dd"))); command.Parameters.Add(new SqlParameter("driveType", DriveType)); command.Parameters.Add(new SqlParameter("gearbox", Gearbox)); command.Parameters.Add(new SqlParameter("engine", Engine)); command.Parameters.Add(new SqlParameter("status", EnumStatus.Dostępny.ToString())); command.Parameters.Add(new SqlParameter("cost", Cost)); command.Parameters.Add(new SqlParameter("VIN", VIN.ToUpper())); command.Parameters.Add(new SqlParameter("licensePlateNum", LicensePlateNum.ToUpper())); try { command.ExecuteNonQuery(); success = true; } catch (Exception) { throw; } } return(success); }