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)); } }
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)); } }