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 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 void GetHotelById_InvalidHotelId_ExceptionThrown() { Assert.Throws <HotelNotFoundException>(() => { GetHotelByIdCommand GetHotelById = CommandFactory.GetHotelByIdCommand(10928); GetHotelById.Execute(); }); }
public void AddHotel_ValidHotel_NoExceptionThrown() { Assert.DoesNotThrow(() => { AddHotelCommand AddHotel = CommandFactory.createAddHotelCommand(_hotel); AddHotel.Execute(); var addedHotelId = AddHotel.GetResult(); GetHotelByIdCommand GetHotelById = CommandFactory.GetHotelByIdCommand(addedHotelId); GetHotelById.Execute(); _insertedHotels.Add(addedHotelId); }); }
public void Delete_ValidHotelId_OkResultReturned() { AddHotelCommand AddHotel = CommandFactory.createAddHotelCommand(_hotelentity); AddHotel.Execute(); var savedHotelId = AddHotel.GetResult(); var deleteResult = _hotelsController.Delete(savedHotelId); Assert.IsInstanceOf <OkResult>(deleteResult); GetHotelByIdCommand GetHotelById = CommandFactory.GetHotelByIdCommand(savedHotelId); Assert.Throws <HotelNotFoundException>(() => GetHotelById.Execute()); }
public void GetHotelById_ValidHotelId_CorrectDataReturned() { AddHotelCommand AddHotel = CommandFactory.createAddHotelCommand(_hotel); AddHotel.Execute(); var hotelId = AddHotel.GetResult(); _insertedHotels.Add(hotelId); GetHotelByIdCommand GetHotelById = CommandFactory.GetHotelByIdCommand(hotelId); GetHotelById.Execute(); var getHotel = GetHotelById.GetResult(); Assert.AreEqual(hotelId, getHotel.Id); }
public ActionResult Delete([FromRoute] int id) { try { //validar que el hotel exista GetHotelByIdCommand commandId = CommandFactory.GetHotelByIdCommand(id); commandId.Execute(); DeleteHotelCommand command = CommandFactory.DeleteHotelCommand(id); command.Execute(); return(Ok()); } catch (HotelNotFoundException ex) { _logger?.LogWarning($"Hotel con id = {id} no conseguido al intentar eliminar"); return(new NotFoundObjectResult(new ErrorMessage($"Hotel con id = {id} no conseguido"))); } }
public ActionResult <HotelDTO> GetById([FromRoute] int hotelId) { try { HotelMapper hotelMapper = MapperFactory.createHotelMapper(); GetHotelByIdCommand commandId = CommandFactory.GetHotelByIdCommand(hotelId); commandId.Execute(); var result = commandId.GetResult(); _logger?.LogInformation($"Obtenido el hotel exitosamente por Id = {hotelId}"); DTO lDTO = hotelMapper.CreateDTO(result); return(Ok(lDTO)); } catch (HotelNotFoundException ex) { _logger?.LogWarning($"Hotel con Id = {hotelId} no conseguido al consultar"); return(NotFound()); } }
public void DeleteHotel_ValidHotelId_HotelNotFound() { Assert.Throws <HotelNotFoundException>(() => { AddHotelCommand AddHotel = CommandFactory.createAddHotelCommand(_hotel); AddHotel.Execute(); var addedHotelId = AddHotel.GetResult(); GetHotelByIdCommand GetHotelById = CommandFactory.GetHotelByIdCommand(addedHotelId); GetHotelById.Execute(); DeleteHotelCommand DeleteHotel = CommandFactory.DeleteHotelCommand(addedHotelId); DeleteHotel.Execute(); GetHotelById = CommandFactory.GetHotelByIdCommand(addedHotelId); GetHotelById.Execute(); }); }