public void DeleteVehicle(VehicleModel vehicle) { // If this is the default vehicle, change it if (vehicle.IsDefaultVehicle && AllVehicles.Count > 1) { VehicleModel NewDefaultVehicle = AllVehicles.First(v => v.VehicleId != vehicle.VehicleId); NewDefaultVehicle.IsDefaultVehicle = true; DefaultVehicle = NewDefaultVehicle; } // Check if the vehicle has fillups and remove those foreach (FillupModel f in vehicle.Fillups) { _db.Fillups.DeleteOnSubmit(f); } // Check if the vehicle has reminders and remove those foreach (ReminderModel r in vehicle.Reminders) { _db.Reminders.DeleteOnSubmit(r); } // Remove the vehicle from the "all" observable collection. AllVehicles.Remove(vehicle); _db.Vehicles.DeleteOnSubmit(vehicle); _db.SubmitChanges(); calcDefault(); }
// Initializes and cleans up default vehicle public void initDefault() { if (DefaultVehicle == null) { if (AllVehicles.Count > 0) { if (AllVehicles.Count(v => v.IsDefaultVehicle == true) == 1) { DefaultVehicle = AllVehicles.First(v => v.IsDefaultVehicle == true); } else { DefaultVehicle = AllVehicles.First(); } } } }
// Add vehicle to database public void AddVehicle(VehicleModel vehicle) { // Check if this is first vehicle... if so, make it default if (_db.Vehicles.Count() == 0) { vehicle.IsDefaultVehicle = true; DefaultVehicle = vehicle; } // Add default reminders ReminderModel reminderOil = new ReminderModel(); reminderOil.ReminderType = "oil change"; reminderOil.RemindDate = false; reminderOil.RemindDateValue = System.DateTime.Now; reminderOil.RemindOdo = false; vehicle.Reminders.Add(reminderOil); ReminderModel reminderRotate = new ReminderModel(); reminderRotate.ReminderType = "rotate tires"; reminderRotate.RemindDate = false; reminderRotate.RemindDateValue = System.DateTime.Now; reminderRotate.RemindOdo = false; vehicle.Reminders.Add(reminderRotate); ReminderModel reminderFluids = new ReminderModel(); reminderFluids.ReminderType = "check fluids"; reminderFluids.RemindDate = false; reminderFluids.RemindDateValue = System.DateTime.Now; reminderFluids.RemindOdo = false; vehicle.Reminders.Add(reminderFluids); _db.Vehicles.InsertOnSubmit(vehicle); _db.SubmitChanges(); // Add the vehicle to the "all" observable collection. AllVehicles.Add(vehicle); }
public IEnumerable <AllVehicles> VehicleList() { List <AllVehicles> vehicles = new List <AllVehicles>(); using (var cn = new SqlConnection(Settings.GetConnectionString())) { SqlCommand cmd = new SqlCommand("SelectAllVehicles", cn); cmd.CommandType = CommandType.StoredProcedure; cn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { AllVehicles row = new AllVehicles(); row.VehicleId = (int)dr["VehicleId"]; row.Type = dr["Type"].ToString(); row.BodyStyle = dr["BodyStyle"].ToString(); row.Year = dr["Year"].ToString(); row.Transmission = dr["Transmission"].ToString(); row.Color = dr["Color"].ToString(); row.Interior = dr["Interior"].ToString(); row.Miles = dr["Miles"].ToString(); row.Vin = dr["Vin"].ToString(); row.Price = (decimal)dr["Price"]; row.Msrp = (decimal)dr["Msrp"]; row.Description = dr["Description"].ToString(); row.ModelName = dr["ModelName"].ToString(); row.MakeName = dr["MakeName"].ToString(); if (dr["ImageFileName"] != DBNull.Value) { row.ImageFileName = dr["ImageFileName"].ToString(); } vehicles.Add(row); } } } return(vehicles); }