示例#1
0
        async Task <TEntityReply> EntityReplyCreated(TEntityReply reply)
        {
            // No need to update category for private entities
            if (reply.IsHidden)
            {
                return(reply);
            }

            // No need to update category for soft deleted replies
            if (reply.IsDeleted)
            {
                return(reply);
            }

            // No need to update category for replies flagged as spam
            if (reply.IsSpam)
            {
                return(reply);
            }

            // Get the entity we are replying to
            var entity = await _entityStore.GetByIdAsync(reply.EntityId);

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

            // Ensure we have a categoryId for the entity
            if (entity.CategoryId <= 0)
            {
                return(reply);
            }

            // Ensure we found the category
            var channel = await _channelStore.GetByIdAsync(entity.CategoryId);

            if (channel == null)
            {
                return(reply);
            }

            // Update channel details
            await _categoryDetailsUpdater.UpdateAsync(channel.Id);

            // return
            return(reply);
        }
示例#2
0
        async Task <TEntity> EntityCreated(TEntity entity)
        {
            // Ensure we have a category for the entity
            if (entity.CategoryId <= 0)
            {
                return(entity);
            }

            // Ensure we found the category
            var category = await _categoryStore.GetByIdAsync(entity.CategoryId);

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

            // Update category details
            await _categoryDetailsUpdater.UpdateAsync(category.Id);

            // return
            return(entity);
        }
示例#3
0
        public override async Task <IViewProviderResult> BuildUpdateAsync(Topic topic, IViewProviderContext context)
        {
            // Ensure entity exists before attempting to update
            var entity = await _entityStore.GetByIdAsync(topic.Id);

            if (entity == null)
            {
                return(await BuildIndexAsync(topic, context));
            }

            // Validate model
            if (await ValidateModelAsync(topic, context.Updater))
            {
                // Get selected categories
                var categoriesToAdd = GetCategoriesToAdd();
                if (categoriesToAdd != null)
                {
                    // Build categories to remove
                    var categoriesToRemove = new List <int>();
                    foreach (var categoryId in await GetCategoryIdsByEntityIdAsync(topic))
                    {
                        if (!categoriesToAdd.Contains(categoryId))
                        {
                            categoriesToRemove.Add(categoryId);
                        }
                    }

                    // Remove categories
                    foreach (var categoryId in categoriesToRemove)
                    {
                        var entityCategory = await _entityCategoryStore.GetByEntityIdAndCategoryIdAsync(topic.Id, categoryId);

                        if (entityCategory != null)
                        {
                            await _entityCategoryManager.DeleteAsync(entityCategory);
                        }
                    }

                    // Get current user
                    var user = await _contextFacade.GetAuthenticatedUserAsync();

                    // Add new entity category relationships
                    foreach (var categoryId in categoriesToAdd)
                    {
                        // Ensure relationship does not already exist
                        var entityCategory = await _entityCategoryStore.GetByEntityIdAndCategoryIdAsync(topic.Id, categoryId);

                        if (entityCategory == null)
                        {
                            // Add relationship
                            await _entityCategoryManager.CreateAsync(new EntityCategory()
                            {
                                EntityId       = topic.Id,
                                CategoryId     = categoryId,
                                CreatedUserId  = user?.Id ?? 0,
                                ModifiedUserId = user?.Id ?? 0,
                            });
                        }
                    }

                    //// Update entity with first found category
                    //foreach (var id in categoriesToAdd)
                    //{
                    //    topic.CategoryId = id;
                    //    await _entityStore.UpdateAsync(topic);
                    //    break;
                    //}

                    // Update added category meta data
                    foreach (var id in categoriesToAdd)
                    {
                        await _categoryDetailsUpdater.UpdateAsync(id);
                    }

                    // Update removed category meta data
                    foreach (var id in categoriesToRemove)
                    {
                        await _categoryDetailsUpdater.UpdateAsync(id);
                    }
                }
            }

            return(await BuildEditAsync(topic, context));
        }