public async Task UpdateBookingAsync(Guid hotelId, Guid id, BookingForAddOrUpdateDto bookingToUpdate) { var bookingEntity = await _hotelInfoRepository .GetFirstAsync <Booking>(b => b.HotelId == hotelId && b.Id == id); if (bookingEntity == null) { throw new NotFoundException($"Could not find booking with hotelId '{hotelId.ToString()}' and id '{id.ToString()}'"); } _mapper.Map(bookingToUpdate, bookingEntity); _hotelInfoRepository.Update(bookingEntity); if (!await _hotelInfoRepository.SaveChangesAsync()) { throw new Exception($"Trying to update booking with id '{id.ToString()}' failed"); } }
public async Task <BookingDto> AddBookingAsync(Guid hotelId, BookingForAddOrUpdateDto bookingToAdd) { var bookingEntity = _mapper.Map <Booking>(bookingToAdd); bookingEntity.HotelId = hotelId; _hotelInfoRepository.Add(bookingEntity); if (!await _hotelInfoRepository.SaveChangesAsync()) { throw new Exception("Trying to add booking failed"); } var createdId = bookingEntity.Id; bookingEntity = await _hotelInfoRepository .GetFirstAsync <Booking, Hotel>(b => b.HotelId == hotelId && b.Id == createdId, b => b.Hotel); if (bookingEntity == null) { throw new NotFoundException($"Could not find booking with hotelId '{hotelId.ToString()}' and id '{createdId.ToString()}'"); } return(_mapper.Map <BookingDto>(bookingEntity)); }
public async Task <IActionResult> UpdateBooking(Guid hotelId, Guid id, [FromBody] BookingForAddOrUpdateDto bookingToUpdate) { await _bookingService.UpdateBookingAsync(hotelId, id, bookingToUpdate); return(NoContent()); }
public async Task <IActionResult> AddBooking(Guid hotelId, BookingForAddOrUpdateDto bookingToAdd) { var booking = await _bookingService.AddBookingAsync(hotelId, bookingToAdd); return(CreatedAtAction(nameof(GetBookingById), new { hotelId = booking.HotelId, id = booking.Id }, booking)); }