private void ValidateTeam(DAL.Models.Tourney tourney, DAL.Models.Team team)
 {
     if (!tourney.Teams.Any(t => t.TeamId == team.Id))
     {
         throw new ActionCannotBeExecutedException(ExceptionMessages.TeamNotFound + $"in Tourney by Id = {tourney.Id} TeamId : {team.Id}");
     }
 }
 private void ValidatePlayers(DAL.Models.Team team, IEnumerable <int> players)
 {
     foreach (int playerId in players)
     {
         if (!team.Players.Any(p => p.Id == playerId))
         {
             throw new ActionCannotBeExecutedException(ExceptionMessages.PlayerNotFound + $"in Team by Id : {team.Id} PlayerId : {playerId}");
         }
     }
 }
        public async Task <IHttpActionResult> CreateAsync([FromUri] int id, [FromBody] CreateRequest request)
        {
            DAL.Models.Tourney tourney = await UnitOfWork.GetTourneyRepository().SelectByIdAsync(id)
                                         ?? throw new ActionCannotBeExecutedException(ExceptionMessages.TourneyNotFound);

            if (request.HomeId == request.GuestId)
            {
                throw new ActionCannotBeExecutedException(ExceptionMessages.TeamsMustBeDifferent);
            }

            //if (tourney.EndDt < DateTime.Now)
            //{
            //	throw new ActionCannotBeExecutedException(ExceptionMessages.TourenyFinished);
            //}

            if (request.GuestPlayersMain.Distinct().Count() != 11 || request.HomePlayersMain.Distinct().Count() != 11)
            {
                throw new ActionCannotBeExecutedException(ExceptionMessages.InvalidPlayersCount);
            }

            if (request.GuestPlayersSpare.Distinct().Count() != 5 || request.HomePlayersSpare.Distinct().Count() != 5)
            {
                throw new ActionCannotBeExecutedException(ExceptionMessages.InvalidPlayersCount);
            }

            if (request.GuestPlayersMain.Any(p => request.GuestPlayersSpare.Contains(p)) ||
                request.HomePlayersMain.Any(p => request.HomePlayersSpare.Contains(p)))
            {
                throw new ActionCannotBeExecutedException(ExceptionMessages.PlayerCanBeOnlyOneComposition);
            }

            ITeamRepository teamRepo = UnitOfWork.GetTeamRepository();
            SelectOptions <DAL.Models.Team> teamSelectOption = new SelectOptions <DAL.Models.Team>();

            teamSelectOption.Includes.Add(t => t.Players);

            DAL.Models.Team homeTeam = await teamRepo.SelectByIdAsync(request.HomeId, teamSelectOption)
                                       ?? throw new ActionCannotBeExecutedException(ExceptionMessages.TeamNotFound + $" Id : {request.HomeId}");

            ValidateTeam(tourney, homeTeam);

            DAL.Models.Team guestTeam = await teamRepo.SelectByIdAsync(request.GuestId, teamSelectOption)
                                        ?? throw new ActionCannotBeExecutedException(ExceptionMessages.TeamNotFound + $" Id : {request.GuestId}");

            ValidateTeam(tourney, guestTeam);

            ValidatePlayers(homeTeam, request.HomePlayersMain);
            ValidatePlayers(guestTeam, request.GuestPlayersMain);

            DAL.Models.Match match = new DAL.Models.Match
            {
                StartDt = request.StartDt,
                Home    = homeTeam,
                Guest   = guestTeam,
                Status  = MatchStatus.Started,
                Tourney = tourney
            };

            foreach (DAL.Models.Player player in homeTeam.Players.Where(p => request.HomePlayersMain.Contains(p.Id))
                     .Concat(guestTeam.Players.Where(p => request.GuestPlayersMain.Contains(p.Id))))
            {
                match.Players.Add(new DAL.Models.MatchPlayer
                {
                    Player = player,
                    Status = MatchPlayerStatus.Main
                });
            }

            foreach (DAL.Models.Player player in homeTeam.Players.Where(p => request.HomePlayersSpare.Contains(p.Id))
                     .Concat(guestTeam.Players.Where(p => request.GuestPlayersSpare.Contains(p.Id))))
            {
                match.Players.Add(new DAL.Models.MatchPlayer
                {
                    Player = player,
                    Status = MatchPlayerStatus.Spare
                });
            }

            match.EndDt = match.StartDt.AddMinutes(90);

            UnitOfWork.GetMatchRepository().Insert(match);

            await UnitOfWork.SaveChangesAsync();

            return(Ok());
        }