public async Task <IActionResult> PutFlight(string id, Flight flight) { if (id.CompareTo(flight.FlightID) != 0) { 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> PutServer(string id, Server server) { if (id.CompareTo(server.ServerID) != 0) { return(BadRequest()); } context.Entry(server).State = EntityState.Modified; try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ServerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutFlight([FromRoute] int id, [FromBody] Flight flight) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } 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 <ActionResult <Server> > PostServer(Server server) { //Adding slash if in the end of url. if (server.ServerURL.Last() != '/') { server.ServerURL += "/"; } _context.Server.Add(server); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (ServerExists(server.ServerID)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetServer", new { id = server.ServerID }, server)); }
public async Task <IActionResult> Create(ReservationCreateViewModel model) { if (ModelState.IsValid) { Reservation _reservation = new Reservation { Id = Guid.NewGuid().ToString(), Name = model.Name, MiddleName = model.MiddleName, Surname = model.Surname, PersonalId = model.PersonalId, Telephone = model.Telephone, Nationality = model.Nationality, FlightTypeId = _context.FlightTypes.FirstOrDefault(f => f.Type == model.TypeName).Id, PlaneId = model.PlaneId }; var CapacityBuisness = _context.Flights.FirstOrDefault(f => f.Id == model.PlaneId).CapacityBuisness; var CapacityNormal = _context.Flights.FirstOrDefault(f => f.Id == model.PlaneId).CapacityNormal; if (model.TypeName == "Buisness Class") { if (--CapacityBuisness < 0) { ViewBag.Message = $"No more seats left in the Buisness Class"; return(View(model)); } _context.Flights.FirstOrDefault(f => f.Id == model.PlaneId).CapacityBuisness--; await _context.SaveChangesAsync(); } else { if (--CapacityNormal < 0) { ViewBag.Message = $"No more seats left in the Economy Class"; return(View(model)); } _context.Flights.FirstOrDefault(f => f.Id == model.PlaneId).CapacityNormal--; await _context.SaveChangesAsync(); } _context.Add(_reservation); await _context.SaveChangesAsync(); return(Redirect("/")); } return(View(model)); }
public async Task <IActionResult> Create(FlightCreateViewModel model) { if (ModelState.IsValid) { Flight flight = new Flight { Id = Guid.NewGuid().ToString(), LocationFrom = model.LocationFrom, LocationTo = model.LocationTo, FlightTakeOff = model.FlightTakeOff, FlightLanding = model.FlightLanding, PlaneModel = model.PlaneModel, PlaneId = model.PlaneId, PilotName = model.PilotName, CapacityNormal = model.CapacityNormal, CapacityBuisness = model.CapacityBuisness }; var checkFlight = _context.Flights.FirstOrDefault(f => f.PlaneId == flight.PlaneId); if (checkFlight != null) { ViewBag.Message = $"Plane id {flight.PlaneId} already exists"; return(View(model)); } _context.Add(flight); await _context.SaveChangesAsync(); return(Redirect("/Identity/FlightList")); } return(View(model)); }
public async Task <IActionResult> DeleteConfirmed(int id) { var flightViewModel = await _context.Flight.FindAsync(id); _context.Flight.Remove(flightViewModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult <FlightPlan> > DeleteFlight(string id) { var fp = await _context.FlightPlan.FindAsync(id); if (fp == null) { return(NotFound()); } _context.FlightPlan.Remove(fp); await _context.SaveChangesAsync(); return(fp); }
public async Task <ActionResult <FlightPlan> > PostFlightPlan(FlightPlan flightPlan) { _context.FlightPlan.Add(flightPlan); //Generate a unique key for new flight. do { flightPlan.FlightID = FlightPlan.GenerateFlightKey(); //As long as generated key is identical to a key in DB, generate a new one. } while (_context.FlightPlan.Count(x => x.FlightID == flightPlan.FlightID) > 0); await _context.SaveChangesAsync(); return(CreatedAtAction("GetFlightPlan", new { id = flightPlan.FlightID }, flightPlan)); }
public async Task <IActionResult> OnPostDelete(string id) { var flight = await context.Flights.FindAsync(id); if (flight == null) { return(NotFound()); } context.Flights.Remove(flight); await context.SaveChangesAsync(); return(Redirect("FlightList/Index")); }
public async Task <IActionResult> OnPostDelete(string id) { var user = await context.Users.FindAsync(id); if (user == null) { return(NotFound()); } context.Users.Remove(user); await context.SaveChangesAsync(); return(Redirect("UserList/Index")); }
public async Task <IEnumerable <Flight> > Get() { HttpClient client = new HttpClient(); var responseStream = await client.GetStringAsync("https://opensky-network.org/api/states/all"); var rawFlights = JsonSerializer.Deserialize <RawFlights>(responseStream); var currentFlights = rawFlights.states .Where(data => (data?.Length ?? 0) >= 12) .Select(raw => Flight.FromRawData(raw)) .ToDictionary(f => f.Icao24); var dbFlights = await dbContext.Flights.ToListAsync(); var dbFlightsIds = dbFlights.Select(f => f.Icao24).ToHashSet(); var dbExistingFlights = dbFlights.Where(f => currentFlights.ContainsKey(f.Icao24) && f.LastContact < currentFlights[f.Icao24].LastContact); foreach (var flight in dbExistingFlights) { var currFlight = currentFlights[flight.Icao24]; flight.BaroAltitude = currFlight.BaroAltitude; flight.CallSign = currFlight.CallSign; flight.LastContact = currFlight.LastContact; flight.Latitude = currFlight.Latitude; flight.Longitude = currFlight.Longitude; flight.OnGround = currFlight.OnGround; flight.OriginCountry = currFlight.OriginCountry; flight.TimePosition = currFlight.TimePosition; flight.TrueTrack = currFlight.TrueTrack; flight.Velocity = currFlight.Velocity; flight.VerticalRate = currFlight.VerticalRate; } var dbNewFlights = currentFlights.Where(f => !dbFlightsIds.Contains(f.Key)); dbContext.Flights.AddRange(dbNewFlights.Select(f => f.Value)); await dbContext.SaveChangesAsync(); return(dbContext.Flights.Take(10)); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { var UserFromDb = await _context.Users.FindAsync(FlightUser.Id); UserFromDb.Id = FlightUser.Id; UserFromDb.Name = FlightUser.Name; UserFromDb.Surname = FlightUser.Surname; UserFromDb.PersonalId = FlightUser.PersonalId; UserFromDb.Address = FlightUser.Address; UserFromDb.PhoneNumber = FlightUser.PhoneNumber; UserFromDb.UserName = FlightUser.Username; UserFromDb.Email = FlightUser.Email; await _context.SaveChangesAsync(); return(Redirect("/Identity/UserList")); } return(RedirectToPage()); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { var FlightFromDb = await context.Flights.FindAsync(Flight.Id); FlightFromDb.CapacityBuisness = Flight.CapacityBuisness; FlightFromDb.CapacityNormal = Flight.CapacityNormal; FlightFromDb.FlightLanding = Flight.FlightLanding; FlightFromDb.FlightTakeOff = Flight.FlightTakeOff; FlightFromDb.LocationFrom = Flight.LocationFrom; FlightFromDb.LocationTo = Flight.LocationTo; FlightFromDb.PilotName = Flight.PilotName; FlightFromDb.PlaneId = Flight.PlaneId; FlightFromDb.PlaneModel = Flight.PlaneModel; await context.SaveChangesAsync(); return(Redirect("/Identity/FlightList")); } return(RedirectToPage()); }
public async Task <bool> SaveChangesAsync() { return(await _context.SaveChangesAsync() > 0); }