public object BuildGroupPostList(string groupId, PostsQueryInput postsQueryInput)
        {
            Check.RequireNotNullOrWhitespace(groupId, "groupId");
            Check.RequireNotNull(postsQueryInput, "postsQueryInput");

            return(ExecuteQuery(postsQueryInput, new[] { groupId }));
        }
        public object BuildHomePostList(string userId, PostsQueryInput postsQueryInput)
        {
            Check.RequireNotNullOrWhitespace(userId, "userId");
            Check.RequireNotNull(postsQueryInput, "postsQueryInput");

            var groupIds = _documentSession
                           .Load <User>(userId)
                           .Memberships.Select(x => x.Group.Id);

            return(ExecuteQuery(postsQueryInput, groupIds));
        }
Beispiel #3
0
        public ActionResult Posts(PostsQueryInput queryInput)
        {
            queryInput.PageSize = 10;

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

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

            dynamic viewModel = new ExpandoObject();

            viewModel.User  = _userViewModelQuery.BuildUser(_userContext.GetAuthenticatedUserId());
            viewModel.Posts = _postViewModelQuery.BuildHomePostList(_userContext.GetAuthenticatedUserId(), queryInput);
            viewModel.Query = new
            {
                Id = "home", // We set the id to home, so that the mustache sightings list creates correct sorting URL
                queryInput.Page,
                queryInput.PageSize,
                queryInput.Sort,
                queryInput.Query,
                queryInput.Field
            };
            viewModel.ShowPosts       = true;
            viewModel.FieldSelectList = new[]
            {
                new
                {
                    Text     = "Title",
                    Value    = "title",
                    Selected = queryInput.Field.ToLower() == "title"
                },
                new
                {
                    Text     = "Body",
                    Value    = "descriptions",
                    Selected = queryInput.Field.ToLower() == "descriptions"
                }
            };

            return(RestfulResult(
                       viewModel,
                       "home",
                       "posts"));
        }
        public ActionResult Posts(string id, PostsQueryInput queryInput)
        {
            string organisationId = VerbosifyId <Organisation>(id);

            if (!_permissionManager.DoesExist <Organisation>(organisationId))
            {
                return(HttpNotFound());
            }

            queryInput.PageSize = 10;

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

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

            var organisationResult = _documentSession
                                     .Query <All_Groups.Result, All_Groups>()
                                     .AsProjection <All_Groups.Result>()
                                     .First(x => x.GroupId == organisationId);

            dynamic viewModel = new ExpandoObject();

            viewModel.Organisation         = _organisationViewModelQuery.BuildOrganisation(organisationId);
            viewModel.Posts                = _postViewModelQuery.BuildGroupPostList(organisationId, queryInput);
            viewModel.UserCountDescription = "Member" + (organisationResult.UserCount == 1 ? string.Empty : "s");
            viewModel.PostCountDescription = "Post" + (organisationResult.PostCount == 1 ? string.Empty : "s");
            viewModel.Query                = new
            {
                Id = organisationId,
                queryInput.Page,
                queryInput.PageSize,
                queryInput.Sort,
                queryInput.Query,
                queryInput.Field
            };
            viewModel.ShowPosts       = true;
            viewModel.FieldSelectList = new[]
            {
                new
                {
                    Text     = "Title",
                    Value    = "title",
                    Selected = queryInput.Field.ToLower() == "title"
                },
                new
                {
                    Text     = "Body",
                    Value    = "body",
                    Selected = queryInput.Field.ToLower() == "body"
                }
            };

            return(RestfulResult(
                       viewModel,
                       "organisations",
                       "posts"));
        }
        private object ExecuteQuery(PostsQueryInput postsQueryInput, IEnumerable <string> groupIds)
        {
            RavenQueryStatistics stats;
            User authenticatedUser = null;

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

            var query = _documentSession
                        .Advanced
                        .LuceneQuery <All_Contributions.Result, All_Contributions>()
                        .Statistics(out stats)
                        .SelectFields <All_Contributions.Result>("GroupIds", "CreatedDateTime", "ParentContributionId", "SubContributionId", "ParentContributionType", "SubContributionType", "UserId", "Observation", "Record", "Post", "User")
                        .WhereIn("GroupIds", groupIds)
                        .AndAlso()
                        .WhereIn("ParentContributionType", new[] { "post" })
                        .AndAlso()
                        .WhereEquals("SubContributionType", null);

            if (!string.IsNullOrWhiteSpace(postsQueryInput.Query))
            {
                var field = "PostAllFields";

                if (postsQueryInput.Field.ToLower() == "title")
                {
                    field = "PostTitle";
                }
                if (postsQueryInput.Field.ToLower() == "body")
                {
                    field = "PostMessage";
                }

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

            switch (postsQueryInput.Sort.ToLower())
            {
            default:
            case "newest":
                query = query.AddOrder(x => x.CreatedDateTime, true);
                break;

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

            return(query
                   .Skip(postsQueryInput.GetSkipIndex())
                   .Take(postsQueryInput.GetPageSize())
                   .Select(x => _postViewFactory.Make(x.Contribution as Post, x.User, x.Groups.First(), authenticatedUser))
                   .ToList()
                   .ToPagedList(
                       postsQueryInput.GetPage(),
                       postsQueryInput.GetPageSize(),
                       stats.TotalResults
                       ));
        }