private IQueryable <Article> RestrictDate(IQueryable <Article> query, ArticleApiOptions options) { bool hasStartDate = options.DateStart.HasValue; bool hasEndDate = options.DateEnd.HasValue; if (!hasStartDate && !hasEndDate) { return(query); } else if (hasStartDate && hasEndDate && (options.DateStart.Value > options.DateEnd.Value)) { return(query); } else { string propertyName = options.ArticleType.Value == ArticleType.Highlighted ? "HighlightedAt" : options.ArticleType.Value == ArticleType.Published ? "PublishedAt" : "CreatedAt"; if (hasStartDate) { query = query.Where(a => EF.Property <DateTime>(a, propertyName) >= options.DateStart.Value); } if (hasEndDate) { query = query.Where(a => EF.Property <DateTime>(a, propertyName) <= options.DateEnd.Value); } } return(query); }
private IQueryable <Article> RestrictSearch(IQueryable <Article> query, ArticleApiOptions options) { string filteredSearchString = options.SearchString != null?options.SearchString.Trim().ToLower() : ""; if (filteredSearchString.Length > 0) { bool isName = options.SearchType.Value.HasFlag(ArticleSearchType.Name); bool isLead = options.SearchType.Value.HasFlag(ArticleSearchType.Lead); bool isContent = options.SearchType.Value.HasFlag(ArticleSearchType.Content); if (isName || isLead || isContent) { query = query.Where(a => (isName && a.Name.ToLower().Contains(filteredSearchString)) || (isLead && a.Lead.ToLower().Contains(filteredSearchString)) || (isContent && a.Content.ToLower().Contains(filteredSearchString))); } } return(query); }
public async Task <IActionResult> GetArticles(int page, [FromQuery] SearchParameters parameters) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(Unauthorized()); } if (parameters == null) { parameters = new SearchParameters(); } if (page == 0) { page = 1; } ArticleSearchType searchType = 0; if (parameters.SearchTitle) { searchType = searchType | ArticleSearchType.Name; } if (parameters.SearchLead) { searchType = searchType | ArticleSearchType.Lead; } if (parameters.SearchContent) { searchType = searchType | ArticleSearchType.Content; } ArticleApiOptions options = new ArticleApiOptions { Offset = (page - 1) * LIST_LIMIT, Limit = LIST_LIMIT, ArticleType = ArticleType.All, IncludeAuthor = true, SearchType = searchType, SearchString = parameters.Search, DateStart = parameters.DateStart, DateEnd = parameters.DateEnd }; if (options.DateEnd.HasValue) { options.DateEnd = options.DateEnd.Value.AddDays(1).Date; //Add one day to include DateEnd in search } if (options.DateStart.HasValue) { options.DateStart = options.DateStart.Value.Date; //Set DateTime to midnight; } //_newsService.GetArticles(options); IQueryable <Article> query = _context.Articles; query = RestrictArticleType(query, options.ArticleType.Value); query = RestrictDate(query, options); query = RestrictSearch(query, options); if (options.IncludeAuthor.HasValue && options.IncludeAuthor.Value) { query = query.Include(a => a.Author); } query = query.Where(a => a.Author == user); int count = query.Count(); query = query.OrderByDescending(a => a.CreatedAt); if (options.Offset.HasValue && options.Offset.Value > 0) { query = query.Skip(options.Offset.Value); } if (options.Limit.HasValue && options.Limit.Value > 0) { query = query.Take(options.Limit.Value); } int pageCount = options.Limit.Value > 0 ? ((count - 1) / options.Limit.Value) + 1 : 1; List <ArticleListElemDTO> list = query.Select <Article, ArticleListElemDTO>(x => new ArticleListElemDTO { Id = x.Id, Name = x.Name, Lead = x.Lead, ImageCount = x.Images.Count(), Author = x.Author.Name, PublishedAt = x.PublishedAt, IsPublished = x.IsPublished, IsHighlighted = x.IsHighlighted, HighlightedAt = x.HighlightedAt, CreatedAt = x.CreatedAt } ).ToList(); ArticleListDTO result = new ArticleListDTO { Limit = options.Limit.Value, Page = page, Count = count, PageCount = pageCount, Articles = list }; return(Ok(result)); }