Commit 59644c18 authored by lap nguyen's avatar lap nguyen

optimize query call

parent e273287d
......@@ -34,7 +34,7 @@ namespace News_site.Controllers
{
try
{
List<Newspaper> newspapers = newsProvider.GetAll();
List<ExtendNewspaper> newspapers = newsProvider.GetAll();
if (newspapers == null)
{
......@@ -47,16 +47,7 @@ namespace News_site.Controllers
return Ok(new {
success = true,
data = new {
collection = result.Select(news => new
{
news.Id,
news.Image,
news.Title,
news.Categorylink,
news.CategorylinkNavigation,
news.Description,
news.CreatedAt
}).ToList(),
collection = result.ToList(),
total = result.Count(),
pagesize = sieveModel.PageSize,
pageIndex = sieveModel.Page
......
......@@ -66,8 +66,17 @@ namespace News_site.Provider
string checkCategoryLinkElem = Regex.Match(newsData, @"<div class=""detail-cate(.*?)</a>", RegexOptions.Singleline).Value;
string checkCategoryLink = Regex.Match(checkCategoryLinkElem, @"href=""(.*?)""", RegexOptions.Singleline).Value.Replace("href=\"", "").Replace("\"", "");
bool isNewspaperCrawled = await db.Newspapers.AnyAsync(news => news.Title.Equals(newsTitle));
bool isCategoryExisted = await db.Categories.AnyAsync(category => category.Link.Equals(checkCategoryLink));
var allNews = await db.Newspapers.Select(news => new
{
news.Title
}).AsNoTracking().ToListAsync();
var allCagegories = await db.Categories.Select(category => new
{
category.Link
}).AsNoTracking().ToListAsync();
bool isNewspaperCrawled = allNews.Any(news => news.Title.Equals(newsTitle));
bool isCategoryExisted = allCagegories.Any(category => category.Link.Equals(checkCategoryLink));
if (!isNewspaperCrawled && isCategoryExisted)
{
......
......@@ -10,9 +10,18 @@ namespace News_site.Provider
{
public class NewsProvider : BaseProvider
{
public List<Newspaper> GetAll()
public List<ExtendNewspaper> GetAll()
{
var news = db.Newspapers.Include(news => news.CategorylinkNavigation).ToList();
var news = db.Newspapers.Include(news => news.CategorylinkNavigation).Select(news => new ExtendNewspaper
{
Id = news.Id,
Image = news.Image,
Title = news.Title,
Categorylink = news.Categorylink,
CategorylinkNavigation = news.CategorylinkNavigation,
Description = news.Description,
CreatedAt = news.CreatedAt
}).ToList();
return news;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment