Exemplo n.º 1
0
        public IActionResult Get([FromQuery] SearchNewsModel searchOptions)
        {
            logger.LogInformation("Get action");

            var newsRepository = storage.GetRepository <INewsRepository>();

            IEnumerable <News> allNews;

            logger.LogInformation("Search all news.");
            allNews = newsRepository.All((options) =>
            {
                options.From     = searchOptions.From;
                options.Count    = searchOptions.Count;
                options.WithBody = searchOptions.WithBody;
            });

            var allNewsModel = allNews.Select(n => new
            {
                Id          = n.NewsId,
                Topic       = n.Topic,
                Body        = n.Body,
                PublishDate = n.PublishDate.ToShortDateString(),
                ImageSrc    = Path.Combine(fileService.LocalNewsImagesPath, n.ImageName)
            });

            logger.LogInformation("Return answer.");
            return(Ok(allNewsModel));
        }
Exemplo n.º 2
0
        public async System.Threading.Tasks.Task <ActionResult <IEnumerable <SearchNewsModel> > > NewsAsync(string searchInput)
        {
            //"https://newsapi.org/v2/everything?q=" + searchInput +"&from=2018-12-10&sortBy=publishedAt&apiKey=" + NewsApiKey
            string url = "https://newsapi.org/v2/everything?q=" + searchInput + "&from=2018-12-10&sortBy=publishedAt&apiKey=" + NewsApiKey;
            string jsonString;

            using (var httpClient = new HttpClient())
            {
                jsonString = await httpClient.GetStringAsync(url);
            }
            NewsObject             json = JsonConvert.DeserializeObject <NewsObject>(jsonString);
            List <SearchNewsModel> list = new List <SearchNewsModel>();

            foreach (var element in json.articles)
            {
                SearchNewsModel news = new SearchNewsModel();

                news.Author  = element.author;
                news.Content = element.content;
                news.Title   = element.title;
                list.Add(news);
            }

            return(list);
        }