Example #1
0
        public async Task AddPlayersToMatch(int MatchId, IEnumerable <int> PlayersId)    // metoda dodaje zawodnikow do meczu
        {
            var match = await _matchRepository.GetAsyncMatch(MatchId);

            if (match == null)
            {
                throw new Exception($"Match with this id {MatchId} does not exist");
            }
            //await _matchRepository.AddAsyncPlayerToMatch(MatchId,PlayersId);
            var playersListInMatch = await _matchRepository.GetAsyncPlayersInMatch(MatchId);

            foreach (int playerId in PlayersId)       //przechodze przez wszystkie id playersow ktorych chce dodac do meczu
            {
                var plr2matches = new MatchhPlayer(); //deklaruje obiekt tablicy posredniej

                bool IsInList = false;
                IsInList = playersListInMatch.Players2Match.Exists(x => x.PlayerId == playerId); // jezeli jest juz przypisany to tu zmienia na prawde

                if (IsInList == false)                                                           //dzieki temu widzimy czy nalezy jezeli falsz czyli nie to dodajemy
                {
                    var player = await _playerRepository.GetAsyncPlayer(playerId);               //pobieram zawodnika o wskazanym id

                    if (player == null)
                    {
                        continue;
                    }
                    else
                    {
                        plr2matches.Matchh = match;   //wypelniamy obiekt tabeli posredniej
                        plr2matches.Player = player;
                        await _matchRepository.AddAsyncPlayerToMatch(MatchId, plr2matches);

                        // match.Players2Match.Add(plr2matches);
                    }
                }
            }

            await Task.CompletedTask;
        }