public async Task <IActionResult> PutFlight(string id, Flight flight) { if (id != flight.FlightId) { return(BadRequest()); } _context.Entry(flight).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FlightExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create([Bind("ID,FlightID,Origin,Destination,Date,DepartureTime,ArrivalTime,DistanceTravelled")] Flight flight) { if (ModelState.IsValid) { _context.Add(flight); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(flight)); }
public async Task <IActionResult> Create([Bind("ID,From,To,Stops,Price,departing,arrival,seatsAvailable")] AirRoutes airRoutes) { if (ModelState.IsValid) { _context.Add(airRoutes); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(airRoutes)); }
public async Task <IActionResult> Create([Bind("FlightId,AircraftType,FromLocation,ToLocation,DepartureTime,ArrivalTime")] Flight flight) { if (ModelState.IsValid) { _context.Add(flight); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(flight)); }
public async Task <IActionResult> Create([Bind("FlightTicketID,FlightNumber,FlightDestination")] FlightTicket flightTicket) { if (ModelState.IsValid) { _context.Add(flightTicket); _logger.LogInformation("Added flight"); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(flightTicket)); }
public async Task <IActionResult> Create([Bind("RouteID,FromCityID,ToCityID,BasicFare,AveDuration")] Route route) { if (ModelState.IsValid) { _context.Add(route); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name", route.FromCityID); ViewData["ToCityID"] = new SelectList(_context.Cities, "CityID", "Name", route.ToCityID); return(View(route)); }
public async Task <IActionResult> Create([Bind("CityID,Name,AirportName,PostCode,StateOrTerritory,State")] City city) { city.State = city.StateOrTerritory.ToString(); if (ModelState.IsValid) { _context.Add(city); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(city)); }
public async Task <ActionResult <Server> > PostServer([FromBody] Server serverJson) { _context.Server.Add(serverJson); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(Conflict()); } return(CreatedAtAction("GetServer", new { id = serverJson.ServerId }, serverJson)); }
public async Task <ActionResult <FlightPlan> > DeleteFlightPlan(string id) { // Find the flight using the id from the DB. var flightPlan = await _context.FlightItems.Where(x => x.FlightId == id).FirstAsync(); if (flightPlan == null) { return(NotFound()); } _context.FlightItems.Remove(flightPlan); await _context.SaveChangesAsync(); return(flightPlan); }
public async Task <IActionResult> Create([Bind("Id,Name,Email,FlightTicketID")] Reservation reservation) { if (ModelState.IsValid) { _context.Add(reservation); _logger.LogInformation("added booking"); await _context.SaveChangesAsync(); var ticket = (from f in _context.Reservations where f.Id == reservation.Id select f).First(); return(RedirectToAction("Edit", "Reservations", ticket)); } ViewData["FlightTicketID"] = new SelectList(_context.FlightTickets, "FlightTicketID", "FlightTicketID", reservation.FlightTicketID); return(View(reservation)); }
public static async Task <string> deleteFlightFromDatabase(WebhookRequest request, FlightContext _dbContext) { var requestParameters = request.QueryResult.Parameters; string fromCity = requestParameters.Fields["from-city"].StringValue; string toCity = requestParameters.Fields["to-city"].StringValue; DateTime departure = DateTimeOffset.Parse(requestParameters.Fields["date-time"].StringValue).UtcDateTime.Date; var flight = await _dbContext.Flights.FirstOrDefaultAsync(f => f.FromCity.ToLower().Contains(fromCity.ToLower()) && f.ToCity.ToLower().Contains(toCity.ToLower()) && f.DepartureDate.Date == departure ); if (flight == null) { textToReturn = "There is no such flight"; } else { _dbContext.Flights.Remove(flight); await _dbContext.SaveChangesAsync(); textToReturn = "Cancelled!"; } return(textToReturn); }
public static async Task <string> addFlightToDatabase(WebhookRequest request, FlightsAPIContext _apiContext, FlightContext _dbContext) { // Extracts the parameters of the request var requestParameters = request.QueryResult.Parameters; string fromCity = requestParameters.Fields["from-city"].StringValue; string toCity = requestParameters.Fields["to-city"].StringValue; DateTime departure = DateTimeOffset.Parse(requestParameters.Fields["date-time"].StringValue).UtcDateTime.Date; var flight = await _apiContext.GetFlight(fromCity, toCity, departure); if (flight.Quotes.Any()) { FlightModel flightItem = new FlightModel { FromCity = flight.Places.Where(p => p.PlaceId == flight.Quotes[0].OutboundLeg.OriginId).First().Name, ToCity = flight.Places.Where(p => p.PlaceId == flight.Quotes[0].OutboundLeg.DestinationId).First().Name, DepartureDate = flight.Quotes[0].OutboundLeg.DepartureDate.Date, Price = flight.Quotes[0].MinPrice, }; _dbContext.Add(flightItem); await _dbContext.SaveChangesAsync(); textToReturn = "Done!"; } else { textToReturn = "Could not add a flight"; } return(textToReturn); }
public async Task <ActionResult <FlightPlan> > PostFlightPlan([FromBody] FlightPlan jsonFlight) { // Give the new flight plan an ID. jsonFlight.FlightId = SetFlightId(jsonFlight.CompanyName); // Add it to the DB. _context.FlightItems.Add(jsonFlight); await _context.SaveChangesAsync(); return(CreatedAtAction("GetFlightPlan", new { id = jsonFlight.Id }, jsonFlight)); }
public async Task <ActionResult <Server> > PostServer(Server server) { _context.ServerItems.Add(server); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (ServerExists(server.ServerId)) { return(Conflict()); } else { throw; } } return(CreatedAtAction ("GetAllServers", new { id = server.ServerId }, server)); }
public async Task <IActionResult> Create(int FromCityID, int ToCityID) { var flightSchedule = new Flight_Schedule { }; if (await TryUpdateModelAsync <Flight_Schedule>(flightSchedule, "", c => c.Flight_ScheduleID, c => c.RouteID, c => c.DepartDateTime, c => c.ArriveDateTime, c => c.AirCraftID, c => c.Economy, c => c.Business, c => c.PremEconomy, c => c.First)) { //if the route in this schedule does not exist, then reject to create if (!await _context.Routes.AnyAsync(r => r.FromCity.CityID == FromCityID && r.ToCity.CityID == ToCityID)) //get rid of the route that does not exist { ViewBag.ErrorNoRoute = "No such route exists!"; ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name"); ViewData["ToCityID"] = new SelectList(_context.Cities, "CityID", "Name"); return(View(flightSchedule)); } int RouteID = await _context.Routes.Where(r => r.FromCity.CityID == FromCityID && r.ToCity.CityID == ToCityID).Select(r => r.RouteID).FirstAsync(); //get the ID of route between2 cities flightSchedule.Route = await _context.Routes.Where(r => r.RouteID == RouteID).FirstAsync(); //set the route in the new Flight_Schedule try { _context.Add(flightSchedule); await _context.SaveChangesAsync(); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } return(RedirectToAction(nameof(Index))); } ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name"); ViewData["ToCityID"] = new SelectList(_context.Cities, "CityID", "Name"); return(View(flightSchedule)); }
/* * Add test objects to the DB context */ private static async Task AddElementsToDb(FlightContext context, Flight testFlight, FlightPlan testFlightPlan, InitialLocation testInitialLocation, Segment testSegmentFirst, Segment testSegmentSecond) { await context.FlightItems.AddAsync(testFlight); await context.FlightPlanItems.AddAsync(testFlightPlan); await context.InitialLocationItems.AddAsync(testInitialLocation); await context.SegmentItems.AddAsync(testSegmentFirst); await context.SegmentItems.AddAsync(testSegmentSecond); await context.SaveChangesAsync(); }
public async void UpdateSeatsAsync(List <int> tripList, string cabin, int passangers) { switch (cabin) { case "Economy": foreach (var t in tripList) { await _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ForEachAsync(f => f.Economy = f.Economy - passangers); } break; case "Business": foreach (var t in tripList) { await _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ForEachAsync(f => f.Business = f.Business - passangers); } break; case "PremEconomy": foreach (var t in tripList) { await _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ForEachAsync(f => f.PremEconomy = f.PremEconomy - passangers); } break; case "First": foreach (var t in tripList) { await _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ForEachAsync(f => f.First = f.First - passangers); } break; } try { await _context.SaveChangesAsync(); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } }
/* * This method updates all the related DBs with the new objects we just created * from the data we received. we return conflict if the object is already exist in our DBs * otherwise, we show the new flight data. */ private async Task <IActionResult> UpdateDb(Flight newFlight, FlightPlan newFlightPlan, InitialLocation newInitialLocation) { try { await _flightContext.SaveChangesAsync(); } catch (DbUpdateException e) { if (FlightExists(newFlight.Id) || FlightPlanExists(newFlightPlan.Id) || InitialLocationExists(newInitialLocation.Id)) { return(Conflict()); } Debug.WriteLine(e.Message); throw; } return(CreatedAtAction ("GetFlightPlan", new { id = newFlightPlan.FlightId }, newFlightPlan)); }
public async Task SaveAsync() { await _context.SaveChangesAsync(); }
public async Task <int> SaveChanges() { return(await _db.SaveChangesAsync()); }