Exemplo n.º 1
0
        public async Task <IActionResult> Get(string leagueCode)
        {
            var competition = await _footballRepository.GetCompetitionAsync(x => x.Code.Equals(leagueCode));

            if (competition != null)
            {
                return(StatusCode(StatusCodes.Status409Conflict, new { message = "League already imported" }));
            }

            var competitionModel = await _footballClient.GetCompetitionByCodeAsync(leagueCode);

            if (competitionModel.Id == 0)
            {
                return(NotFound(new { message = "Not found" }));
            }

            competition = _mapper.Map <Competition>(competitionModel);

            var competitionTeamModel = await _footballClient.GetTeamsByCompetitionAsync(competitionModel.Id);

            var teams = _mapper.Map <List <Team> >(competitionTeamModel.Teams);

            await GetPlayersForTeams(teams);

            _footballRepository.Add(competition);

            await _footballRepository.UpsertTeamsAsync(teams);

            await _footballRepository.UpsertCompetitionTeamsAsync(GetCompetitionTeams(competition, teams));

            await _footballRepository.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), leagueCode, new { message = "Successfully imported" }));
        }