Пример #1
0
        public HttpResponseMessage Get(int id)
        {
            try
            {
                var result = AcmeRepo.GetFlight(id);

                if (result == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, AcmeModelFactory.Create(result)));
            }
            catch (Exception ex)
            {
                _logger.LogMessage("Exception: " + ex.Message);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
Пример #2
0
        public HttpResponseMessage Post([FromBody] BookingModel bm)
        {
            try
            {
                Booking booking = AcmeEntityFactory.Create(bm);

                //validate the flight number.
                if (AcmeRepo.GetFlight(booking.FlightNo) == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid flight number in booking."));
                }

                //make sure the passenger is not already booked for this flight on this date.
                if (AcmeRepo.GetBookings(booking.PassengerName, booking.Date, "", "", booking.FlightNo).FirstOrDefault() != null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Booking already exists for this passenger for flight {0} on date {1}", booking.FlightNo, booking.Date)));
                }
                //check available seats
                if (AcmeRepo.GetAvailableFlights(booking.Date, booking.Date, 1).FirstOrDefault(f => f.FlightNo == booking.FlightNo) == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("No available seats for flight {0} on date {1}", booking.FlightNo, booking.Date)));
                }

                if (AcmeRepo.CreateBooking(booking) && AcmeRepo.Save())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, AcmeModelFactory.Create(booking)));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Failed to save booking."));
                }
            }
            catch (Exception ex)
            {
                _logger.LogMessage("Exception: " + ex.Message);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
Пример #3
0
 public IEnumerable <BookingModel> Get(string passengerName = "", DateTime?date = null, string arrivalCity = "", string departerCity = "", int flightNumber = -1)
 {
     return(AcmeRepo.GetBookings(passengerName, date, arrivalCity, departerCity, flightNumber).ToList().Select(b => AcmeModelFactory.Create(b)));
 }
Пример #4
0
 public IEnumerable <AvailableFlightModel> Get(DateTime startDate, DateTime endDate, int availSeats)
 {
     return(AcmeRepo.GetAvailableFlights(startDate, endDate, availSeats).ToList().Select(f => AcmeModelFactory.Create(f)));
 }
Пример #5
0
 public IEnumerable <FlightModel> Get()
 {
     return(AcmeRepo.GetAllFlights().ToList().Select(f => AcmeModelFactory.Create(f)));
 }