Пример #1
0
        public async Task <IActionResult> StockExchangeSecurityRequest(Guid id, string exchangeId, string stockStatus)
        {
            Authentication authentication = new Authentication(_ingestionContext, _logger);
            TokenEntity    tokenEntity    = await authentication.GetAccessTokenByClientConfigId(id);

            if (tokenEntity != null)
            {
                string endPoint = _configuration.GetValue <string>("MorningStar:EquityApi:StockExchangeSecurityList");
                EquityApi.StockExchangeSecurity.Request request =
                    EquityApi.StockExchangeSecurity.Request.Create(tokenEntity.Token, exchangeId, "exchangeId", exchangeId, stockStatus);
                string requestUrl = endPoint.SetQueryParams(request);

                IngestionTask ingestionTask = IngestionTask.Create(endPoint, JsonConvert.SerializeObject(request));
                _ingestionContext.IngestionTasks.Add(ingestionTask);

                EquityApi.StockExchangeSecurity.Response response = await RestClient.GetDynamicResponseAsync <EquityApi.StockExchangeSecurity.Response>(requestUrl);

                PersistenceService persistence = new PersistenceService(_ingestionContext, _mapper, _logger);
                int result = await persistence.SaveAsync(ingestionTask.IngestionTaskId, response);

                if (result > 0)
                {
                    List <ChangeTable> changes = _ingestionContext.ChangeTables.FromSqlRaw("EXECUTE MergeStockExchangeSecurity @TaskId={0}", ingestionTask.IngestionTaskId).ToList();
                    _logger.LogInformation("MergeStockExchangeSecurity: {0}", JsonConvert.SerializeObject(changes));
                }

                return(Ok(result));
            }
            return(NotFound(id));
        }
        public async Task <int> SaveAsync(Guid ingestionTaskId, EquityApi.StockExchangeSecurity.Response response)
        {
            try
            {
                List <StockExchangeSecurityEntity> entities = response.StockExchangeSecurityEntityList;
                if (ValidateEntities(entities) == 0)
                {
                    return(0);
                }

                List <TStockExchangeSecurity> list = new List <TStockExchangeSecurity>();
                foreach (var entity in entities)
                {
                    TStockExchangeSecurity item = _mapper.Map <TStockExchangeSecurity>(entity);
                    item.Id = Guid.NewGuid();
                    item.IngestionTaskId = ingestionTaskId;

                    list.Add(item);
                }
                _ingestionContext.TStockExchangeSecurities.AddRange(list);
                return(await _ingestionContext.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                throw new Exception(ex.Message, ex.InnerException);
            }
        }