public ActionResult Delete(int id) { var handler = new HotelCommandHandler(_repository); var command = new DeleteHotelCommand( id ); 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 = "Exclusão de hotel feito com sucesso" })); }
public ICommandResult Handle(DeleteHotelCommand 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 as entidade var Hotel = _repository.GetById(command.Id); //Soft Delete da entidade Hotel.DeleteHotel(); //Aplicar as validações AddNotifications(Hotel); //verificar as validações if (Invalid) { return(new CommandResult(false, "Não foi possivel excluir o hotel!")); } //Salvar _repository.Delete(Hotel); //Devolver o retorno return(new CommandResult(true, "Hotel excluir com sucesso!")); }
public void DeleteHotel_InvalidHotelId_NoExceptionThrown() { Assert.DoesNotThrow(() => { DeleteHotelCommand DeleteHotel = CommandFactory.DeleteHotelCommand(12093); DeleteHotel.Execute(); }); }
public void TearDown() { foreach (var hotelId in _insertedHotels) { DeleteHotelCommand DeleteHotel = CommandFactory.DeleteHotelCommand(hotelId); DeleteHotel.Execute(); } _insertedHotels.Clear(); }
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 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(); }); }
public async Task <IActionResult> DeleteAsync(int id) { if (id == default) { return(NotFound()); } var deleteCommand = new DeleteHotelCommand { Id = id }; var result = await _mediator.Send(deleteCommand); if (result.Success) { return(RedirectToAction(nameof(GetAllAsync))); } ModelState.AddModelError(string.Empty, result.Error); return(BadRequest(ModelState)); }