public void JoinPlayer(JoinPlayerRequest request, int id)
        {
            var player = _context.Players
                         .Where(p => p.FirstName == request.firstName)
                         .Where(p => p.LastName == request.lastName)
                         .Where(p => p.DateOfBirth == request.birthDate)
                         .SingleOrDefault();

            //czy gracz istnieje
            if (player == null)
            {
                throw new PlayerDoesNotExistException("Gracz nie istnieje");
            }

            //czy gracz przypisany do drużyny
            var playerTeamExist = _context.PlayerTeams
                                  .Any(pt => pt.IdPlayer == player.IdPlayer &&
                                       pt.IdTeam == id);

            if (playerTeamExist)
            {
                throw new PlayerTeamAlreadyExistException("Gracz jest już przypisany do drużyny");
            }

            var team = _context.Teams.Where(t => t.IdTeam == id).SingleOrDefault();

            //czy spelnia kryterium wieku
            var playerAge = (DateTime.Now.Year - request.birthDate.Year);

            if (playerAge > team.MaxAge)
            {
                throw new PlayerTooOldException("Gracz zbyt zaawansowany wiekowo");
            }

            PlayerTeam pt = new PlayerTeam {
                // IdPlayer = player.IdPlayer,
                // IdTeam = id,
                NumOnShirt = request.numOnShirt,
                Comment    = request.comment,
                Player     = player,
                Team       = team
            };

            _context.PlayerTeams.Add(pt);
            _context.SaveChanges();
        }
Exemplo n.º 2
0
 public IActionResult JoinPlayer(JoinPlayerRequest request, int id)
 {
     try
     {
         _dbService.JoinPlayer(request, id);
         return(Ok("Player has joined to the team"));
     }
     catch (PlayerDoesNotExistException e)
     {
         return(NotFound(e.Message));
     }
     catch (PlayerTeamAlreadyExistException e) {
         return(BadRequest(e.Message));
     }
     catch (PlayerTooOldException e)
     {
         return(BadRequest(e.Message));
     }
 }