public IActionResult AddChallenge(AddChallengeDTO dto) { if (dto == null || dto.Title == null || dto.Description == null) { return(BadRequest()); } Category category = _categoryRepository.GetById(dto.CategoryId); if (category == null) { return(BadRequest()); } Challenge challenge = Model.Challenge.MapAddChallengeDTOToChallenge(dto); challenge.Category = category; //Already exists -> return a 303 See Other StatusCode if (_challengeRepo.ChallengeExists(challenge)) { return(StatusCode(303)); } try { _challengeRepo.AddChallenge(challenge); _challengeRepo.SaveChanges(); return(Ok()); } catch (Exception) { return(StatusCode(500)); } }
public async Task AddChallengeAsync(Challenge challenge) { try { challengeRepository.AddChallenge(challenge); await context.SaveChangesAsync(); } catch (DbUpdateException) { } }
public IActionResult Post([FromBody] Models.ChallengeSubmitModel challenge) { //query for team1 Team team1 = _TeamRepository.GetByTeamName(challenge.team1); Team team2 = _TeamRepository.GetByTeamName(challenge.team2); if (team1 == null) { return(BadRequest(new { message = "Improper input: first team does not exist." })); } if (team2 == null) { return(BadRequest(new { message = "Improper input: second team does not exist." })); } Challenge newChallenge = new Challenge(team1, team2, challenge.gamemode); bool success = _ChallengeRepository.AddChallenge(newChallenge); if (!success) { return(StatusCode(500)); } List <Challenge> challenges = _ChallengeRepository.GetUnresolvedTeamChallenges(challenge.team1); Challenge createdChallenge = null; foreach (var cha in challenges) { if (cha.Team2.teamname == challenge.team2 && cha.GameModeId == challenge.gamemode) { createdChallenge = cha; } } if (createdChallenge == null) { return(StatusCode(500)); } foreach (var user in createdChallenge.Team1.Userlist) { user.password = null; } foreach (var user in createdChallenge.Team2.Userlist) { user.password = null; } return(Created("api/challenge/add", createdChallenge)); }