Ejemplo n.º 1
0
        async Task <TEntity> EntityUpdating(TEntity entity)
        {
            // Get previous history points
            var previousHistories = await _entityHistoryStore.QueryAsync()
                                    .Take(1)
                                    .Select <EntityHistoryQueryParams>(q =>
            {
                q.EntityId.Equals(entity.Id);
                q.EntityReplyId.Equals(0);
            })
                                    .OrderBy("CreatedDate", OrderBy.Desc)
                                    .ToList();

            // Get the most recently added history point
            EntityHistory previousHistory = null;

            if (previousHistories?.Data != null)
            {
                previousHistory = previousHistories.Data[0];
            }

            // If we have previous history we don't need to add a starting point
            if (previousHistory != null)
            {
                return(entity);
            }

            // Get existing entity before any changes
            var existingEntity = await _entityStore.GetByIdAsync(entity.Id);

            // We need an existing entity
            if (existingEntity == null)
            {
                return(entity);
            }

            // If we don't have any existing history points add our
            // existing entity (before updates) as the starting / original history point
            await _entityHistoryManager.CreateAsync(new EntityHistory()
            {
                EntityId      = existingEntity.Id,
                Message       = existingEntity.Message,
                Html          = existingEntity.Html,
                CreatedUserId = existingEntity.EditedUserId > 0
                    ? existingEntity.EditedUserId
                    : existingEntity.CreatedUserId,
                CreatedDate = existingEntity.EditedDate ?? existingEntity.CreatedDate
            });

            return(entity);
        }
Ejemplo n.º 2
0
 async Task <IPagedResults <EntityHistory> > GetEntityHistory(
     int page,
     int pageSize,
     int entityId,
     int entityReplyId,
     string sortBy,
     OrderBy sortOrder)
 {
     return(await _entityHistoryStore.QueryAsync()
            .Take(page, pageSize)
            .Select <EntityHistoryQueryParams>(q =>
     {
         q.EntityId.Equals(entityId);
         q.EntityReplyId.Equals(entityReplyId);
     })
            .OrderBy(sortBy, sortOrder)
            .ToList());
 }
Ejemplo n.º 3
0
        // --------------
        // Index
        // --------------

        public async Task <IActionResult> Index(int id)
        {
            // Get history point
            var history = await _entityHistoryStore.GetByIdAsync(id);

            if (history == null)
            {
                return(NotFound());
            }

            // Get entity for history point
            var entity = await _entityStore.GetByIdAsync(history.EntityId);

            if (entity == null)
            {
                return(NotFound());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                            entity.CategoryId, history.EntityReplyId > 0
                    ? Permissions.viewReplyHistory
                    : Permissions.ViewEntityHistory))
            {
                return(Unauthorized());
            }

            // Get previous history
            var previousHistory = await _entityHistoryStore.QueryAsync()
                                  .Take(1, false)
                                  .Select <EntityHistoryQueryParams>(q =>
            {
                q.Id.LessThan(history.Id);
                q.EntityId.Equals(history.EntityId);
                q.EntityReplyId.Equals(history.EntityReplyId);
            })
                                  .OrderBy("Id", OrderBy.Desc)
                                  .ToList();

            // Get newest / most recent history entry
            var latestHistory = await _entityHistoryStore.QueryAsync()
                                .Take(1, false)
                                .Select <EntityHistoryQueryParams>(q =>
            {
                q.EntityId.Equals(history.EntityId);
                q.EntityReplyId.Equals(history.EntityReplyId);
            })
                                .OrderBy("Id", OrderBy.Desc)
                                .ToList();

            // Compare previous to current
            var html = history.Html;

            if (previousHistory?.Data != null)
            {
                html = PrepareDifAsync(previousHistory.Data[0].Html, history.Html);
            }

            // Build model
            var viewModel = new HistoryIndexViewModel()
            {
                History       = history,
                LatestHistory = latestHistory?.Data[0],
                Html          = html
            };

            return(View(viewModel));
        }