public async Task UpdateMatch_WhenCalled_ReturnNotFound() { const int id = 1; const int updateRound = 2; var matchUpdateDto = new MatchUpdateDto { Round = updateRound }; _matchService.Setup(m => m.GetDetailByIdAsync(id)).ReturnsAsync((Match)null); var result = await _matchesController.UpdateMatch(id, matchUpdateDto); _matchService.Verify(m => m.GetDetailByIdAsync(id), Times.Once); _matchService.Verify(m => m.UpdateAsync(It.IsAny <Match>()), Times.Never); Assert.That(result, Is.TypeOf <NotFoundResult>()); }
public async Task <IActionResult> ReschuduleMatchDate(int id, [FromBody] MatchUpdateDto model) { var match = await _matchRepository.GetByIdAsync(id); if (match == null) { return(BadRequest($"Invalid match id : { id }")); } var reschuduleResult = match.ReschuduleMachDate(model.MatchDate); if (reschuduleResult.IsFailure) { return(BadRequest(reschuduleResult.Error)); } await _matchRepository.UpdateAsync(match); await _unitOfWork.Commit(); return(NoContent()); }
public async Task <IActionResult> UpdateMatch(int id, [FromBody] MatchUpdateDto matchUpdateDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var match = await _matchService.GetDetailByIdAsync(id); if (match == null) { return(NotFound()); } _mapper.Map(matchUpdateDto, match); await _matchService.UpdateAsync(match); var updatedMatch = await _matchService.GetDetailByIdAsync(id); var returnMatch = _mapper.Map <MatchDetailDto>(updatedMatch); return(Ok(returnMatch)); }
public async Task UpdateaMatch_WhenCalled_UpdateExistingMatch() { const int id = 1; const int updateRound = 2; var matchUpdateDto = new MatchUpdateDto { Round = updateRound }; var match = new Match { Id = id, Round = 1 }; var expectedMatch = new Match { Id = id, Round = updateRound }; _matchService.SetupSequence(m => m.GetDetailByIdAsync(id)) .ReturnsAsync(match) .ReturnsAsync(expectedMatch); var result = await _matchesController.UpdateMatch(id, matchUpdateDto); var okObjectResult = result as OkObjectResult; var okObjectResultValue = okObjectResult.Value as MatchDetailDto; _matchService.Verify(m => m.GetDetailByIdAsync(id), Times.Exactly(2)); _matchService.Verify(m => m.UpdateAsync(It.IsAny <Match>()), Times.Once); Assert.That(result, Is.TypeOf <OkObjectResult>()); Assert.That(okObjectResultValue.Round, Is.EqualTo(updateRound)); }
public async Task <IActionResult> PutMatch(Guid id, MatchUpdateDto matchUpdateDto) { if (id != matchUpdateDto.Id) { return(BadRequest()); } if (matchUpdateDto.TeamOneScore == null) { ModelState.AddModelError("TeamOneScore", "Score is required."); } if (matchUpdateDto.TeamOneScore < 0) { ModelState.AddModelError("TeamOneScore", "Score cannot be negative."); } if (matchUpdateDto.TeamTwoScore == null) { ModelState.AddModelError("TeamTwoScore", "Score is required."); } if (matchUpdateDto.TeamTwoScore < 0) { ModelState.AddModelError("TeamTwoScore", "Score cannot be negative."); } if (matchUpdateDto.PlayerOneId == Guid.Empty) { ModelState.AddModelError("PlayerOneId", "Id cannot be empty"); } if (matchUpdateDto.PlayerTwoId == Guid.Empty) { ModelState.AddModelError("PlayerTwoId", "Id cannot be empty"); } if (matchUpdateDto.PlayerThreeId == Guid.Empty) { ModelState.AddModelError("PlayerThreeId", "Id cannot be empty"); } if (matchUpdateDto.PlayerFourId == Guid.Empty) { ModelState.AddModelError("PlayerFourId", "Id cannot be empty"); } var list = new List <Guid>(); list.Add(matchUpdateDto.PlayerOneId); list.Add(matchUpdateDto.PlayerTwoId); list.Add(matchUpdateDto.PlayerThreeId); list.Add(matchUpdateDto.PlayerFourId); if (list.Count != list.Distinct().Count()) { ModelState.AddModelError("", "Duplicate players are not allowed."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Match match = await _context.Matches.FindAsync(id); if (match == null) { return(NotFound()); } match.TeamOneScore = (int)matchUpdateDto.TeamOneScore; match.TeamTwoScore = (int)matchUpdateDto.TeamTwoScore; match.PlayerOneId = matchUpdateDto.PlayerOneId; match.PlayerTwoId = matchUpdateDto.PlayerTwoId; match.PlayerThreeId = matchUpdateDto.PlayerThreeId; match.PlayerFourId = matchUpdateDto.PlayerFourId; _context.Matches.Update(match); await _context.SaveChangesAsync(); await UpdateAllRatings(); return(NoContent()); }