public bool SyncAreas() { ResourceAreas resourceAreas = new ResourceAreas(apiToken); // Object to comunicate with API football-data.org. DALAreas dalAreas = new DALAreas(connectionString); // Object to comunicate with Database. List <Area> lstApiAreas = resourceAreas.GetAll(); // All areas available on API. List <Area> lstDbAreas = dalAreas.GetAll(); // All areas available on Database. List <Area> lstInsertAreas = new List <Area>(); // Temporary List<Areas> to insert. List <Area> lstUpdateAreas = new List <Area>(); // Temporary List<Areas> to update. if (lstDbAreas.Count == 0) // In case theres no record in database we can just insert all AreasApi. { dalAreas.Insert(lstApiAreas); return(true); } foreach (Area areaApi in lstApiAreas) // If database has alreday some records then... { Area tempArea = dalAreas.GetById(areaApi.Id.ToString()); if (tempArea == null) { lstInsertAreas.Add(areaApi); } else if (areaApi.Name != tempArea.Name || areaApi.ParentAreaId != tempArea.ParentAreaId) // Check for differences. { lstUpdateAreas.Add(areaApi); } } if (lstInsertAreas.Count != 0) { dalAreas.Insert(lstInsertAreas); } if (lstUpdateAreas.Count != 0) { dalAreas.Update(lstUpdateAreas); } return(true); }
public Area GetAreaById(string Id) { DALAreas dalAreas = new DALAreas(connectionString); return(dalAreas.GetById(Id)); }