public ActionResult <HotelDTO> Update([FromRoute] int hotelId, [FromBody] HotelDTO hotelDTO) { try { GetHotelByIdCommand commandId = CommandFactory.GetHotelByIdCommand(hotelId); commandId.Execute(); HotelMapper hotelMapper = MapperFactory.createHotelMapper(); Entity entity = hotelMapper.CreateEntity(hotelDTO); UpdateHotelCommand command = CommandFactory.UpdateHotelCommand(hotelId, (Hotel)entity); command.Execute(); var result = command.GetResult(); _logger?.LogInformation($"Obtenido el hotel exitosamente, despues de actualizar con el Id = {hotelId}"); DTO lDTO = hotelMapper.CreateDTO(result); return(Ok(lDTO)); } catch (HotelNotFoundException ex) { _logger?.LogWarning("Hotel con id = {hotelId} no conseguido al intentar actualizar"); return(new NotFoundObjectResult(new ErrorMessage($"Hotel con id {hotelId} no conseguido"))); } catch (RequiredAttributeException e) { _logger?.LogWarning($"El atributo requerido no fue recibido al actualizar el Hotel: {e.Message}"); return(new BadRequestObjectResult(new ErrorMessage(e.Message))); } catch (InvalidAttributeException e) { _logger?.LogWarning($"El valor del atributo es invalido al actualizar el Hotel: {e.Message}"); return(new BadRequestObjectResult(new ErrorMessage(e.Message))); } }
public ActionResult <HotelDTO> Create([FromBody] HotelDTO hotelDTO) { try { HotelMapper hotelMapper = MapperFactory.createHotelMapper(); Entity entity = hotelMapper.CreateEntity(hotelDTO); AddHotelCommand command = CommandFactory.createAddHotelCommand((Hotel)entity); command.Execute(); int idFromData = command.GetResult(); _logger?.LogInformation($"Obtenido el id = {idFromData} el Hotel exitosamente al agregar"); GetHotelByIdCommand commandId = CommandFactory.GetHotelByIdCommand(idFromData); commandId.Execute(); var result = commandId.GetResult(); _logger?.LogInformation($"Obtenido el hotel exitosamente por Id = {idFromData}"); DTO lDTO = hotelMapper.CreateDTO(result); return(CreatedAtAction("Get", "hotels", lDTO)); } catch (RequiredAttributeException e) { _logger?.LogWarning($"Atributo requerido no recibido al agregar Hotel: {e.Message}"); return(new BadRequestObjectResult(new ErrorMessage(e.Message))); } catch (InvalidAttributeException e) { _logger?.LogWarning($"Valor del atributo es invalido al agregar Hotel: {e.Message}"); return(new BadRequestObjectResult(new ErrorMessage(e.Message))); } }
public void CreateEntityTest() { HotelMapper _HotelMapper = MapperFactory.createHotelMapper(); HotelDTO HotelDTO = DTOFactory.CreateHotelDTO(_hotel.Id, _hotel.Name, _hotel.AmountOfRooms, _hotel.RoomCapacity, _hotel.IsActive, _hotel.AddressSpecification, _hotel.PricePerRoom, _hotel.Website, _hotel.Phone, _hotel.Picture, _hotel.Stars, _hotel.Location.Id); var result = _HotelMapper.CreateEntity(HotelDTO); Assert.IsInstanceOf <Hotel>(result); }