public async Task <HttpResponseMessage> Get(string eventName) { eventBitEntities entities = new eventBitEntities(); //Grab my X-AUTH from header. string reqAuth = Request.Headers.GetValues("X-AUTH-CLAIMS").First(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://dev.experienteventbit.com/webapi/API/Event/"); client.DefaultRequestHeaders.Add("X-AUTH-CLAIMS", reqAuth); HttpResponseMessage response = client.GetAsync(eventName + "/TrackingData").Result; string newXAuthHeader = response.Headers.GetValues("X-AUTH-CLAIMS").First(); var data = await response.Content.ReadAsStringAsync(); //Check for error HttpResponseMessage r = new HttpResponseMessage(); if (response.StatusCode == HttpStatusCode.OK) { var d = JsonConvert.DeserializeObject <TrackedData>(data); SnapshotState ssState; //Check my snapshot id. ssState = entities.SnapshotStates.FirstOrDefault(x => x.ShowCode == eventName); if (ssState != null) { if (ssState.UniqueIdentifier == d.UniqueIdentifier) { return(RH.OK(newXAuthHeader, "Snapshot unique identifier matches currently loaded snapshot, no new snapshot available.")); } } else { ssState = new SnapshotState(); ssState.ShowCode = eventName; entities.SnapshotStates.Add(ssState); } ProcessTrackedData(d, eventName); ssState.UniqueIdentifier = d.UniqueIdentifier; entities.SaveChanges(); return(RH.OK(newXAuthHeader, "Snapshot sync complete.")); } else { return(RH.BadRequest(newXAuthHeader, data)); } }
private void ProcessDataToEntitiesGeneric <T>(dynamic d, string id, out int sysEventID) where T : class { // var ent = entities.Set<t.GetType()>(); //I want to make sure I dispose of this here using (eventBitEntities entities = new eventBitEntities()) { var table = entities.Set <T>(); //I pass in show code but everything is based on sysEventId?? sysEventID = 0; foreach (JObject data in d) { var jsonEnt = data.ToObject <T>(); int pKey = Convert.ToInt32(jsonEnt.GetType().GetProperty(id + "ID").GetValue(jsonEnt, null)); if (sysEventID == 0) { sysEventID = Convert.ToInt32(jsonEnt.GetType().GetProperty("sysEventID").GetValue(jsonEnt, null)); } var ent = table.Find(pKey); if (ent == null) { table.Add(jsonEnt); } else { CopyPropertyValues(jsonEnt, ent); } } entities.SaveChanges(); } //Expression Tree? Maybe? }