Exemple #1
0
        public override async Task <CauseResponseGetById> GetById(CauseRequestGetById request, ServerCallContext context)
        {
            string cacheKey = "Cause.GetById::" + request.Id.ToString();
            bool   IsCached = true;
            Cause  cacheEntry;
            var    response = new CauseResponseGetById();

            try
            {
                if (!_cache.TryGetValue <Cause>(cacheKey, out cacheEntry))
                {
                    cacheEntry = await _manager.GetById(request.Id);

                    var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(_cacheTimeInSeconds));
                    _cache.Set(cacheKey, cacheEntry, cacheEntryOptions);
                    IsCached = false;
                }
                response.Cause   = cacheEntry;
                response.Success = true;
                response.Status  = RequestCodes.TWO_ZERO_ZERO + ", recived 1 row from " + (IsCached ? Cache.MemoryCache : Cache.Database);
                response.Error   = "";
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Status  = RequestCodes.FIVE_ZERO_ZERO;
                response.Error   = e.ToString();
            }

            return(await Task.FromResult <CauseResponseGetById>(response));
        }
Exemple #2
0
        /// <summary>
        /// TryGetById, Includes (Effect, Exclude)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="includeProperties"></param>
        /// <param name="onSuccess"></param>
        /// <param name="onFail"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public async Task TryGetById(int id, string includeProperties, Action <Cause, string> onSuccess, Action <Exception, string> onFail, CascadingAppStateProvider state)
        {
            try
            {
                string key = ("causality_Cause_trygetbyid_" + id).Replace(" ", "").ToLower();

                Cause  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 <Cause>(result.Value);
                        source = "indexedDB";
                    }
                    else if (await _onlineState.IsOnline())
                    {
                        getFromServer = true;
                    }
                    else
                    {
                        throw new Exception("No connection");
                    }
                }
                else
                {
                    getFromServer = true;
                }

                if (getFromServer)
                {
                    CauseRequestGetById  req = new() { Id = id };
                    CauseResponseGetById ret = await _causeService.GetByIdAsync(req);

                    if (ret.Success)
                    {
                        foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (includeProperty.ToLower().Equals("effect"))
                            {
                                EffectRequestGet  _req = new() { Filter = "e => e.CauseId = " + ret.Cause.Id, OrderBy = "Id", Ascending = true };
                                EffectResponseGet _ret = await _effectService.GetAsync(_req);

                                ret.Cause.Effects.Add(_ret.Effects);
                            }
                            if (includeProperty.ToLower().Equals("exclude"))
                            {
                                ExcludeRequestGet  _req = new() { Filter = "e => e.CauseId = " + ret.Cause.Id, OrderBy = "Id", Ascending = true };
                                ExcludeResponseGet _ret = await _excludeService.GetAsync(_req);

                                ret.Cause.Excludes.Add(_ret.Excludes);
                            }
                        }
                        data   = ret.Cause;
                        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);
            }
        }