Example #1
0
        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            VehiclesSqlRepository vehiclesSqlRepository = new VehiclesSqlRepository();

            vehiclesSqlRepository.UpdateVehicleAvailability(travelWarrant.VehicleId, true);

            bool success = travelWarrantsSqlRepository.Delete(travelWarrant.Id);

            if (success)
            {
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show("Fail");
            }
        }
        private void UpdateTravelWarrant()
        {
            if (CbStatus.Text != string.Empty && TbDate.Text != string.Empty && TbTime.Text != string.Empty && CbDrivers.Text != string.Empty && CbVehicles.Text != string.Empty)
            {
                if (DateTime.TryParse(TbDate.Text, out DateTime date) && (TimeSpan.TryParse(TbTime.Text, out TimeSpan time)))
                {
                    if ((Regex.IsMatch(TbDate.Text, @"\d\d/\d\d/\d\d\d\d") || Regex.IsMatch(TbDate.Text, @"\d/\d\d/\d\d\d\d")) && Regex.IsMatch(TbTime.Text, @"\d\d:\d\d:\d\d"))
                    {
                        int driverId      = int.Parse(CbDrivers.Text.Substring(0, CbDrivers.Text.IndexOf(' ')));
                        int vehicleId     = int.Parse(CbVehicles.Text.Substring(0, CbVehicles.Text.IndexOf(' ')));
                        int fuelCostId    = 0;
                        int travelRouteId = 0;

                        foreach (FuelCostPanel fuelCostPanel in SpFuelCosts.Children)
                        {
                            if (fuelCostPanel.RbFcSelect.IsChecked == true)
                            {
                                fuelCostId = int.Parse(fuelCostPanel.LblId.Text);
                            }
                        }

                        foreach (TravelRoutePanel travelRoutePanel in SpTravelRoutes.Children)
                        {
                            if (travelRoutePanel.RbTrSelect.IsChecked == true)
                            {
                                travelRouteId = int.Parse(travelRoutePanel.LblId.Text);
                            }
                        }

                        TravelWarrant updatedTravelWarrant = new TravelWarrant
                        {
                            Id            = travelWarrant.Id,
                            WarrantStatus = CbStatus.Text,
                            DateIssued    = date,
                            TimeIssued    = time,
                            DriverId      = driverId,
                            VehicleId     = vehicleId,
                            FuelCostId    = fuelCostId,
                            TravelRouteId = travelRouteId
                        };

                        int oldVehicleId = travelWarrant.VehicleId;
                        vehiclesSqlRepository.UpdateVehicleAvailability(oldVehicleId, true);

                        vehiclesSqlRepository.UpdateVehicleAvailability(vehicleId, false);

                        bool success = travelWarrantsSqlRepository.Update(updatedTravelWarrant);

                        if (success)
                        {
                            MessageBox.Show("Success");
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("Fail");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Date format must be: \"MM/dd/yyyy\"\nTime format must be: \"HH:MM:ss\"");
                    }
                }
                else
                {
                    MessageBox.Show("Date format must be: \"MM/dd/yyyy\"\nTime format must be: \"HH:MM:ss\"");
                }
            }
            else
            {
                MessageBox.Show("All input fields are required");
            }
        }