public GameResponse Execute(UpdateGameTitleInput input) { var gameResponse = new GameResponse(); try { CheckInputValidity(input); Log.Information("Updating GameId: [{Id}] to new title [{NewTitle}]...", input.Id, input.NewTitle); var gameToUpdate = Repository.SingleOrDefault(game => game.Id == input.Id); if (gameToUpdate == null) { var exception = new Exception($"Failed to find game for id: [{input.Id}]."); Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message); HandleErrors(gameResponse, exception, 404); return(gameResponse); } gameToUpdate.Name = input.NewTitle; var updatedGame = Repository.Update(gameToUpdate); gameResponse.Game = updatedGame; gameResponse.StatusCode = 200; Log.Information("Successful updated GameId: [{Id}] to title [{NewTitle}].", input.Id, input.NewTitle); } catch (Exception exception) { Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message); HandleErrors(gameResponse, exception); } return(gameResponse); }
public void InvalidInputNameIsEmpty(string name) { var invalidInput = new UpdateGameTitleInput { Id = 666, NewTitle = name }; var response = request.Execute(invalidInput); response.Should().NotBeNull(); response.ErrorResponse.ErrorSummary.Should().NotBeNullOrWhiteSpace(); response.ErrorResponse.ErrorSummary.Should().BeEquivalentTo($"Input {nameof(invalidInput.NewTitle)} cannot be empty."); }
public void InvalidInputIdNotValid(int Id) { var invalidInput = new UpdateGameTitleInput { Id = Id, NewTitle = "Something" }; var response = request.Execute(invalidInput); response.Should().NotBeNull(); response.ErrorResponse.Should().NotBeNull(); response.ErrorResponse.ErrorSummary.Should().NotBeNullOrWhiteSpace(); response.ErrorResponse.ErrorSummary.Should().BeEquivalentTo($"Input {nameof(Id)} must be 1 or greater."); }
private void CheckInputValidity(UpdateGameTitleInput input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (input.Id <= 0) { throw new ArgumentException($"Input {nameof(input.Id)} must be 1 or greater."); } if (string.IsNullOrWhiteSpace(input.NewTitle)) { throw new ArgumentException($"Input {nameof(input.NewTitle)} cannot be empty."); } }
public async Task <GameResponse> ExecuteAsync(UpdateGameTitleInput input) { Log.Information("Updating title for game with id: [{Id}]...", input.Id); var gameResponse = new GameResponse(); try { if (string.IsNullOrWhiteSpace(input.NewTitle)) { var exception = new ArgumentException($"[{nameof(input)}] property [{nameof(input.NewTitle)}] cannot be empty."); HandleErrors(gameResponse, exception); return(gameResponse); } var gameToUpdate = await Repository.SingleOrDefaultAsync(g => g.Id == input.Id); if (gameToUpdate == null) { var exception = new Exception($"Update failed: No Game found for Id: [{input.Id}]."); Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message); HandleErrors(gameResponse, exception, 404); } else { gameToUpdate.Name = input.NewTitle; gameResponse.Game = await Repository.UpdateAsync(gameToUpdate); gameResponse.StatusCode = 200; } } catch (Exception exception) { Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message); HandleErrors(gameResponse, exception); } return(gameResponse); }
public IActionResult Put(UpdateGameTitleInput input) { return(ExecuteAndHandleRequest(() => GameRequestAggregate.UpdateGameTitleRequest.Execute(input))); }
public async Task <IActionResult> Put(UpdateGameTitleInput input) { return(await ExecuteAndHandleRequestAsync(() => GameRequestAggregateAsync.UpdateGameTitleRequestAsync.ExecuteAsync(input))); }