Exemplo n.º 1
0
        public static HttpResponseMessage FindFlightById(HttpRequestMessage request, int id)
        {
            var flightById = new TestFlight();

            using (var context = new FlightPlannerDBContext())
            {
                var idExost = context.Flights.Any(f => f.Id == id);
                if (idExost)
                {
                    var flight = context.Flights.Include(a => a.From).Include(a => a.To).SingleOrDefault(f => f.Id == id);
                    flightById.From.AirportName = flight.From.AirportName;
                    flightById.From.City        = flight.From.City;
                    flightById.From.Country     = flight.From.Country;
                    flightById.To.AirportName   = flight.To.AirportName;
                    flightById.To.City          = flight.To.City;
                    flightById.To.Country       = flight.To.Country;
                    flightById.Carrier          = flight.Carrier;
                    flightById.DepartureTime    = flight.DepartureTime;
                    flightById.ArrivalTime      = flight.ArrivalTime;
                    flightById.Id = flight.Id;

                    return(request.CreateResponse(HttpStatusCode.OK, flightById));
                }
                return(request.CreateResponse(HttpStatusCode.NotFound));
            }
        }
Exemplo n.º 2
0
        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));
                }
            }
        }