private IQueryable <Quiz> ApplyOrdering(IQueryable <Quiz> result, QuizSearchModel queryParameters)
        {
            if (queryParameters.OrderBy == null)
            {
                result = result.OrderByDescending(q => q.CreatedOn);
            }

            switch (queryParameters.OrderBy)
            {
            case ResultOrder.ByDate:
                result = queryParameters.OrderDescending
                        ? result.OrderByDescending(q => q.CreatedOn)
                        : result.OrderBy(q => q.CreatedOn);
                break;

            case ResultOrder.ByNumberOfQuestions:
                result = queryParameters.OrderDescending
                        ? result.OrderByDescending(q => q.NumberOfQuestions).ThenByDescending(q => q.Questions.Count)
                        : result.OrderBy(q => q.NumberOfQuestions).ThenBy(q => q.Questions.Count);
                break;

            case ResultOrder.ByTimesTaken:
                result = queryParameters.OrderDescending
                        ? result.OrderByDescending(q => q.Solutions.Count)
                        : result.OrderBy(q => q.Solutions.Count);
                break;
            }

            return(result);
        }
Beispiel #2
0
        public ActionResult Search(QuizSearchModel searchModel, int page = 1)
        {
            var service = new QuizService();
            var model   = service.SearchQuiz(searchModel);

            var resultModel = new QuizListModel
            {
                SearchModel = searchModel,
                Results     = model
            };

            return(View("Index", resultModel));
        }
        public IQueryable <Quiz> SearchQuizzes(QuizSearchModel queryParameters)
        {
            if (queryParameters == null)
            {
                throw new ArgumentNullException(nameof(queryParameters), "No query parameters!");
            }

            var result = this.quizzes.All();

            result = this.ApplyFiltering(result, queryParameters);
            result = this.ApplyOrdering(result, queryParameters);

            return(result);
        }
Beispiel #4
0
        internal QuizListModel InitializeSearchModel(int parliamentId)
        {
            var searchModel = new QuizSearchModel
            {
                ParliamentId = parliamentId
            };
            var resultModel = SearchQuiz(searchModel);

            return(new QuizListModel
            {
                SearchModel = new QuizSearchModel(),
                Results = resultModel
            });
        }
Beispiel #5
0
        public ActionResult Query(QuizSearchModel query)
        {
            if (!this.ModelState.IsValid)
            {
                // Todo: Something more appropriate
                return(this.PartialView("_NoResultsPartial"));
            }

            var models = this.quizzes.SearchQuizzes(query)
                         .To <QuizBasicViewModel>()
                         .ToList();

            if (models.Count == 0)
            {
                return(this.PartialView("_NoResultsPartial"));
            }

            return(this.PartialView("_QueryResultPartial", models));
        }
Beispiel #6
0
        internal IPagedList <QuizSummaryModel> SearchQuiz(QuizSearchModel searchModel)
        {
            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var query = context.Quizes
                            .Where(x => 1 == 1);

                if (searchModel.ParliamentId != 0)
                {
                    query = query.Where(x => x.ParliamentId == searchModel.ParliamentId);
                }

                if (searchModel.QueryString != null)
                {
                    query = query.Where(x => x.Title.Contains(searchModel.QueryString) ||
                                        x.Description.Contains(searchModel.QueryString));
                }



                switch (searchModel.Sort ?? QuizSort.CreateTime)
                {
                case QuizSort.CreateTime:
                    query = query.OrderBy(x => x.TimeCreated);
                    break;

                case QuizSort.Title:
                    query = query.OrderBy(x => x.Title);
                    break;
                }

                var result = query.Select(x => new QuizSummaryModel
                {
                    Title             = x.Title,
                    Description       = x.Description,
                    ImageRelativePath = x.ImageRelativePath,
                    QuizId            = x.QuizId
                })
                             .ToPagedList(searchModel.page.HasValue ? searchModel.page.Value : 1, 16);

                return(result);
            }
        }
        private IQueryable <Quiz> ApplyFiltering(IQueryable <Quiz> result, QuizSearchModel queryParameters)
        {
            if (queryParameters.Category != null)
            {
                result = result.Where(q => q.Category.Name.Equals(queryParameters.Category, StringComparison.OrdinalIgnoreCase));
            }

            if (queryParameters.KeyPhrase != null)
            {
                string phrase = queryParameters.KeyPhrase.ToLower();

                result = result.Where(q => q.Title.ToLower().Contains(phrase) ||
                                      q.Description.ToLower().Contains(phrase));
            }

            if (queryParameters.FromDate != null)
            {
                result = result.Where(q => q.CreatedOn >= queryParameters.FromDate);
            }

            if (queryParameters.ToDate != null)
            {
                result = result.Where(q => q.CreatedOn <= queryParameters.ToDate);
            }

            if (queryParameters.MinQuestions != null)
            {
                result = result.Where(q => q.Questions.Count >= queryParameters.MinQuestions);
            }

            if (queryParameters.MaxQuestions != null)
            {
                result = result.Where(q => q.Questions.Count <= queryParameters.MaxQuestions);
            }

            return(result);
        }
Beispiel #8
0
        public ActionResult Search(QuizSearchModel query)
        {
            var results = new List <QuizRankedBySolutions>();
            var range   = new MonthlyRange();

            if (query != null && (query.Category != null || query.KeyPhrase != null))
            {
                results = this.Ranking
                          .OrderBySolutions(this.quizzes.SearchQuizzes(query), range)
                          .To <QuizRankedBySolutions>()
                          .ToList();
            }

            var maxSolutions = this.GetMaxSolutions(range);

            var page = new SearchPageViewModel(maxSolutions, range)
            {
                QuizSearchModel = query,
                Quizzes         = results,
                Categories      = this.categories.GetCategoryOptions()
            };

            return(this.View(page));
        }