Пример #1
0
        public ActionResult News(string symbol, DateTimeOffset from, DateTimeOffset to)
        {
            symbol = symbol.ToUpperInvariant();
            ArticleList articleList = repository.GetNewsArticles("company", symbol);

            if (articleList is not null && !articleList.IsStale(15))
            {
                IEnumerable <NewsArticle> ret = articleList.Articles.Where(a =>
                {
                    DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(a.DateTime);
                    return(dto >= from && dto <= to);
                });
                return(Ok(ret));
            }

            var fromStr    = from.ToString("yyyy-MM-dd");
            var toStr      = to.ToString("yyyy-MM-dd");
            var parameters =
                $"https://finnhub.io/api/v1/company-news?symbol={symbol}&from={fromStr}&to={toStr}";

            HttpResponseMessage response = finnhubClient.GetAsync(parameters).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(BadRequest($"Status code: {response.StatusCode}"));
            }

            List <FinnhubArticle> articles =
                response.Content.ReadFromJsonAsync <List <FinnhubArticle> >(serializerOptions).Result;

            bool shouldAdd = articleList is null;

            articleList = new ArticleList
            {
                Category    = "company",
                Symbol      = symbol,
                Articles    = mapper.Map <List <NewsArticle> >(articles),
                LastUpdated = DateTimeOffset.UtcNow
            };

            if (shouldAdd)
            {
                repository.AddEntity(articleList);
            }

            repository.SaveAll();

            IEnumerable <NewsArticle> articlesRet = articleList.Articles.Where(a =>
            {
                DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(a.DateTime);
                return(dto >= from && dto <= to);
            });

            return(Ok(articlesRet));
        }
Пример #2
0
        public ActionResult MarketNews(string category, int minId = 0)
        {
            category = category.ToLowerInvariant();
            ArticleList articleList = repository.GetNewsArticles(category);

            if (articleList is not null && !articleList.IsStale(15))
            {
                IEnumerable <NewsArticle> ret = articleList.Articles.Where(a => a.Id >= minId);
                return(Ok(ret));
            }

            var parameters = $"https://finnhub.io/api/v1/news?category={category}&minId={minId}";
            HttpResponseMessage response = finnhubClient.GetAsync(parameters).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(BadRequest($"Status code: {response.StatusCode}"));
            }

            List <FinnhubArticle> articles =
                response.Content.ReadFromJsonAsync <List <FinnhubArticle> >(serializerOptions).Result;

            bool shouldAdd = articleList is null;

            articleList = new ArticleList
            {
                Category    = category,
                Symbol      = "",
                Articles    = mapper.Map <List <NewsArticle> >(articles),
                LastUpdated = DateTimeOffset.UtcNow
            };

            if (shouldAdd)
            {
                repository.AddEntity(articleList);
            }

            repository.SaveAll();

            return(Ok(articleList.Articles));
        }