Esempio n. 1
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));
            }
        }
Esempio n. 2
0
 public IEnumerable <AvailableFlightModel> Get(DateTime startDate, DateTime endDate, int availSeats)
 {
     return(AcmeRepo.GetAvailableFlights(startDate, endDate, availSeats).ToList().Select(f => AcmeModelFactory.Create(f)));
 }