public async Task AddRoute(string iataAirlineCode, string originAirportCode, string destinationAirportCode, List <FlightDuration> flightDurations, List <ClassPricingScheme> pricingSchemes) { if (await DoesRouteExist(iataAirlineCode, originAirportCode, destinationAirportCode)) { throw new InvalidOperationException("Route already exists."); } using FlightReservationContext db = await Task.Run(() => new FlightReservationContext ()); Route route = new Route { AirlineCode = iataAirlineCode, OriginAirportCode = originAirportCode, DestinationAirportCode = destinationAirportCode, PricingScheme = JsonConvert.SerializeObject(pricingSchemes) }; db.Route.Add(route); await db.SaveChangesAsync(); foreach (FlightDuration fd in flightDurations) { RouteAircraft aircraftRoute = new RouteAircraft { RouteId = route.RouteId, IcaoTypeCode = fd.Aircraft.IcaoTypeCode, FlightDuration = fd.DurationHours * 60 + fd.DurationMinutes }; db.RouteAircraft.Add(aircraftRoute); } await db.SaveChangesAsync(); }
public void AddFlightTest() { using (FlightReservationContext db = new FlightReservationContext()) { List <ClassPricingScheme> pricingScheme = new List <ClassPricingScheme> { new ClassPricingScheme { TravelClassCode = "E", TravelClassName = "Economy Class", AdultFare = 200, ChildFare = 160, InfantFare = 40 }, new ClassPricingScheme { TravelClassCode = "B", TravelClassName = "Business Class", AdultFare = 1000, ChildFare = 800, InfantFare = 200 } }; Route newRoute = new Route { AirlineCode = "BA", OriginAirportCode = "BHX", DestinationAirportCode = "ARN", PricingScheme = JsonConvert.SerializeObject(pricingScheme) }; db.Route.Add(newRoute); db.SaveChanges(); RouteAircraft newRouteAircraft = new RouteAircraft { RouteId = newRoute.RouteId, IcaoTypeCode = "A320", FlightDuration = 105 }; db.RouteAircraft.Add(newRouteAircraft); db.SaveChanges(); m_crudManager.AddFlight(newRouteAircraft.RouteAircraftId, "BA188", new DateTime(2020, 9, 6, 15, 30, 0)); var query = from f in db.Flight join ra in db.RouteAircraft on f.RouteAircraftId equals ra.RouteAircraftId join r in db.Route on ra.RouteId equals r.RouteId where r.AirlineCode == "BA" && r.OriginAirportCode == "BHX" && r.DestinationAirportCode == "ARN" select f; List <Flight> selectedFlights = query.ToList(); Assert.IsTrue(selectedFlights.Exists(f => f.RouteAircraftId == newRouteAircraft.RouteAircraftId && f.FlightNumber == "BA188" && f.ScheduledDeparture == new DateTime(2020, 9, 6, 15, 30, 0))); Flight flightToDelete = selectedFlights.Find(f => f.RouteAircraftId == newRouteAircraft.RouteAircraftId && f.FlightNumber == "BA188" && f.ScheduledDeparture == new DateTime(2020, 9, 6, 15, 30, 0)); if (flightToDelete != null) { db.Flight.Remove(flightToDelete); } db.RouteAircraft.Remove(newRouteAircraft); db.Route.Remove(newRoute); db.SaveChanges(); } }