Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        // COMPETITIONS METHODS.
        public List <Competition> GetAllCompetitions()
        {
            DALCompetitions dal = new DALCompetitions(connectionString);

            return(dal.GetAll());
        }