public async Task <IActionResult> UpdateMatch(string matchID, [FromBody] MatchToCreateDto matchToUpdate) { try { bool valid = int.TryParse(matchID, out int id); if (!ModelState.IsValid || !valid) { return(BadRequest(ModelState)); } Match newMatch = new Match() { ID = id, Description = matchToUpdate.TeamA + "-" + matchToUpdate.TeamB, MatchDate = Convert.ToDateTime(matchToUpdate.MatchDate), MatchTime = TimeSpan.Parse(matchToUpdate.MatchTime), TeamA = matchToUpdate.TeamA, TeamB = matchToUpdate.TeamB, Sport = (Sport)matchToUpdate.Sport }; newMatch = await _db.Matches.UpdateMatch(newMatch); return(Created( $"api/matches/UpdateMatch/{newMatch.ID}", _mapper.Map <MatchToReturnDto>(newMatch))); } catch (Exception) { // log exception return(StatusCode(500)); } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { try { MatchToCreateDto match = (MatchToCreateDto)validationContext.ObjectInstance; bool valid = DateTime.TryParse(match.MatchDate, out DateTime matchDate); if (!valid) { return(new ValidationResult("Unreadable match date")); } valid = TimeSpan.TryParse(match.MatchTime, out TimeSpan matchTime); if (!valid) { return(new ValidationResult("Unreadable match time")); } if (!Enum.IsDefined(typeof(Sport), match.Sport)) { return(new ValidationResult("Nonexistent sport")); } if (String.IsNullOrEmpty(match.TeamA)) { return(new ValidationResult("Please add a TeamA")); } if (String.IsNullOrEmpty(match.TeamB)) { return(new ValidationResult("Please add a TeamB")); } return(ValidationResult.Success); } catch (Exception) { // log exception return(new ValidationResult("Model Validation Error")); } }