Exemplo n.º 1
0
        public void SetCategoryCOA()
        {
            var jsonSerializerSettings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            var token = GetTokenAsync().Result;

            var categoryUri = APIEndpoint.Core + $"master/categories?size={int.MaxValue}";
            //var masterUnitUri = $"https://com-danliris-service-core-dev.azurewebsites.net/v1/master/units/simple";
            var categoryResponse = _http.GetAsync(categoryUri, token).Result;

            var categoryResult = new BaseResponse <List <CategoryCOAResult> >()
            {
                data = new List <CategoryCOAResult>()
            };

            if (categoryResponse.IsSuccessStatusCode)
            {
                categoryResult = JsonConvert.DeserializeObject <BaseResponse <List <CategoryCOAResult> > >(categoryResponse.Content.ReadAsStringAsync().Result, jsonSerializerSettings);
            }
            //else
            //{
            //    SetCategoryCOA();
            //}

            //if (categoryResult.data.Count > 0)
            _cacheManager.Set("Categories", categoryResult.data);
        }
Exemplo n.º 2
0
 public static T Get <T>(this IMemoryCacheManager cacheManager, string key, Func <T> acquire, int cacheTime = 60)
 {
     if (cacheManager.IsSet(key))
     {
         return(cacheManager.Get <T>(key));
     }
     else
     {
         var result = acquire();
         cacheManager.Set(key, result, cacheTime);
         return(result);
     }
 }
Exemplo n.º 3
0
        public async Task <T> GetItemFromHashAsync <T>(string redisKey, string redisHashKey)
        {
            var compareKey = GenerateKey(redisKey + redisHashKey);

            if (_memoryCacheManager.IsSet(compareKey))
            {
                return(_memoryCacheManager.Get <T>(compareKey));
            }

            var rValue = await _db.HashGetAsync(GenerateKey(redisKey), redisHashKey);

            if (!rValue.HasValue)
            {
                _memoryCacheManager.Set(compareKey, 0, MemoryCacheTime);
                return(default(T));
            }

            var resultValue = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue);

            _memoryCacheManager.Set(compareKey, resultValue, MemoryCacheTime);

            return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue));
        }
        public void SetBankAccount()
        {
            var jsonSerializerSettings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            var token = GetTokenAsync().Result;

            var bankAccountUri      = APIEndpoint.Core + $"master/account-banks?size={int.MaxValue}";
            var bankAccountResponse = _http.GetAsync(bankAccountUri, token).Result;

            var bankAccountResult = new BaseResponse <List <BankAccountCOAResult> >()
            {
                data = new List <BankAccountCOAResult>()
            };

            if (bankAccountResponse.IsSuccessStatusCode)
            {
                bankAccountResult = JsonConvert.DeserializeObject <BaseResponse <List <BankAccountCOAResult> > >(bankAccountResponse.Content.ReadAsStringAsync().Result, jsonSerializerSettings);
            }

            _cacheManager.Set(MemoryCacheConstant.BankAccounts, bankAccountResult.data);
        }
Exemplo n.º 5
0
        public async Task <IEnumerable <CategoryDto> > Handle(GetCategoriesQuery request, CancellationToken cancellationToken)
        {
            if (!_memoryCacheManager.TryGetValue(nameof(GetCategoriesQuery), out IEnumerable <CategoryDto> categoryDtos))
            {
                var categories = await _unitOfWork.Categories.GetAllAsync();

                categoryDtos = categories.Select(c => new CategoryDto
                {
                    Id   = c.Id,
                    Name = c.Name
                });

                _memoryCacheManager.Set(nameof(GetCategoriesQuery), categoryDtos);
            }
            return(categoryDtos);
        }
        public async Task <AccessTokenResult> GetAccessToken()
        {
            var cacheKey = "_GraphToken";

            var graphToken = _memoryCacheManager.Get <AccessTokenResult>(cacheKey);

            if (!string.IsNullOrEmpty(graphToken?.Access_Token))
            {
                return(graphToken);
            }

            var clientCredentials = new ClientCredential(_client_secret);
            var app = new ConfidentialClientApplication(_client_id, $"{_host}/{_tenant}", "https://graph.microsoft.com", clientCredentials, null, new TokenCache());

            // With client credentials flows the scopes is ALWAYS of the shape "resource/.default", as the
            // application permissions need to be set statically (in the portal or by PowerShell), and then granted by
            // a tenant administrator
            var scopes = new string[] { _scope };

            AuthenticationResult result = null;

            try
            {
                result = await app.AcquireTokenForClientAsync(scopes);
            }
            catch (MsalServiceException ex) when(ex.Message.Contains("AADSTS70011"))
            {
                // Invalid scope. The scope has to be of the form "https://resourceurl/.default"
                // Mitigation: change the scope to be as expected
            }
            var token = new AccessTokenResult
            {
                Access_Token = result.AccessToken,
                Expires_In   = result.ExpiresOn.ToString(),
            };

            var cacheTime = result.ExpiresOn - DateTime.UtcNow;

            //cacheTime = TimeSpan.FromMinutes(5);
            _memoryCacheManager.Set(cacheKey, token, cacheTime);

            return(token);
        }
        //public void SetMachineSpinning()
        //{
        //    var masterMachineSpinningUri = MasterDataSettings.Endpoint + $"machine-spinnings/simple";
        //    //var masterUnitUri = $"https://com-danliris-service-core-dev.azurewebsites.net/v1/master/units/simple";
        //    var machineSpinningResponse = _http.GetAsync(masterMachineSpinningUri, Token).Result;

        //    var machineSpinningResult = new MachineSpinningResult();
        //    if (machineSpinningResponse.EnsureSuccessStatusCode().IsSuccessStatusCode)
        //    {
        //        machineSpinningResult = JsonConvert.DeserializeObject<MachineSpinningResult>(machineSpinningResponse.Content.ReadAsStringAsync().Result);
        //    }
        //    else
        //    {
        //        SetMachineSpinning();
        //    }

        //    if (machineSpinningResult.data.Count > 0)
        //        cacheManager.Set("MachineSpinnings", machineSpinningResult.data);

        //}

        public void SetMachineType()
        {
            var masterMachineSpinningUri = MasterDataSettings.Endpoint + $"machine-spinnings/machine/types";
            //var masterUnitUri = $"https://com-danliris-service-core-dev.azurewebsites.net/v1/master/units/simple";
            var machineSpinningResponse = _http.GetAsync(masterMachineSpinningUri, Token).Result;

            List <string> result = new List <string>();

            if (machineSpinningResponse.EnsureSuccessStatusCode().IsSuccessStatusCode)
            {
                result = JsonConvert.DeserializeObject <List <string> >(machineSpinningResponse.Content.ReadAsStringAsync().Result);
            }
            else
            {
                SetMachineType();
            }

            if (result.Count > 0)
            {
                cacheManager.Set("MachineTypes", result);
            }
        }