コード例 #1
0
        public async Task <IActionResult> BulkAddFixture([FromRoute] short year, [FromRoute] byte season,
                                                         [FromBody] IList <IList <MatchAddRequest> > newMatches, [FromQuery] byte startingFromRound = 1)
        {
            try
            {
                var teams = (await _teamsService.GetYearTeamsAsync(year)).ToDictionary(team => team.Abbreviation.ToLowerInvariant(), team => team);

                foreach (var match in newMatches.SelectMany(m => m.Select(match => match)))
                {
                    if (!teams.ContainsKey(match.HomeTeamAbbr.ToLowerInvariant()) || !teams.ContainsKey(match.AwayTeamAbbr.ToLowerInvariant()))
                    {
                        throw new ArgumentException($"Teams not found for match {match.HomeTeamAbbr}-{match.AwayTeamAbbr}.");
                    }
                    match.HomeTeamId   = teams[match.HomeTeamAbbr.ToLowerInvariant()].Id;
                    match.AwayTeamId   = teams[match.AwayTeamAbbr.ToLowerInvariant()].Id;
                    match.HomeTeamName = teams[match.HomeTeamAbbr.ToLowerInvariant()].Name;
                    match.AwayTeamName = teams[match.AwayTeamAbbr.ToLowerInvariant()].Name;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error matching teams.", ex);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error matching teams."));
            }
            try
            {
                byte round = startingFromRound;
                foreach (var roundMatches in newMatches)
                {
                    await _matchesService.BulkAddMatches(_mapper.Map <IList <Match> >(roundMatches,

                                                                                      opt =>
                    {
                        opt.Items["year"]   = year;
                        opt.Items["season"] = season;
                        opt.Items["round"]  = round;
                    }));

                    _logger.LogDebug($"{roundMatches.Count} matches Created for {year}:{season}:{round}.");
                    round++;
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error bulk adding matches.", ex);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error adding matches."));
            }
        }