public async Task <QuestionSearchResult> Search(QuestionSearchParams request) { var query = dbcontext.Questions .Include(q => q.User) .Include(q => q.Topic) .Where(q => EF.Functions.Like(q.Title, $"%{request.Anywhere}%") || EF.Functions.Like(q.Content, $"%{request.Anywhere}%")); if (!String.IsNullOrEmpty(request.Title)) { query = query.Where(q => EF.Functions.Like(q.Title, $"%{request.Title}%")); } if (!String.IsNullOrEmpty(request.Content)) { query = query.Where(q => EF.Functions.Like(q.Content, $"%{request.Content}%")); } if (request.TopicId != null) { query = query.Where(q => q.Topic.Id == request.TopicId); } if (!string.IsNullOrEmpty(request.Username)) { query = query.Where(q => q.User.UserName == request.Username); } if (!request.IncludeHidden) { query = query.Where(q => q.Type != QuestionType.HiddenByModerator); } if (request.OnlyHidden) { query = query.Where(q => q.Type == QuestionType.HiddenByModerator); } var count = await query.CountAsync(); query = query .OrderByDescending(q => q.LastUpdated) .Skip((request.Page - 1) * request.CountPerPage) .Take(request.CountPerPage); var list = await query.Select(q => DbMapper.MapDbQuestion(q)).ToListAsync(); return(new QuestionSearchResult() { Page = request.Page, PageSize = request.CountPerPage, PageCount = (int)Math.Ceiling(((float)count) / request.CountPerPage), Count = count, Questions = list, }); }
public async Task <QuestionSearchResult> Search(QuestionSearchParams request, string username, string role) { if (username != null) { request.Username = username; request.IncludeHidden = true; } else { request.OnlyHidden = false; } if (UserService.AuthenticateModerator(role)) { request.IncludeHidden = true; } else { request.OnlyHidden = false; } return(await questionRepo.Search(request)); }