コード例 #1
0
        public IActionResult CreateBooking([FromBody]long customerId, [FromBody] Booking booking)
        {
            try
            {
                Customer customer = _customerRepository.GetById(customerId);
                if (customer == null)
                {
                    return BadRequest("Invalid customer customerId: " + customerId);
                }

                if (!_bookingsService.CheckBookingDates(booking))
                {
                    return BadRequest("Invalid booking dates");
                }

                if (!_bookingsService.CheckRoomsAvailability(booking))
                {
                    return BadRequest("Unavailable rooms");
                }

                booking.ExpirationDate = BookingExpirationType.Short;
                booking.Price = _bookingsService.CalculatePrice(booking);
                if (!_customerService.AddBooking(customer, booking))
                {
                    return BadRequest("Unable to add new booking");
                }

                _customerRepository.SaveChanges();

                return Ok();
            }
            catch (Exception e)
            {
                return StatusCode(500, new { error = e.Message });
            }
        }