Exemplo n.º 1
0
        public async Task <IActionResult> UpdateBooking(int userId, int id, BookingForUpdateDto bookingForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var bookingFromRepo = await _repo.GetBooking(id);

            var whenValue = bookingFromRepo.When;

            if (bookingFromRepo == null)
            {
                return(BadRequest());
            }

            if (bookingFromRepo.UserId != userId)
            {
                return(Unauthorized());
            }

            _mapper.Map(bookingForUpdateDto, bookingFromRepo);
            // *IMPORTANT* Be careful here. Make sure no await, no task.
            // Just classes or else mapping exception eventhough
            // you have already did automapper mapping

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating booking {id} failed on save");
        }
        public async Task <IActionResult> UpdateBooking(int id, BookingForUpdateDto bookingForUpdateDto)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var bookingFromRepo = await _repo.GetBooking(id);

            _mapper.Map(bookingForUpdateDto, bookingFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating booking {id} failed on save");
        }
        public async Task <IActionResult> UpdateBooking(int id, [FromBody] BookingForUpdateDto booking)
        {
            if (booking == null)
            {
                return(BadRequest("BookingForUpdateDto object is null"));
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            var bookingEntity = await _repository.Booking.GetBookingAsync(id, trackChanges : true);

            if (bookingEntity == null)
            {
                return(NotFound());
            }

            var room = await _repository.Room.GetRoomAsync(bookingEntity.RoomId, trackChanges : false);

            if (room == null)
            {
                return(BadRequest());
            }

            var reserved = CheckIfRoomIsReserved(room, booking.Start, booking.End, id);

            if (reserved)
            {
                return(Conflict("Room is already reserved for this time."));
            }

            _mapper.Map(booking, bookingEntity);
            await _repository.SaveAsync();

            return(NoContent());
        }