コード例 #1
0
        public async Task <IActionResult> getallbillers()
        {
            List <Biller> billers = null;
            BillerError   e       = new BillerError();

            CancellationTokenSource cts;

            cts = new CancellationTokenSource();
            cts.CancelAfter(settings.Value.redisCancellationToken);

            Redis  redis = new Redis(settings, cache);
            string key   = "all_billers";

            //validate request
            if (!ModelState.IsValid)
            {
                var modelErrors = new List <BillerError>();
                var eD          = new List <string>();
                foreach (var modelState in ModelState.Values)
                {
                    foreach (var modelError in modelState.Errors)
                    {
                        eD.Add(modelError.ErrorMessage);
                    }
                }
                e.error        = ((int)HttpStatusCode.BadRequest).ToString();
                e.errorDetails = eD;

                return(BadRequest(e));
            }

            //check redis cache for details
            try
            {
                billers = await redis.getbillers(key, cts.Token);

                if (billers != null && billers.Count > 0)
                {
                    return(CreatedAtAction("getallbillers", billers));
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            //Get Billers from Sql
            try
            {
                billers = await billerSqlRepo.GetAllBillers();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            //Write to Redis
            try
            {
                if (billers != null && billers.Count > 0)
                {
                    await redis.setbillers(key, billers, cts.Token);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            return(CreatedAtAction("getallbillers", billers));
        }
コード例 #2
0
        public async Task <IActionResult> addbiller([FromBody] Biller biller)
        {
            Biller _biller = null;

            BillerError e     = new BillerError();
            Redis       redis = new Redis(settings, cache);

            CancellationTokenSource cts;

            cts = new CancellationTokenSource();
            cts.CancelAfter(settings.Value.redisCancellationToken);

            biller.created_on           = DateTime.Now;
            biller.status               = (int)Status.Active;
            biller.billercontact.status = (int)Status.Active;

            //validate request
            if (!ModelState.IsValid)
            {
                var modelErrors = new List <BillerError>();
                var eD          = new List <string>();
                foreach (var modelState in ModelState.Values)
                {
                    foreach (var modelError in modelState.Errors)
                    {
                        eD.Add(modelError.ErrorMessage);
                    }
                }
                e.error        = ((int)HttpStatusCode.BadRequest).ToString();
                e.errorDetails = eD;

                return(BadRequest(e));
            }

            //produce on kafka
            try
            {
                //string billerText = JsonHelper.toJson(_biller);
                //var config = new Dictionary<string, object> { { "bootstrap.servers", settings.Value.brokerList } };
                //using (var producer = new Producer<Null, string>(config, null, new StringSerializer(Encoding.UTF8)))
                //{
                //    var deliveryReport = await producer.ProduceAsync(settings.Value.addBillerTopic, null, billerText);
                //    var result = deliveryReport.Value;
                //    producer.Flush(TimeSpan.FromSeconds(1));

                //}
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            //return _biller;


            //Add to mongo
            try
            {
                //_biller = await billerMongoRepo.AddBiller(biller);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            //Add Biller to sql server
            try
            {
                if (biller.id == 0)
                {
                    _biller = await billerSqlRepo.AddBiller(biller);
                }
                else if (biller.id > 0)
                {
                    _biller = await billerSqlRepo.UpdateBiller(biller);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            //Add to redis
            try
            {
                if (_biller != null)
                {
                    string key = "billers_with_id_" + _biller.id;

                    await redis.setbillerAsync(key, _biller, cts.Token);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            return(CreatedAtAction("addbiller", biller));
        }