public async Task <ActionResult <Booking> > PostBooking(Booking booking) { var hotel = await HotelDB.Hotels.FindAsync(booking.hotel); // Get hotel for booking if (hotel == null) { return(Ok()); // Respond 200 } var bookings = await BookingDB.Bookings.Where(b => b.hotel == booking.hotel).ToListAsync(); // Get all bookings at the hotel that the booking is for if (BookingSupport.createBooking(booking, hotel, bookings) == null) // Attempt to create booking. Checks validity of booking behind the scenes. { return(Ok()); // Respond 200 } else { BookingDB.Bookings.Add(booking); // Add booking to database await BookingDB.SaveChangesAsync(); // Update database } // Return a CreatedAtActionResult (201) response, that indicates a new record has been made serverside, to the client. // The response will also build the url refernce to this new object: // GetBooking function -> Get request to root of this API /api/BookingAPI/Bookings/ // + The id of the new record to access it = /api/BookingAPI/Bookings/{id} return(CreatedAtAction(nameof(GetBooking), new { id = booking.Id }, booking)); }
public async Task <ActionResult <Booking> > CheckHotelBooking(Booking booking) { var hotel = await HotelDB.Hotels.FindAsync(booking.hotel); // The hotel where the booking is var bookings = await BookingDB.Bookings.Where(b => b.hotel == booking.hotel).ToListAsync(); // All bookings at hotel where booking will be if (BookingSupport.checkAvailabilty(booking, hotel, bookings)) { return(booking); // Check if the booking is available and respond 200 with booking in body } else { return(Ok()); // respond 200 } }