コード例 #1
0
ファイル: BookingService.cs プロジェクト: fdres/HotelInfo.Api
        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");
            }
        }
コード例 #2
0
ファイル: BookingService.cs プロジェクト: fdres/HotelInfo.Api
        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));
        }
コード例 #3
0
        public async Task <IActionResult> UpdateBooking(Guid hotelId, Guid id, [FromBody] BookingForAddOrUpdateDto bookingToUpdate)
        {
            await _bookingService.UpdateBookingAsync(hotelId, id, bookingToUpdate);

            return(NoContent());
        }
コード例 #4
0
        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));
        }