public object Get(string showCode, string entityId) { PreviewDTO pDTO = new PreviewDTO(); SQLDataHelper SQLDA = new SQLDataHelper(); eventBitEntities entities = new eventBitEntities(); var entityState = entities.EntityStates.FirstOrDefault(x => x.ShowCode == showCode && x.EntityID == entityId); DataTable dt = new DataTable(); if (entityState != null) { dt = SQLDA.GetEntityDataTable(entityId, (entityState.sysEventId ?? 0)); pDTO.Columns = dt.Columns.Cast <DataColumn>().Where(x => !x.ColumnName.StartsWith("sys")) .Select(x => Char.ToLowerInvariant(x.ColumnName[0]) + x.ColumnName.Substring(1)) .ToArray(); pDTO.SysRowStampNumMax = entityState.sysRowStampNumMax.ToString(); pDTO.RowCount = SQLDA.GetRowCountForEntity(entityId, (entityState.sysEventId ?? 0)); } pDTO.Data = dt; return(pDTO); }
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)); } }
public async Task <HttpResponseMessage> Get(string id, string eventName) { EntityState entityState; //Clear up data structures using (eventBitEntities entities = new eventBitEntities()) { entityState = entities.EntityStates.FirstOrDefault(x => x.ShowCode == eventName && x.EntityID == id); if (entityState == null) { entityState = new EntityState(); entityState.EntityID = id; entityState.ShowCode = eventName; } EntityCallResponse resp = await GetEntityResponse(id, eventName, entityState.sysRowStampNumMax); //Error Check This Out. if (resp == null || resp.StatusCode != HttpStatusCode.OK) { return(RH.BadRequest(resp.X_AUTH_CLAIMS, resp.Content)); } var d = JsonConvert.DeserializeObject(resp.Content); ResponseDTO rDTO = new ResponseDTO(); rDTO.Count = ((JArray)d).Count; rDTO.LastSince = resp.RequestString; //Sync Data Here //There has to be a way I can get this to be more generic. if (rDTO.Count > 0) { try { //There has to be a way to make this SUPER generic. //Currently a messy switch. At least it makes it easy to debug. //ProcessDataToEntities(entities, entityState, d, id); int sysEventId = 0; GenericSwitchToAssignTypes(d, id, out sysEventId); UpdateEntitySyncLog(id, eventName, sysEventId); } catch (Exception e) { return(RH.BadRequest(resp.X_AUTH_CLAIMS, e.Message.ToString())); } } return(RH.OK(resp.X_AUTH_CLAIMS, JsonConvert.SerializeObject(rDTO))); } }
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? }