Exemple #1
0
 public void AddUser(UsersInMatch entity, int id)
 {
     _context.Matches.First(t => t.MatchId == id)
     .UsersInMatches
     .Add(entity);
     _context.UsersInMatches.Update(entity);
     _context.SaveChanges();
 }
        public async Task <ActionResult <MatchModel> > PostSolo(int userid1, int userid2, int tournamentid, int round)
        {
            try
            {
                var tournament = await _repository.GetTournamentById(tournamentid);

                if (tournament == null)
                {
                    return(NotFound("Tournament not found!"));
                }

                var homeUser = await _repository.GetUserById(userid1);

                var awayUser = await _repository.GetUserById(userid2);

                if (homeUser == null || awayUser == null)
                {
                    return(NotFound("User not foun!"));
                }
                var match = new Match()
                {
                    Tournament = _mapper.Map <Tournament>(tournament),
                    Home       = _mapper.Map <User>(homeUser),
                    Away       = _mapper.Map <User>(awayUser),
                    Round      = round
                };
                _repository.Add(match);

                if (await _repository.SaveChangesAsync())
                {
                    var uimEntity = new UsersInMatch()
                    {
                        Home  = true,
                        User  = homeUser,
                        Match = match
                    };
                    var uimEntity2 = new UsersInMatch()
                    {
                        Home  = false,
                        User  = awayUser,
                        Match = match
                    };
                    _repository.AddUser(uimEntity, match.MatchId);
                    _repository.AddUser(uimEntity2, match.MatchId);
                    var location = _linkGenerator.GetPathByAction(HttpContext,
                                                                  "GetSoloMatch",
                                                                  values: new { id = match.MatchId });
                    return(Created(location, _mapper.Map <MatchModel>(match)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure!"));
            }
            return(BadRequest());
        }