public static bool FlightIsDeleted(int id) { using (var context = new FlightPlannerDBContext()) { var flight = context.Flights.Include(c => c.From).SingleOrDefault(f => f.Id == id); if (flight != null) { context.Flights.Remove(flight); context.SaveChanges(); return(true); } return(false); } }
public static HttpResponseMessage AddFlight(HttpRequestMessage request, Flight flight) { lock (thisLock) { if (DateIsInvalid(flight)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } if (FromAirportSameTo(flight)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } using (var context = new FlightPlannerDBContext()) { if (IsSameFlight(context, flight)) { return(new HttpResponseMessage(HttpStatusCode.Conflict)); } var flights = context.Flights.Add(flight); context.SaveChanges(); var fakeFlight = new TestFlight(); fakeFlight.Carrier = flights.Carrier; fakeFlight.From.AirportName = flights.From.AirportName; fakeFlight.From.City = flights.From.City; fakeFlight.From.Country = flights.From.Country; fakeFlight.To.AirportName = flights.To.AirportName; fakeFlight.To.City = flights.To.City; fakeFlight.To.Country = flights.To.Country; fakeFlight.ArrivalTime = flights.ArrivalTime; fakeFlight.DepartureTime = flights.DepartureTime; fakeFlight.Id = flights.Id; return(request.CreateResponse(HttpStatusCode.Created, fakeFlight)); } } }