private void InitializeApiModel()
 {
     this.apiModel = new ImportTeamsApi
     {
         Api = new ImportTeamsInputModel
         {
             Results = 1,
             Teams   = new ImportTeamInputModel[]
             {
                 new ImportTeamInputModel
                 {
                     TeamId     = 3,
                     Name       = "Manchester United",
                     Code       = "MU",
                     IsNational = false,
                     Country    = "England",
                 },
             },
         },
     };
 }
Exemple #2
0
        public int Create(ImportTeamsApi models, int leagueId)
        {
            int totalTeams = 0;

            League  league  = this.leagueRepository.All().FirstOrDefault(x => x.Id == leagueId);
            Country country = this.countryRepository.All().FirstOrDefault(x => x.Name == models.Api.Teams[0].Country);


            if (league != null && country != null)
            {
                foreach (var model in models.Api.Teams)
                {
                    try
                    {
                        Team teamToCheck = this.teamRepository.All().FirstOrDefault(x => x.Id == model.TeamId);

                        // if the team already exists
                        if (teamToCheck != null)
                        {
                            int teamId = teamToCheck.Id;

                            // checking if the current team is being add in the mapping table
                            TeamLeagues teamLeague = this.teamLeaguesRepository
                                                     .All().FirstOrDefault(x => x.TeamId == teamId && x.LeagueId == leagueId);

                            // if its being added, doin nothing
                            if (teamLeague != null)
                            {
                                continue;
                            }

                            // if its not, create new mapping table
                            else
                            {
                                TeamLeagues teamsLeague = new TeamLeagues
                                {
                                    TeamId   = teamId,
                                    LeagueId = leagueId,
                                };

                                this.teamLeaguesRepository.Add(teamsLeague);
                            }
                        }

                        // if the team does not exists
                        else
                        {
                            Team team = new Team()
                            {
                                Id            = model.TeamId,
                                Name          = model.Name,
                                Code          = model.Code,
                                Logo          = model.Logo,
                                IsNational    = model.IsNational,
                                Founded       = model.Founded,
                                VenueName     = model.VenueName,
                                VenueCapacity = model.VenueCapacity,
                                CountryId     = country.Id,
                            };

                            TeamLeagues teamsLeague = new TeamLeagues
                            {
                                TeamId   = team.Id,
                                LeagueId = leagueId,
                            };

                            this.teamRepository.Add(team);
                            this.teamLeaguesRepository.Add(teamsLeague);
                            totalTeams++;
                        }
                    }
                    catch (Exception ex)
                    {
                        StringBuilder sb = new StringBuilder();
                        using StreamWriter sw = new StreamWriter(this.FilePath, true);

                        sb.AppendLine($"An exception occured at {DateTime.UtcNow}");
                        sb.AppendLine($"There is some problem with deserializating teams from json");
                        sb.AppendLine($"Original exception message: ");
                        sb.AppendLine(ex.Message);

                        if (ex.InnerException != null)
                        {
                            sb.AppendLine(ex.InnerException.ToString());
                        }

                        sb.AppendLine("Team Object: ");
                        sb.AppendLine(model.ToString());
                        sb.AppendLine("End of exception here.");
                        sb.AppendLine("--------------");
                        sb.AppendLine("---------------");
                        sw.Write(sb.ToString());
                        sw.Close();
                    }
                }

                this.teamRepository.SaveChanges();
                this.teamLeaguesRepository.SaveChanges();
            }

            return(totalTeams);
        }