Exemple #1
0
 public void AddTeam(TeamsInMatch entity, int id)
 {
     _context.Matches.First(t => t.MatchId == id)
     .TeamsInMatches
     .Add(entity);
     _context.TeamsInMatches.Update(entity);
     _context.SaveChanges();
 }
        public async Task <ActionResult <MatchModel> > PostDuo(int teamid1, int teamid2, int tournamentid)
        {
            try
            {
                var tournament = await _repository.GetTournamentById(tournamentid);

                if (tournament == null)
                {
                    return(NotFound("Tournament not found!"));
                }
                var homeTeam = await _repository.GetTeamById(teamid1);

                var awayTeam = await _repository.GetTeamById(teamid2);

                if (homeTeam == null || awayTeam == null)
                {
                    return(NotFound("Team not found!"));
                }
                var match = new Match()
                {
                    Tournament = _mapper.Map <Tournament>(tournament),
                    HomeTeam   = homeTeam.Name,
                    AwayTeam   = homeTeam.Name
                };
                _repository.Add(match);
                if (await _repository.SaveChangesAsync())
                {
                    var uimEntity = new TeamsInMatch()
                    {
                        Home  = true,
                        Team  = homeTeam,
                        Match = match
                    };
                    var uimEntity2 = new TeamsInMatch()
                    {
                        Home  = false,
                        Team  = awayTeam,
                        Match = match
                    };
                    _repository.AddTeam(uimEntity, match.MatchId);
                    _repository.AddTeam(uimEntity2, match.MatchId);
                    var location = _linkGenerator.GetPathByAction(HttpContext,
                                                                  "GetDuoMatch",
                                                                  values: new { id = match.MatchId });
                    return(Created(location, _mapper.Map <MatchModel>(match)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure!"));
            }
            return(BadRequest());
        }