Exemplo n.º 1
0
        public async Task <IActionResult> Put(int id, RoomForUpdateDto roomForUpdateDto)
        {
            if (!_repo.isRoomExist(id))
            {
                return(NotFound());
            }
            var roomFromRepo = await _repo.GetRoomById(id);

            _mapper.Map(roomForUpdateDto, roomFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"We have problem when we update room info{id} ");
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateRoom(int hotelId, RoomForUpdateDto roomForUpdateDto)
        {
            if (roomForUpdateDto == null)
            {
                return(BadRequest());
            }

            var roomEntity = _mapper.Map <Room>(roomForUpdateDto);

            roomEntity.HotelId = hotelId;

            _hotelrepo.Add(roomEntity);

            if (await _unitOfWork.CompleteAsync())
            {
                var roomToReturn = _mapper.Map <RoomForUpdateDto>(roomEntity);
                return(CreatedAtRoute("GetRoom", new { id = roomEntity.Id }, roomToReturn));
            }

            throw new Exception("Creating the room failed on save");
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateRoom(int id, RoomForUpdateDto roomForUpdateDto)
        {
            if (roomForUpdateDto == null)
            {
                return(BadRequest());
            }

            var roomFromRepo = await _hotelrepo.GetRoom(id);

            if (roomFromRepo == null)
            {
                return(NotFound($"Could not find room with an ID of {id}"));
            }

            _mapper.Map <RoomForUpdateDto, Room>(roomForUpdateDto, roomFromRepo);

            if (await _unitOfWork.CompleteAsync())
            {
                return(NoContent());
            }

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

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

            var roomEntity = await _repository.Room.GetRoomAsync(id, trackChanges : true);

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

            _mapper.Map(room, roomEntity);
            await _repository.SaveAsync();

            return(NoContent());
        }