Ejemplo n.º 1
0
        public async Task <List <ArticleDto> > GetArticles(GetArticlesDTO getArticlesDTO)
        {
            List <ArticleDto> allArticles = new List <ArticleDto>();

            try
            {
                Parallel.ForEach(getArticlesDTO.TagList, async(tag) =>
                {
                    List <ArticleDto> bingArticles    = new List <ArticleDto>();
                    List <ArticleDto> newsApiArticles = new List <ArticleDto>();
                    Task[] getArticleTasks            = new Task[]
                    {
                        Task.Run(() => bingArticles    = this._articleEngine.GetBingApiArticles(tag.Name).Result),
                        Task.Run(() => newsApiArticles = this._articleEngine.GetNewsApiArticles(tag.Name, tag.Id).Result),
                    };
                    Task.WaitAll(getArticleTasks);
                    if (bingArticles.Count > 0)
                    {
                        if (newsApiArticles.Count > 0)
                        {
                            allArticles.AddRange(newsApiArticles.Concat(bingArticles).ToList());
                        }
                        else
                        {
                            allArticles.AddRange(bingArticles);
                        }
                    }
                    else
                    {
                        allArticles.AddRange(newsApiArticles);
                    }

                    Task.Run(() => this._articleEngine.AddArticlesAsync(getArticlesDTO.UserId, tag.Id, allArticles));
                });
            }
            catch (Exception ex)
            {
                return(allArticles);
            }
            return(allArticles);
        }
Ejemplo n.º 2
0
 public async Task <List <ArticleDto> > GetArticles([FromBody] GetArticlesDTO getArticlesDTO)
 {
     return(await this._articleManager.GetArticles(getArticlesDTO));
 }