/// <summary> /// Deletes an aircraft from the user's list. Does NOT remove the underlying aircraft. /// </summary> /// <param name="AircraftID">The ID of the aircraft to delete</param> /// <returns>The updated list of aircraft for the user.</returns> public Aircraft[] FDeleteAircraftforUser(int AircraftID) { if (String.IsNullOrEmpty(User)) { throw new MyFlightbookException("No user specified for Deleteaircraft"); } AircraftStats acs = new AircraftStats(User, AircraftID); // if the user has no flights with this aircraft, simply remove it from their list and be done if (acs.UserFlights != 0) { throw new MyFlightbookException(Resources.Aircraft.errAircraftInUse); } else { new DBHelper().DoNonQuery("DELETE FROM useraircraft WHERE userName=?username AND idAircraft=?aircraftID", (comm) => { comm.Parameters.AddWithValue("username", User); comm.Parameters.AddWithValue("aircraftID", AircraftID); } ); InvalidateCache(); } // Delete any deadlines associated with this aircraft foreach (DeadlineCurrency dc in DeadlineCurrency.DeadlinesForUser(User, AircraftID)) { dc.FDelete(); } // And delete any custom currencies associated with the aircraft foreach (CustomCurrency cc in CustomCurrency.CustomCurrenciesForUser(User)) { List <int> ids = new List <int>(cc.AircraftRestriction); if (ids.Contains(AircraftID)) { ids.Remove(AircraftID); cc.AircraftRestriction = ids; cc.FCommit(); } } // we don't actually delete the aircraft; no need to do so, even if it's not used by anybody because // (a) we can't force caches of aircraft lists to be invalid and, // (b) no harm from keeping it - if somebody re-uses the tailnumber, it will re-use the existing flight. return(GetAircraftForUser()); }
/// <summary> /// Replaces one aircraft for another for the user. The old aircraft is not migrated if any existing flights use it. /// </summary> /// <param name="acNew">The new aircraft</param> /// <param name="acOld">The old aircraft</param> /// <param name="fMigrateFlights">True to migrate any existing flights. If false, the old flights (and the old aircraft) may remain.</param> public void ReplaceAircraftForUser(Aircraft acNew, Aircraft acOld, bool fMigrateFlights) { if (acNew == null) { throw new ArgumentNullException("acNew"); } if (acOld == null) { throw new ArgumentNullException("acOld"); } if (acNew.AircraftID == acOld.AircraftID) { return; } // Add the new aircraft first FAddAircraftForUser(acNew); // Migrate any flights, if necessary... if (fMigrateFlights) { List <Aircraft> lstAc = new List <Aircraft>(GetAircraftForUser()); // make sure we are populated with both old and new so that UpdateFlightAircraftForUser works. // (This can happen if you have one version of an aircraft and you go to add another version of it; // they won't both be there, but the query used in UpdateFlightAircraftForUser wants them both present. if (!lstAc.Exists(ac => ac.AircraftID == acNew.AircraftID)) { lstAc.Add(acNew); } if (!lstAc.Exists(ac => ac.AircraftID == acOld.AircraftID)) { lstAc.Add(acOld); } CachedAircraft = lstAc.ToArray(); // we'll nullify the cache below. LogbookEntry.UpdateFlightAircraftForUser(this.User, acOld.AircraftID, acNew.AircraftID); // Migrate any custom currencies associated with the aircraft foreach (CustomCurrency cc in CustomCurrency.CustomCurrenciesForUser(User)) { List <int> lst = new List <int>(cc.AircraftRestriction); for (int i = 0; i < lst.Count; i++) { if (lst[i] == acOld.AircraftID) { lst[i] = acNew.AircraftID; cc.AircraftRestriction = lst; cc.FCommit(); break; } } } // And migrate any deadlines associated with the aircraft foreach (DeadlineCurrency dc in DeadlineCurrency.DeadlinesForUser(User, acOld.AircraftID)) { dc.AircraftID = acNew.AircraftID; dc.FCommit(); } try { // Then delete the old aircraft. Ignore any errors FDeleteAircraftforUser(acOld.AircraftID); } catch (MyFlightbookException) { InvalidateCache(); } } }