示例#1
0
        public bool SyncSeasons()
        {
            ResourceCompetitions resourceCompetitions = new ResourceCompetitions(apiToken);         // Object to comunicate with API football-data.org.
            DALSeasons           dalSeasons           = new DALSeasons(connectionString);           // Object to comunicate with Database.

            List <Competition> lstApiCompetitions = resourceCompetitions.GetAll();                  // All competitions available on API.
            List <Season>      lstDbSeasons       = dalSeasons.GetAll();                            // All seasons available on Database.

            List <Season> lstInsertSeasons = new List <Season>();                                   // Temporary List<Areas> to insert.
            List <Season> lstUpdateSeasosn = new List <Season>();                                   // Temporary List<Areas> to update.


            if (lstDbSeasons.Count == 0)                                                            // In case theres no record in database we can just insert all Seasons.
            {
                foreach (Competition competition in lstApiCompetitions)
                {
                    if (competition.CurrentSeason != null)
                    {
                        lstInsertSeasons.Add(competition.CurrentSeason);
                    }
                }

                dalSeasons.Insert(lstInsertSeasons);
                return(true);
            }


            foreach (Competition apiCompetition in lstApiCompetitions)                                 // If database has alreday some records then...
            {
                if (apiCompetition.CurrentSeason != null)
                {
                    Season dbCompetitionCurrentSeason = dalSeasons.GetById(apiCompetition.CurrentSeason.Id.ToString());

                    if (dbCompetitionCurrentSeason == null)
                    {
                        lstDbSeasons.Add(apiCompetition.CurrentSeason);
                    }
                    else if (apiCompetition.CurrentSeason.StartDate != dbCompetitionCurrentSeason.StartDate ||
                             apiCompetition.CurrentSeason.EndDate != dbCompetitionCurrentSeason.EndDate)               // Checks for any difference.
                    {
                        lstUpdateSeasosn.Add(apiCompetition.CurrentSeason);
                    }
                }
            }


            if (lstInsertSeasons.Count != 0)
            {
                dalSeasons.Insert(lstInsertSeasons);
            }
            if (lstUpdateSeasosn.Count != 0)
            {
                dalSeasons.Update(lstUpdateSeasosn);
            }


            return(true);
        }
示例#2
0
        public bool SyncCompetitions()
        {
            ResourceCompetitions resourceCompetitions = new ResourceCompetitions(apiToken);         // Object to comunicate with API football-data.org.
            DALCompetitions      dalCompetitions      = new DALCompetitions(connectionString);      // Object to comunicate with Database.

            List <Competition> lstApiCompetition = resourceCompetitions.GetAll();                   // All Competition available on API.
            List <Competition> lstDbCompetition  = dalCompetitions.GetAll();                        // All Competition available on Database.

            List <Competition> lstInsertCompetition = new List <Competition>();                     // Temporary List<Competition> to insert.
            List <Competition> lstUpdateCompetition = new List <Competition>();                     // Temporary List<Competition> to update.


            if (lstDbCompetition.Count == 0)                                                        // In case theres no record in database we can just insert all CompetitionApi.
            {
                dalCompetitions.Insert(lstApiCompetition);
                return(true);
            }


            foreach (Competition competitionApi in lstApiCompetition)                               // If database has alreday some records then...
            {
                Competition competitionDb = dalCompetitions.GetById(competitionApi.Id.ToString());

                if (competitionDb == null)
                {
                    lstInsertCompetition.Add(competitionApi);
                }
                else
                {
                    DateTime?competitionApiLastUpdated = NormalizeApiDateTime(competitionApi.LastUpdated);

                    if (competitionApiLastUpdated.ToString() != competitionDb.LastUpdated)
                    {
                        lstUpdateCompetition.Add(competitionApi);
                    }
                }
            }

            if (lstInsertCompetition.Count != 0)
            {
                dalCompetitions.Insert(lstInsertCompetition);
            }
            if (lstUpdateCompetition.Count != 0)
            {
                dalCompetitions.Update(lstUpdateCompetition);
            }


            return(true);
        }