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 UpdateHotel_ExistingHotel_DataIsUpdated() { AddHotelCommand AddHotel = CommandFactory.createAddHotelCommand(_hotel); AddHotel.Execute(); var insertedHotelId = AddHotel.GetResult(); _insertedHotels.Add(insertedHotelId); _hotel.Name = "Upated Name"; _hotel.PricePerRoom = 999; _hotel.Phone = "+58 4241364429"; _hotel.Website = "http://updatedhotel.com"; _hotel.AmountOfRooms = 99; _hotel.AddressSpecification = "New Address specification"; UpdateHotelCommand UpdateHotel = CommandFactory.UpdateHotelCommand(insertedHotelId, _hotel); UpdateHotel.Execute(); var updatedHotel = UpdateHotel.GetResult(); Assert.AreEqual(_hotel.Name, updatedHotel.Name); Assert.AreEqual(_hotel.PricePerRoom, updatedHotel.PricePerRoom); Assert.AreEqual(_hotel.Phone, updatedHotel.Phone); Assert.AreEqual(_hotel.Website, updatedHotel.Website); Assert.AreEqual(_hotel.AmountOfRooms, updatedHotel.AmountOfRooms); Assert.AreEqual(_hotel.AddressSpecification, updatedHotel.AddressSpecification); }
public void Handle(UpdateHotelCommand Message) { if (Message != null) { var hotel = _mapper.Map <Hotel>(Message); _repository.Update(hotel); } }
public ActionResult Put(int id, [FromBody] RegisterHotelVM model) { var handler = new HotelCommandHandler(_repository); var command = new UpdateHotelCommand( id, model.Name, model.Description, model.Rating, model.Street, model.Number, model.ZipCode, model.State, model.City, model.FeatureDescriptions ); handler.Handle(command); if (!handler.Valid) { List <string> errorMessages = new List <string>(); foreach (var erro in handler.Notifications) { errorMessages.Add(erro.Message); } return(BadRequest(new { success = false, errors = errorMessages })); } return(Ok(new { success = true, message = "Atualização do hotel feito com sucesso" })); }
public ICommandResult Handle(UpdateHotelCommand command) { //Fazer as validações em reposiório if (!_repository.IsHotelExists(command.Id)) { AddNotification("Hotel.Id", "O id informado não consta na base de dados"); } //Gerar os objetos de valor var address = new Address(command.Street, command.Number, command.ZipCode, command.State, command.City); //Gerar as entidade var Hotel = _repository.GetById(command.Id); //Update da entidade Hotel.UpdateHotel( command.Name, command.Description, command.Rating, address, command.FeatureDescriptions ); //Aplicar as validações AddNotifications(Hotel); //verificar as validações if (Invalid) { return(new CommandResult(false, "Não foi possivel atualizar o hotel!")); } //Salvar _repository.Update(Hotel); //Devolver o retorno return(new CommandResult(true, "Hotel atualizado com sucesso!")); }
public async Task <IActionResult> Put(UpdateHotelCommand hotel) { await mediator.Send(hotel); return(CreatedAtAction("Get", new { id = hotel.Id }, hotel)); }