public ActionResult <IEnumerable <CountDTO> > SourceCount([FromQuery] SearchByDateDTO searchByDate)
        {
            if (searchByDate.EndingDate != null && searchByDate.StartingDate != null)
            {
                DateTime startDate;
                DateTime endDate;

                if (!DateTime.TryParse(searchByDate.StartingDate, out startDate))
                {
                    return(BadRequest("StartingDate tarih formatında değil"));
                }
                else if (!DateTime.TryParse(searchByDate.EndingDate, out endDate))
                {
                    return(BadRequest("EndingDate tarih formatında değil"));
                }

                if (startDate >= endDate)
                {
                    return(BadRequest("StartingDate, EndingDate tarihinden ileri bir tarih olamaz"));
                }
            }

            var sourceCount = _dbContext.Sources
                              .Count();

            if (sourceCount == 0)
            {
                return(NotFound());
            }

            return(Ok(sourceCount));
        }
        public ActionResult <IEnumerable <CountDTO> > NewsCount([FromQuery] SearchByDateDTO searchByDate)
        {
            var query = _dbContext.News.Where(x => x.NewsKeywords.Count > 0).AsQueryable();

            if (searchByDate.EndingDate != null && searchByDate.StartingDate != null)
            {
                DateTime startDate;
                DateTime endDate;

                if (!DateTime.TryParse(searchByDate.StartingDate, out startDate))
                {
                    return(BadRequest("StartingDate tarih formatında değil"));
                }
                else if (!DateTime.TryParse(searchByDate.EndingDate, out endDate))
                {
                    return(BadRequest("EndingDate tarih formatında değil"));
                }

                if (startDate >= endDate)
                {
                    return(BadRequest("StartingDate, EndingDate tarihinden ileri bir tarih olamaz"));
                }

                query = query.Where(c => c.NewsDate >= startDate && c.NewsDate < endDate);
            }

            var newsCount = query.Count();

            if (newsCount == 0)
            {
                return(NotFound());
            }

            return(Ok(new Response <int>(newsCount)));
        }
        public ActionResult <IEnumerable <GraphDTO> > NewsCountBySource([FromQuery] SearchByDateDTO searchByDate)
        {
            DateTime startDate;
            DateTime endDate;
            var      query = _dbContext.News.AsQueryable();

            if (searchByDate.StartingDate != null || searchByDate.StartingDate != null)
            {
                if (!DateTime.TryParse(searchByDate.StartingDate, out startDate))
                {
                    return(BadRequest("StartingDate tarih formatında değil"));
                }
                else if (!DateTime.TryParse(searchByDate.EndingDate, out endDate))
                {
                    return(BadRequest("EndingDate tarih formatında değil"));
                }

                if (startDate >= endDate)
                {
                    return(BadRequest("StartingDate, EndingDate tarihinden ileri bir tarih olamaz"));
                }

                query = query.Where(c => c.NewsDate >= startDate && c.NewsDate < endDate);
            }

            var getNewsCountBySource = query.Where(x => x.NewsKeywords.Count > 0)
                                       .GroupBy(x => new { x.SourceId })
                                       .Select(c => new GraphDTO
            {
                Id    = c.Key.SourceId.ToString(),
                Key   = _dbContext.Sources.Where(x => x.Id == c.Key.SourceId).Select(x => x.SourceName).FirstOrDefault(),
                Value = c.Count().ToString()
            })
                                       .OrderByDescending(x => x.Value)
                                       .Take(20)
                                       .ToList();

            if (getNewsCountBySource?.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(getNewsCountBySource));
        }
        public ActionResult <IEnumerable <GraphDTO> > KeywordCount([FromQuery] SearchByDateDTO searchByDate)
        {
            DateTime startDate;
            DateTime endDate;
            var      query = _dbContext.NewsKeywords.AsQueryable();

            if (searchByDate.StartingDate != null || searchByDate.StartingDate != null)
            {
                if (!DateTime.TryParse(searchByDate.StartingDate, out startDate))
                {
                    return(BadRequest("StartingDate tarih formatında değil"));
                }
                else if (!DateTime.TryParse(searchByDate.EndingDate, out endDate))
                {
                    return(BadRequest("EndingDate tarih formatında değil"));
                }

                if (startDate >= endDate)
                {
                    return(BadRequest("StartingDate, EndingDate tarihinden ileri bir tarih olamaz"));
                }

                query = query.Where(c => c.News.NewsDate >= startDate && c.News.NewsDate < endDate);
            }

            var topKeywordCounts = query
                                   .GroupBy(x => x.Keyword.KeywordValue)
                                   .Select(z => new GraphDTO
            {
                Id    = _dbContext.Keywords.FirstOrDefault(x => x.KeywordValue.Contains(z.Key)).Id.ToString(),
                Key   = z.Key,
                Value = z.Count().ToString()
            })
                                   .ToList();

            if (topKeywordCounts?.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(new Response <IEnumerable <GraphDTO> >(topKeywordCounts)));
        }
예제 #5
0
        public async Task <IActionResult> Get([FromQuery] PaginationQuery pagination, [FromQuery] SearchByDateDTO searchByDate)
        {
            if (searchByDate.EndingDate != null && searchByDate.StartingDate != null)
            {
                DateTime startDate;
                DateTime endDate;

                if (!DateTime.TryParse(searchByDate.StartingDate, out startDate))
                {
                    return(BadRequest("StartingDate tarih formatında değil"));
                }
                else if (!DateTime.TryParse(searchByDate.EndingDate, out endDate))
                {
                    return(BadRequest("EndingDate tarih formatında değil"));
                }

                if (startDate >= endDate)
                {
                    return(BadRequest("StartingDate, EndingDate tarihinden ileri bir tarih olamaz"));
                }
            }

            var paginationFilter = _mapper.Map <PaginationFilter>(pagination);
            var getallnews       = await _newsService.GetAllNews(paginationFilter);

            var getallnewsDto = _mapper.Map <IEnumerable <News>, IEnumerable <NewsDTO> >(getallnews);

            foreach (var item in getallnewsDto)
            {
                var source = await _sourceService.GetSourceById(item.SourceId);

                item.SourceName = source.SourceName;
            }

            if (pagination == null || pagination.PageNumber < 1 || pagination.PageSize < 1)
            {
                return(Ok(new PagedResponse <NewsDTO>(getallnewsDto)));
            }

            var paginationResponse = PaginationHelpers.CreatePaginationResponse(_uriService, paginationFilter, getallnewsDto);

            return(Ok(paginationResponse));
        }
예제 #6
0
        public async Task <IActionResult> GetFromRss([FromQuery] PaginationQuery pagination, [FromQuery] string searchTerm, [FromQuery] SearchByDateDTO searchByDate)
        {
            var link     = $"https://news.google.com/rss/search?q={searchTerm}&hl=tr&gl=TR&ceid=TR:tr";
            var newsList = new List <NewsDTO>();

            XmlDocument xml    = new XmlDocument();
            var         client = _httpClientFactory.CreateClient();

            client.BaseAddress = new Uri(link);
            string result = await client.GetStringAsync("");

            xml.LoadXml(result);
            XmlNodeList entries = xml.DocumentElement.GetElementsByTagName("item");

            if (entries.Count == 0)
            {
                return(NotFound());
            }

            foreach (XmlNode entry in entries)
            {
                var random      = new Random();
                var title       = entry["title"].InnerText;
                var index       = title.Split("-").Reverse().FirstOrDefault().Length;
                var cleanString = title.Remove(title.Length - index - 1, index + 1);
                var news        = new NewsDTO
                {
                    Id         = random.Next(1000, 3000),
                    NewsTitle  = cleanString,
                    NewsDate   = DateTime.Parse(entry["pubDate"].InnerText),
                    NewsUrl    = entry["link"].InnerText,
                    SourceName = entry["source"].InnerText
                };
                newsList.Add(news);
            }

            return(Ok(new Response <IEnumerable <NewsDTO> >(newsList.OrderByDescending(x => x.NewsDate))));
        }