示例#1
0
 public IActionResult AddNewPlayerToTeam(int id, AddNewPlayerToTeamRequest request)
 {
     try
     {
         _services.AddNewPlayerToTeam(id, request);
         return(Ok());
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(BadRequest(e.Message));
     }
 }
示例#2
0
        public async Task AddNewPlayerToTeam(int IdTeam, AddNewPlayerToTeamRequest request)
        {
            try
            {
                var playerInfo = (from p in _context.Player where p.FirstName == request.FirstName && p.LastName == request.LastName && p.DateOfBirth == request.BirthDate select p).FirstOrDefault();
                if (playerInfo == null)
                {
                    throw new Exception("Nie istnieje zawodnik o podanych danych");
                }

                var IsInTeam = (from t in _context.PlayerTeam where t.IdPlayer == playerInfo.IdPlayer select t).FirstOrDefault();
                if (IsInTeam != null)
                {
                    throw new Exception("Zawodnik nalezy juz do druzyny");
                }

                var maxAge = (from t in _context.Team where t.IdTeam == IdTeam select t.MaxAge).FirstOrDefault();
                if (maxAge == 0)
                {
                    throw new Exception("Druzyna o podanym id nie istnieje");
                }

                if (DateTime.Now.Year - playerInfo.DateOfBirth.Year < maxAge)
                {
                    _context.PlayerTeam.Add(new PlayerTeam {
                        IdPlayer = playerInfo.IdPlayer, IdTeam = IdTeam, Comment = request.Comment, NumOnShirt = request.NumOnShirt
                    });
                    _context.SaveChanges();
                }
                else
                {
                    throw new Exception("Zawodnik jest za stary");
                }
            }catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }