public void Handle(DomainModelCreatedEvent <IdentificationNew> domainEvent)
        {
            Check.RequireNotNull(domainEvent, "domainEvent");

            var sighting = domainEvent.Sender as Sighting;

            dynamic activity = MakeActivity(
                domainEvent,
                "identificationadded",
                domainEvent.DomainModel.CreatedOn,
                string.Format("{0} identified a sighting", domainEvent.User.GetName()),
                sighting.Groups.Select(x => x.Group),
                sighting.Id,
                domainEvent.DomainModel.Id);

            var projects = _documentSession.Load <Project>(sighting.Groups.Where(x => x.Group.GroupType == "project").Select(x => x.Group.Id));

            activity.IdentificationAdded = new
            {
                Sighting       = _sightingViewFactory.Make(sighting, domainEvent.User, projects, domainEvent.User),
                Identification = _identificationViewFactory.Make(sighting, domainEvent.DomainModel, domainEvent.User, domainEvent.User)
            };

            _documentSession.Store(activity);
            _documentSession.SaveChanges();
            _backChannelService.SendActivityToGroupChannel(activity);
        }
Exemplo n.º 2
0
        private object Execute(IRavenQueryable <All_Activities.Result> query, ActivitiesQueryInput activityInput, PagingInput pagingInput)
        {
            var profiler = MiniProfiler.Current;

            if (activityInput.NewerThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime > activityInput.NewerThan.Value);
            }
            else if (activityInput.OlderThan.HasValue)
            {
                query = query.Where(x => x.CreatedDateTime < activityInput.OlderThan.Value);
            }

            RavenQueryStatistics stats;

            List <dynamic> activities;

            using (profiler.Step("Get top 10 activity list RavenDB query"))
            {
                activities = query
                             .Statistics(out stats)
                             .OrderByDescending(x => x.CreatedDateTime)
                             .Skip(pagingInput.GetSkipIndex())
                             .Take(pagingInput.GetPageSize())
                             .As <dynamic>()
                             .ToList();
            }


            var contributionIds = activities
                                  .SelectMany(x => new[] { x.ContributionId, x.SubContributionId })
                                  .Where(x => !string.IsNullOrWhiteSpace(x))
                                  .Cast <string>()
                                  .Distinct()
                                  .ToList();

            List <All_Contributions.Result> contributions;

            using (profiler.Step("Get the 10 contributions RavenDB query"))
            {
                contributions = _documentSession
                                .Query <All_Contributions.Result, All_Contributions>()
                                .AsProjection <All_Contributions.Result>()
                                .Include(x => x.Contribution.User.Id)
                                .Where(x => x.ParentContributionId.In(contributionIds))
                                .ToList();
            }

            User authenticatedUser = null;

            using (profiler.Step("Get authenticated user RavenDB query"))
            {
                if (_userContext.IsUserAuthenticated())
                {
                    authenticatedUser = _documentSession.Load <User>(_userContext.GetAuthenticatedUserId());
                }
            }

            using (profiler.Step("Build activity list view model"))
            {
                return(new PagedList <object>()
                {
                    Page = pagingInput.Page,
                    PageSize = pagingInput.PageSize,
                    TotalResultCount = stats.TotalResults,
                    PagedListItems = activities.Select(x =>
                    {
                        dynamic activity = new ExpandoObject();

                        activity.Id = x.Id;
                        activity.Type = x.Type;
                        activity.CreatedDateTime = x.CreatedDateTime;
                        activity.CreatedDateTimeOrder = x.CreatedDateTimeOrder;
                        activity.Description = x.Description;

                        var user = _documentSession.Load <User>((string)x.User.Id);
                        activity.User = _userViewFactory.Make(user, null);
                        activity.Groups = x.Groups;
                        activity.ContributionId = x.ContributionId;
                        activity.SubContributionId = x.SubContributionId;

                        if (x.Type == "sightingadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && (y.ParentContributionType == "observation" || y.ParentContributionType == "record") && y.SubContributionType == null);

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("sighting");
                            }
                            else
                            {
                                activity.ObservationAdded = new
                                {
                                    Observation = _sightingViewFactory.Make(result.Contribution as Sighting, result.User, result.Groups, authenticatedUser)
                                };
                            }

                            //if (x.RecordAdded != null)
                            //{
                            //    activity.RecordAdded = new
                            //        {
                            //            Record = sighting
                            //        };
                            //}
                        }

                        if (x.Type == "identificationadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "identification");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("identification");
                            }
                            else
                            {
                                activity.IdentificationAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    Identification = _identificationViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as IdentificationNew, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "sightingnoteadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.SubContributionId == x.SubContributionId && y.SubContributionType == "note");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("note");
                            }
                            else
                            {
                                activity.SightingNoteAdded = new
                                {
                                    Sighting = _sightingViewFactory.Make(result.ParentContribution as Sighting, result.User, result.Groups, authenticatedUser),
                                    SightingNote = _sightingNoteViewFactory.Make(result.ParentContribution as Sighting, result.Contribution as SightingNote, result.User, authenticatedUser)
                                };
                            }
                        }

                        if (x.Type == "postadded")
                        {
                            var result = contributions.FirstOrDefault(y => y.ParentContributionId == x.ContributionId && y.ParentContributionType == "post");

                            if (result == null)
                            {
                                activity.DeletedActivityItem = MakeDeletedActivityItem("news item");
                            }
                            else
                            {
                                activity.PostAdded = new
                                {
                                    Post = _postViewFactory.Make(result.Contribution as Post, result.User, result.Groups.First(), authenticatedUser)
                                };
                            }
                        }

                        return activity;
                    })
                });
            }
        }
Exemplo n.º 3
0
        public object BuildSighting(string id)
        {
            var authenticatedUser = _userContext.IsUserAuthenticated() ? _documentSession.Load <User>(_userContext.GetAuthenticatedUserId()) : null;

            var results = _documentSession
                          .Query <All_Contributions.Result, All_Contributions>()
                          .AsProjection <All_Contributions.Result>()
                          .Where(x => x.ParentContributionId == id)
                          .ToList();

            var sightingResult = results.First(x => (x.ParentContributionType == "observation" || x.ParentContributionType == "record") && x.SubContributionType == null);

            var sighting = sightingResult.Contribution as Sighting;
            var projects = sightingResult.Groups;
            var user     = sightingResult.User;

            dynamic viewModel = _sightingViewFactory.Make(sighting, user, projects, authenticatedUser);

            viewModel.Identifications = results.Where(x => x.SubContributionType == "identification").Select(x => _identificationViewFactory.Make(sighting, x.Contribution as IdentificationNew, x.User, authenticatedUser));
            viewModel.Notes           = results.Where(x => x.SubContributionType == "note").Select(x => _sightingNoteViewFactory.Make(sighting, x.Contribution as SightingNote, x.User, authenticatedUser));

            return(viewModel);
        }