private async Task <IReadOnlyCollection <AuditRecord <Guid> > > CollectDiscussionHistory(TopicHistoryQuery request, CancellationToken cancellationToken)
        {
            var command = new DiscussionHistoryQuery(request.Principal)
            {
                TopicId = request.Id
            };

            return(await _mediator.Send(command, cancellationToken));
        }
예제 #2
0
        protected override async Task <IReadOnlyCollection <AuditRecord <Guid> > > Process(DiscussionHistoryQuery request, CancellationToken cancellationToken)
        {
            var parameters = new List <object>();

            var sql = new StringBuilder();

            sql.AppendLine("SELECT *");
            sql.AppendLine("FROM [IQ].[Discussion]");
            sql.AppendLine("FOR SYSTEM_TIME ALL");

            if (request.Id.HasValue)
            {
                sql.AppendLine("WHERE [Id] = {0}");
                parameters.Add(request.Id.Value);
            }
            else if (request.TopicId.HasValue)
            {
                sql.AppendLine("WHERE [TopicId] = {0}");
                parameters.Add(request.TopicId.Value);
            }

            var entities = await DataContext.Discussions
                           .FromSqlRaw(sql.ToString(), parameters.ToArray())
                           .AsNoTracking()
                           .OrderBy(p => p.PeriodEnd)
                           .ProjectTo <DiscussionReadModel>(Mapper.ConfigurationProvider)
                           .ToListAsync(cancellationToken);

            var historyList = new List <AuditRecord <Guid> >();

            // group changes by id
            foreach (var group in entities.GroupBy(p => p.Id))
            {
                var groupList = group
                                .OrderBy(p => p.PeriodEnd)
                                .ToList();

                var changeList = _changeCollector.CollectChanges(groupList, nameof(Discussion), d => d.DisplayName);
                historyList.AddRange(changeList);
            }

            return(historyList.ToList());
        }