コード例 #1
0
        public object BuildProjectList(ProjectsQueryInput projectsQueryInput)
        {
            Check.RequireNotNull(projectsQueryInput, "projectsQueryInput");

            return(ExecuteQuery(projectsQueryInput));
        }
コード例 #2
0
        private object ExecuteQuery(ProjectsQueryInput projectsQueryInput)
        {
            RavenQueryStatistics stats;
            User authenticatedUser = null;

            if (_userContext.IsUserAuthenticated())
            {
                authenticatedUser = _documentSession.Load <User>(_userContext.GetAuthenticatedUserId());
            }

            var query = _documentSession
                        .Advanced
                        .LuceneQuery <All_Groups.Result, All_Groups>()
                        .Statistics(out stats)
                        .SelectFields <All_Groups.Result>("GroupType", "GroupId", "CreatedDateTime", "UserCount", "SightingCount", "PostCount", "VoteCount", "LatestObservationIds", "LatestObservations", "SortName")
                        .WhereEquals("GroupType", "project");

            if (!string.IsNullOrWhiteSpace(projectsQueryInput.Category))
            {
                query = query
                        .AndAlso()
                        .WhereIn("Categories", new [] { projectsQueryInput.Category });
            }

            if (!string.IsNullOrWhiteSpace(projectsQueryInput.Query))
            {
                var field = "AllFields";

                if (projectsQueryInput.Field.ToLower() == "name")
                {
                    field = "Name";
                }
                if (projectsQueryInput.Field.ToLower() == "description")
                {
                    field = "Description";
                }

                query = query
                        .AndAlso()
                        .Search(field, projectsQueryInput.Query);
            }

            switch (projectsQueryInput.Sort.ToLower())
            {
            case "a-z":
                query = query.AddOrder(x => x.SortName, false);
                break;

            case "z-a":
                query = query.AddOrder(x => x.SortName, true);
                break;

            case "newest":
                query = query.AddOrder(x => x.CreatedDateTime, true);
                break;

            case "oldest":
                query = query.AddOrder(x => x.CreatedDateTime, false);
                break;

            default:
            case "popular":
                query = query.AddOrder(x => x.UserCount, true);
                break;
            }

            return(query
                   .Skip(projectsQueryInput.GetSkipIndex())
                   .Take(projectsQueryInput.GetPageSize())
                   .ToList()
                   .Select(x => _groupViewFactory.Make(x.Group, authenticatedUser, true, x.SightingCount, x.UserCount, x.PostCount, x.LatestObservations != null ? x.LatestObservations.Take(4) : null))
                   .ToPagedList(
                       projectsQueryInput.GetPage(),
                       projectsQueryInput.GetPageSize(),
                       stats.TotalResults
                       ));
        }
コード例 #3
0
        public ActionResult List(ProjectsQueryInput queryInput)
        {
            queryInput.PageSize = 15;

            if (string.IsNullOrWhiteSpace(queryInput.Sort) ||
                (queryInput.Sort.ToLower() != "popular" &&
                 queryInput.Sort.ToLower() != "newest" &&
                 queryInput.Sort.ToLower() != "oldest" &&
                 queryInput.Sort.ToLower() != "a-z" &&
                 queryInput.Sort.ToLower() != "z-a"))
            {
                queryInput.Sort = "popular";
            }

            queryInput.Category = queryInput.Category ?? string.Empty;
            if (!string.IsNullOrWhiteSpace(queryInput.Category) && !Categories.IsValidCategory(queryInput.Category))
            {
                queryInput.Category = string.Empty;
            }

            queryInput.Query = queryInput.Query ?? string.Empty;
            queryInput.Field = queryInput.Field ?? string.Empty;

            dynamic viewModel = new ExpandoObject();

            viewModel.Projects           = _projectViewModelQuery.BuildProjectList(queryInput);
            viewModel.CategorySelectList = Categories.GetSelectList(queryInput.Category);
            viewModel.Query = new
            {
                queryInput.Page,
                queryInput.PageSize,
                queryInput.Sort,
                queryInput.Category,
                queryInput.Query,
                queryInput.Field
            };
            viewModel.FieldSelectList = new[]
            {
                new
                {
                    Text     = "Name",
                    Value    = "name",
                    Selected = queryInput.Field.ToLower() == "name"
                },
                new
                {
                    Text     = "Description",
                    Value    = "description",
                    Selected = queryInput.Field.ToLower() == "description"
                }
            };

            if (_userContext.IsUserAuthenticated())
            {
                var user = _documentSession
                           .Query <All_Users.Result, All_Users>()
                           .AsProjection <All_Users.Result>()
                           .Where(x => x.UserId == _userContext.GetAuthenticatedUserId())
                           .Single();

                viewModel.ShowProjectExploreWelcome = user.User.CallsToAction.Contains("project-explore-welcome");
            }

            return(RestfulResult(
                       viewModel,
                       "projects",
                       "list"));
        }