/// <summary> /// TryGet, Includes (Cause, Effect), OrderBy (Id, EventId, Order, Value, UpdatedDate) /// </summary> /// <param name="filter"></param> /// <param name="orderby"></param> /// <param name="ascending"></param> /// <param name="includeProperties"></param> /// <param name="onSuccess"></param> /// <param name="onFail"></param> /// <param name="state"></param> /// <returns></returns> public async Task TryGet(Expression <Func <Class, bool> > filter, string orderby, bool ascending, string includeProperties, Action <IEnumerable <Class>, string> onSuccess, Action <Exception, string> onFail, CascadingAppStateProvider state) { try { var serializer = new ExpressionSerializer(new BinarySerializer()); var bytes = serializer.SerializeBinary(filter); var predicateDeserialized = serializer.DeserializeBinary(bytes); string filterString = predicateDeserialized.ToString(); string key = ("causality_Class_tryget_" + filterString + "_" + orderby + "_" + ascending.ToString()).Replace(" ", "").ToLower(); List <Class> data = new(); bool getFromServer = false; string source = ""; if (state.AppState.UseIndexedDB) { var result = await _indexedDBManager.GetRecordByIndex <string, Blob>(new StoreIndexQuery <string> { Storename = _indexedDBManager.Stores[0].Name, IndexName = "key", QueryValue = key }); if (result is not null) { data = JsonConvert.DeserializeObject <List <Class> >(result.Value); source = "indexedDB"; } else if (await _onlineState.IsOnline()) { getFromServer = true; } else { throw new Exception("No connection"); } } else { getFromServer = true; } if (getFromServer) { ClassRequestGet req = new() { Filter = filterString, OrderBy = orderby, Ascending = ascending }; ClassResponseGet ret = await _classService.GetAsync(req); if (ret.Success) { foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { foreach (var item in ret.Classes) { if (includeProperty.ToLower().Equals("cause")) { CauseRequestGet _req = new() { Filter = "c => c.ClassId = " + item.Id, OrderBy = "Id", Ascending = true }; CauseResponseGet _ret = await _causeService.GetAsync(_req); item.Causes.Add(_ret.Causes); } if (includeProperty.ToLower().Equals("effect")) { EffectRequestGet _req = new() { Filter = "e => e.ClassId = " + item.Id, OrderBy = "Id", Ascending = true }; EffectResponseGet _ret = await _effectService.GetAsync(_req); item.Effects.Add(_ret.Effects); } } } data = ret.Classes.ToList(); source = ret.Status; if (state.AppState.UseIndexedDB) { await _indexedDBManager.AddRecord(new StoreRecord <Blob> { Storename = "Blobs", Data = new Blob() { Key = key, Value = JsonConvert.SerializeObject(data) } }); } } else { throw new Exception("No connection"); } } onSuccess(data, RequestCodes.TWO_ZERO_ZERO + ", recived " + data.Count.ToString() + " record from " + source); } catch (Exception e) { onFail(e, RequestCodes.FIVE_ZERO_ZERO); } }
/// <summary> /// TryGetById, Includes (Class, Cause, Effect, Exclude) /// </summary> /// <param name="id"></param> /// <param name="onSuccess"></param> /// <param name="onFail"></param> /// <param name="state"></param> /// <returns></returns> public async Task TryGetById(int id, string includeProperties, Action <Event, string> onSuccess, Action <Exception, string> onFail, CascadingAppStateProvider state) { try { string key = ("causality_Event_trygetbyid_" + id).Replace(" ", "").ToLower(); Event data = new(); bool getFromServer = false; string source = ""; if (state.AppState.UseIndexedDB) { var result = await _indexedDBManager.GetRecordByIndex <string, Blob>(new StoreIndexQuery <string> { Storename = _indexedDBManager.Stores[0].Name, IndexName = "key", QueryValue = key }); if (result is not null) { data = JsonConvert.DeserializeObject <Event>(result.Value); source = "indexedDB"; } else if (await _onlineState.IsOnline()) { getFromServer = true; } else { throw new Exception("No connection"); } } else { getFromServer = true; } if (getFromServer) { EventRequestGetById req = new() { Id = id }; EventResponseGetById ret = await _eventService.GetByIdAsync(req); if (ret.Success) { foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { if (includeProperty.ToLower().Equals("class")) { ClassRequestGet _req = new() { Filter = "c => c.EventId = " + ret.Event.Id, OrderBy = "Id", Ascending = true }; ClassResponseGet _ret = await _classService.GetAsync(_req); ret.Event.Classes.Add(_ret.Classes); } if (includeProperty.ToLower().Equals("cause")) { CauseRequestGet _req = new() { Filter = "c => c.EventId = " + ret.Event.Id, OrderBy = "Id", Ascending = true }; CauseResponseGet _ret = await _causeService.GetAsync(_req); ret.Event.Causes.Add(_ret.Causes); } if (includeProperty.ToLower().Equals("effect")) { EffectRequestGet _req = new() { Filter = "e => e.EventId = " + ret.Event.Id, OrderBy = "Id", Ascending = true }; EffectResponseGet _ret = await _effectService.GetAsync(_req); ret.Event.Effects.Add(_ret.Effects); } if (includeProperty.ToLower().Equals("exclude")) { ExcludeRequestGet _req = new() { Filter = "e => e.EventId = " + ret.Event.Id, OrderBy = "Id", Ascending = true }; ExcludeResponseGet _ret = await _excludeService.GetAsync(_req); ret.Event.Excludes.Add(_ret.Excludes); } } data = ret.Event; source = ret.Status; if (state.AppState.UseIndexedDB) { await _indexedDBManager.AddRecord(new StoreRecord <Blob> { Storename = "Blobs", Data = new Blob() { Key = key, Value = JsonConvert.SerializeObject(data) } }); } } else { throw new Exception("No connection"); } } onSuccess(data, RequestCodes.TWO_ZERO_ZERO + ", recived 1 record from " + source); } catch (Exception e) { onFail(e, RequestCodes.FIVE_ZERO_ZERO); } }