Ejemplo n.º 1
0
        public async Task DeleteAsync(DeleteForum command)
        {
            var forum = await _dbContext.Forums
                        .FirstOrDefaultAsync(x =>
                                             x.Category.SiteId == command.SiteId &&
                                             x.Id == command.Id &&
                                             x.Status != ForumStatusType.Deleted);

            if (forum == null)
            {
                throw new DataException($"Forum with Id {command.Id} not found.");
            }

            forum.Delete();
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Deleted,
                                            typeof(Forum),
                                            forum.Id));

            await ReorderForumsInCategory(forum.CategoryId, command.Id, command.SiteId, command.UserId);

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(forum.Id));
            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Ejemplo n.º 2
0
        public async Task UpdateAsync(UpdateCategory command)
        {
            await _updateValidator.ValidateCommandAsync(command);

            var category = await _dbContext.Categories
                           .FirstOrDefaultAsync(x =>
                                                x.SiteId == command.SiteId &&
                                                x.Id == command.Id &&
                                                x.Status != StatusType.Deleted);

            if (category == null)
            {
                throw new DataException($"Category with Id {command.Id} not found.");
            }

            category.UpdateDetails(command.Name, command.PermissionSetId);
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Updated,
                                            typeof(Category),
                                            category.Id,
                                            new
            {
                category.Name,
                category.PermissionSetId
            }));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Ejemplo n.º 3
0
        public async Task CreateAsync(CreateCategory command)
        {
            await _createValidator.ValidateCommandAsync(command);

            var categoriesCount = await _dbContext.Categories
                                  .Where(x => x.SiteId == command.SiteId && x.Status != StatusType.Deleted)
                                  .CountAsync();

            var sortOrder = categoriesCount + 1;

            var category = new Category(command.Id,
                                        command.SiteId,
                                        command.Name,
                                        sortOrder,
                                        command.PermissionSetId);

            _dbContext.Categories.Add(category);
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Created,
                                            typeof(Category),
                                            category.Id,
                                            new
            {
                category.Name,
                category.PermissionSetId,
                category.SortOrder
            }));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Ejemplo n.º 4
0
        public async Task DeleteAsync(DeleteCategory command)
        {
            var category = await _dbContext.Categories
                           .Include(x => x.Forums)
                           .FirstOrDefaultAsync(x =>
                                                x.SiteId == command.SiteId &&
                                                x.Id == command.Id &&
                                                x.Status != StatusType.Deleted);

            if (category == null)
            {
                throw new DataException($"Category with Id {command.Id} not found.");
            }

            category.Delete();
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Deleted,
                                            typeof(Category),
                                            category.Id));

            var otherCategories = await _dbContext.Categories
                                  .Where(x =>
                                         x.SiteId == command.SiteId &&
                                         x.Id != command.Id &&
                                         x.Status != StatusType.Deleted)
                                  .ToListAsync();

            for (int i = 0; i < otherCategories.Count; i++)
            {
                otherCategories[i].Reorder(i + 1);
                _dbContext.Events.Add(new Event(command.SiteId,
                                                command.UserId,
                                                EventType.Reordered,
                                                typeof(Category),
                                                otherCategories[i].Id,
                                                new
                {
                    otherCategories[i].SortOrder
                }));
            }

            foreach (var forum in category.Forums)
            {
                forum.Delete();
                _dbContext.Events.Add(new Event(command.SiteId,
                                                command.UserId,
                                                EventType.Deleted,
                                                typeof(Forum),
                                                forum.Id));
            }

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Ejemplo n.º 5
0
        public async Task CreateAsync(CreateForum command)
        {
            await _createValidator.ValidateCommandAsync(command);

            var forumsCount = await _dbContext.Forums
                              .Where(x => x.CategoryId == command.CategoryId && x.Status != ForumStatusType.Deleted)
                              .CountAsync();

            var sortOrder = forumsCount + 1;

            var forum = new Forum(command.Id,
                                  command.CategoryId,
                                  command.Name,
                                  command.Slug,
                                  command.Description,
                                  sortOrder,
                                  command.PermissionSetId);

            _dbContext.Forums.Add(forum);
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Created,
                                            typeof(Forum),
                                            forum.Id,
                                            new
            {
                forum.Name,
                forum.Slug,
                forum.Description,
                forum.CategoryId,
                forum.PermissionSetId,
                forum.SortOrder
            }));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Ejemplo n.º 6
0
        public async Task MoveAsync(MoveForum command)
        {
            var forum = await _dbContext.Forums
                        .FirstOrDefaultAsync(x =>
                                             x.Category.SiteId == command.SiteId &&
                                             x.Id == command.Id &&
                                             x.Status != ForumStatusType.Deleted);

            if (forum == null)
            {
                throw new DataException($"Forum with Id {command.Id} not found.");
            }

            if (command.Direction == Direction.Up)
            {
                forum.MoveUp();
            }
            else if (command.Direction == Direction.Down)
            {
                forum.MoveDown();
            }

            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Reordered,
                                            typeof(Forum),
                                            forum.Id,
                                            new
            {
                forum.SortOrder
            }));

            var sortOrderToReplace = forum.SortOrder;

            var adjacentForum = await _dbContext.Forums
                                .FirstOrDefaultAsync(x =>
                                                     x.CategoryId == forum.CategoryId &&
                                                     x.SortOrder == sortOrderToReplace &&
                                                     x.Status != ForumStatusType.Deleted);

            if (command.Direction == Direction.Up)
            {
                adjacentForum.MoveDown();
            }
            else if (command.Direction == Direction.Down)
            {
                adjacentForum.MoveUp();
            }

            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Reordered,
                                            typeof(Forum),
                                            adjacentForum.Id,
                                            new
            {
                adjacentForum.SortOrder
            }));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Categories(command.SiteId));
        }
Ejemplo n.º 7
0
        public async Task <IndexPageModel> BuildIndexPageModelAsync(Guid siteId)
        {
            var model = new IndexPageModel
            {
                Categories = await _cacheManager.GetOrSetAsync(CacheKeys.Categories(siteId), async() =>
                {
                    var categories = await _dbContext.Categories
                                     .Include(x => x.Forums)
                                     .Where(x => x.SiteId == siteId && x.Status == CategoryStatusType.Published)
                                     .OrderBy(x => x.SortOrder)
                                     .ToListAsync();

                    return(categories.Select(category => new IndexPageModel.CategoryModel
                    {
                        Id = category.Id,
                        Name = category.Name,
                        PermissionSetId = category.PermissionSetId,
                        ForumIds = category.Forums
                                   .Where(x => x.Status == ForumStatusType.Published)
                                   .OrderBy(x => x.SortOrder)
                                   .Select(x => x.Id)
                                   .ToList()
                    }).ToList());
                })
            };

            foreach (var category in model.Categories)
            {
                category.Forums = new List <IndexPageModel.ForumModel>();

                foreach (var forumId in category.ForumIds)
                {
                    var forum = await _cacheManager.GetOrSetAsync(CacheKeys.Forum(forumId), async() =>
                    {
                        var entity = await _dbContext.Forums
                                     .Include(x => x.LastPost).ThenInclude(x => x.CreatedByUser)
                                     .Include(x => x.LastPost).ThenInclude(x => x.Topic)
                                     .Where(x => x.Id == forumId && x.Status == ForumStatusType.Published)
                                     .OrderBy(x => x.SortOrder)
                                     .FirstOrDefaultAsync();

                        if (entity != null)
                        {
                            return(new IndexPageModel.ForumModel
                            {
                                Id = entity.Id,
                                Name = entity.Name,
                                Slug = entity.Slug,
                                Description = entity.Description,
                                TotalTopics = entity.TopicsCount,
                                TotalReplies = entity.RepliesCount,
                                PermissionSetId = entity.PermissionSetId,
                                LastTopicId = entity.LastPost?.TopicId == null ? entity.LastPost?.Id : entity.LastPost?.Topic?.Id,
                                LastTopicTitle = entity.LastPost?.Title ?? entity.LastPost?.Topic?.Title,
                                LastTopicSlug = entity.LastPost?.Slug ?? entity.LastPost?.Topic?.Slug,
                                LastPostTimeStamp = entity.LastPost?.CreatedOn,
                                LastPostUserId = entity.LastPost?.CreatedByUser?.Id,
                                LastPostUserDisplayName = entity.LastPost?.CreatedByUser?.DisplayName
                            });
                        }

                        return(new IndexPageModel.ForumModel());
                    });

                    category.Forums.Add(forum);
                }
            }

            return(model);
        }