public async Task Update(int id, UpdateGameDTO game) { var updateCommand = _Mapper.Map <UpdateGameCommand>(game); updateCommand.Id = id; await _Bus.SendCommand(updateCommand); }
public async Task Put_GivenGame_WhenAuthorized_ThenAcceptResponse() { // Arrage var token = await GetToken(); _client.DefaultRequestHeaders.Clear(); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var responseGame = await _client.GetAsync("/gtracker/game/2"); var game = JsonConvert.DeserializeObject <GameDTO>(await responseGame.Content.ReadAsStringAsync()); UpdateGameDTO gameUpdated = new UpdateGameDTO { AcquisicionDate = game.AcquisicionDate, Kind = game.Kind, Name = game.Name, Observation = "Ajsutando observação de atualização desse game" }; var myContent = JsonConvert.SerializeObject(gameUpdated, Formatting.Indented); var buffer = Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await _client.PutAsync("/gtracker/game/2", byteContent); // Assert response.StatusCode.Should().Be(HttpStatusCode.Accepted); }
public async Task <ActionResult> UpdatePartial([FromRoute] int id, [FromBody] JsonPatchDocument <UpdateGameDTO> patchGameDTO) { Game game = await _gameRepository.FindByIdAsync(id); if (game.IsNull()) { return(NotFound()); } UpdateGameDTO gameDTO = _mapper.Map <UpdateGameDTO>(game); if (!TryValidateModel(gameDTO)) { return(ValidationProblem(ModelState)); } await ValidateGameModelForeignKeysOnPatch(patchGameDTO, gameDTO); if (!ModelState.IsValid) { return(ValidationProblem(ModelState)); } patchGameDTO.ApplyTo(gameDTO); _mapper.Map(gameDTO, game); await _gameRepository.UpdateAsync(game); return(NoContent()); }
public IActionResult Put(int id, [FromBody] UpdateGameDTO updateGameDTO) { var valid = _gameValidator.Validate(updateGameDTO); if (!valid.IsValid) { return(BadRequest()); } Game game = _gameRepository.GetGame(id); if (game == null) { return(NotFound()); } game.Name = updateGameDTO.name; game.Status = updateGameDTO.status; if (_gameRepository.SaveGame() > 0) { return(Ok()); } return(BadRequest()); }
public void Update(UpdateGameDTO model) { if (model != null) { var game = _mapper.Map <UpdateGameCommand>(model); _bus.SendCommand(game); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { IPlatformRepository platformRepository = (IPlatformRepository)context.HttpContext.RequestServices.GetService(typeof(IPlatformRepository)); UpdateGameDTO gameDTO = (UpdateGameDTO)context.ActionArguments["gameDTO"]; if (gameDTO.PlatformId == 0 || !await platformRepository.ExistsAsync(gameDTO.PlatformId)) { context.ModelState.AddModelError("PlatformId", "The given platform id does not correspond to an existing Platform"); } await next(); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] UpdateGameDTO game) { try { await _gameService.Update(id, game); return(Accepted()); } catch (Exception e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task <ActionResult> Update([FromRoute] int id, [FromBody] UpdateGameDTO gameDTO) { Game game = await _gameRepository.FindByIdAsync(id); if (game.IsNull()) { return(NotFound()); } _mapper.Map(gameDTO, game); await _gameRepository.UpdateAsync(game); return(NoContent()); }
public ActionResult Put([FromBody] UpdateGameDTO updateGameDTO) { try { _gameService.Update(updateGameDTO); return(Ok()); } catch (Exception e) { string errors = e.Message; return(ValidationProblem(new ValidationProblemDetails() { Type = "Model Validation Error", Detail = errors })); } }
public async Task Put_GivenGame_WhenInvalid_ThenBadRequestResponse(string acquisicionDate, int kind, string name, string observation, int qntErrors) { // Arrage var token = await GetToken(); _client.DefaultRequestHeaders.Clear(); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); DateTime result; DateTime?finalDate = null; if (DateTime.TryParse(acquisicionDate, out result)) { finalDate = result; } UpdateGameDTO gameUpdated = new UpdateGameDTO { AcquisicionDate = finalDate, Kind = kind, Name = name, Observation = observation }; var myContent = JsonConvert.SerializeObject(gameUpdated, Formatting.Indented); var buffer = Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await _client.PutAsync("/gtracker/game/2", byteContent); var notifications = JsonConvert.DeserializeObject <List <DomainNotification> >(await response.Content.ReadAsStringAsync()); // Assert response.StatusCode.Should().Be(HttpStatusCode.BadRequest); notifications.Should().HaveCount(qntErrors); }
public async Task Put_GivenGame_WhenNoToken_ThenAcceptResponse() { // Arrage UpdateGameDTO gameUpdated = new UpdateGameDTO { AcquisicionDate = DateTime.Now, Kind = 1, Name = "Tentativa de atualizar nome", Observation = "Ajsutando observação de atualização desse game" }; var myContent = JsonConvert.SerializeObject(gameUpdated, Formatting.Indented); var buffer = Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await _client.PutAsync("/gtracker/game/2", byteContent); // Assert response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); }
private async Task ValidateGameModelForeignKeysOnPatch(JsonPatchDocument <UpdateGameDTO> patchGameDTO, UpdateGameDTO gameDTO) { if (patchGameDTO.Operations.Any(op => op.path.ToLower() == "platformid") && !await _platformRepository.ExistsAsync(gameDTO.PlatformId)) { ModelState.AddModelError("PlatformId", "The given platform's id does not corresponds to an existing platform"); } }