Exemplo n.º 1
0
        public async Task PinAsync(PinTopic command)
        {
            var topic = await _dbContext.Posts
                        .FirstOrDefaultAsync(x =>
                                             x.Id == command.Id &&
                                             x.TopicId == null &&
                                             x.ForumId == command.ForumId &&
                                             x.Forum.Category.SiteId == command.SiteId &&
                                             x.Status != StatusType.Deleted);

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

            topic.Pin(command.Pinned);

            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Pinned,
                                            typeof(Post),
                                            command.Id));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(topic.ForumId));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public async Task DeleteAsync(DeleteReply command)
        {
            var reply = await _dbContext.Posts
                        .Include(x => x.CreatedByUser)
                        .Include(x => x.Topic).ThenInclude(x => x.Forum).ThenInclude(x => x.Category)
                        .Include(x => x.Topic).ThenInclude(x => x.Forum).ThenInclude(x => x.LastPost)
                        .FirstOrDefaultAsync(x =>
                                             x.Id == command.Id &&
                                             x.TopicId == command.TopicId &&
                                             x.Topic.ForumId == command.ForumId &&
                                             x.Topic.Forum.Category.SiteId == command.SiteId &&
                                             x.Status != PostStatusType.Deleted);

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

            reply.Delete();

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

            if (reply.IsAnswer)
            {
                reply.Topic.SetAsAnswered(false);
            }

            reply.Topic.DecreaseRepliesCount();
            reply.Topic.Forum.DecreaseRepliesCount();
            reply.Topic.Forum.Category.DecreaseRepliesCount();
            reply.CreatedByUser.DecreaseRepliesCount();

            if (reply.Topic.Forum.LastPost != null && (reply.Id == reply.Topic.Forum.LastPostId || reply.Id == reply.Topic.Forum.LastPost.TopicId))
            {
                var newLastPost = await _dbContext.Posts
                                  .Where(x => x.ForumId == reply.Topic.ForumId &&
                                         x.Status == PostStatusType.Published &&
                                         (x.Topic == null || x.Topic.Status == PostStatusType.Published) &&
                                         x.Id != reply.Id)
                                  .OrderByDescending(x => x.CreatedOn)
                                  .FirstOrDefaultAsync();

                if (newLastPost != null)
                {
                    reply.Topic.Forum.UpdateLastPost(newLastPost.Id);
                }
                else
                {
                    reply.Topic.Forum.UpdateLastPost(null);
                }
            }

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(reply.Topic.ForumId));
        }
Exemplo n.º 4
0
        public async Task UpdateAsync(UpdateForum command)
        {
            await _updateValidator.ValidateCommandAsync(command);

            var forum = await _dbContext.Forums
                        .Include(x => x.Category)
                        .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.");
            }

            var originalCategoryId = forum.CategoryId;

            if (originalCategoryId != command.CategoryId)
            {
                forum.Category.DecreaseTopicsCount(forum.TopicsCount);
                forum.Category.DecreaseRepliesCount(forum.RepliesCount);

                var newCategory = await _dbContext.Categories.FirstOrDefaultAsync(x => x.Id == command.CategoryId);

                newCategory.IncreaseTopicsCount(forum.TopicsCount);
                newCategory.IncreaseRepliesCount(forum.RepliesCount);

                await ReorderForumsInCategory(originalCategoryId, command.Id, command.SiteId, command.UserId);

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

                forum.Reorder(newCategoryForumsCount + 1);
            }

            forum.UpdateDetails(command.CategoryId, command.Name, command.Slug, command.Description, command.PermissionSetId);

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

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(command.Id));
            _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId));
        }
Exemplo n.º 5
0
        public async Task <string> CreateAsync(CreateTopic command)
        {
            await _createValidator.ValidateCommandAsync(command);

            var title = Regex.Replace(command.Title, @"\s+", " "); // Remove multiple spaces from title

            var slug = string.IsNullOrWhiteSpace(command.Slug)
                ? await GenerateSlugAsync(command.ForumId, title)
                : command.Slug;

            var topic = Post.CreateTopic(command.Id,
                                         command.ForumId,
                                         command.UserId,
                                         title,
                                         slug,
                                         command.Content,
                                         command.Status);

            _dbContext.Posts.Add(topic);
            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Created,
                                            typeof(Post),
                                            command.Id,
                                            new
            {
                command.ForumId,
                title,
                Slug = slug,
                command.Content,
                command.Status
            }));

            var forum = await _dbContext.Forums.Include(x => x.Category).FirstOrDefaultAsync(x => x.Id == topic.ForumId);

            forum.UpdateLastPost(topic.Id);
            forum.IncreaseTopicsCount();
            forum.Category.IncreaseTopicsCount();

            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == topic.CreatedBy);

            user.IncreaseTopicsCount();

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(forum.Id));

            return(slug);
        }
Exemplo n.º 6
0
        public async Task CreateAsync(CreateReply command)
        {
            await _createValidator.ValidateCommandAsync(command);

            var reply = Post.CreateReply(command.Id,
                                         command.TopicId,
                                         command.ForumId,
                                         command.UserId,
                                         command.Content,
                                         command.Status);

            _dbContext.Posts.Add(reply);

            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Created,
                                            typeof(Post),
                                            command.Id,
                                            new
            {
                command.TopicId,
                command.ForumId,
                command.Content,
                command.Status
            }));

            var topic = await _dbContext.Posts
                        .Include(x => x.Forum)
                        .ThenInclude(x => x.Category)
                        .FirstOrDefaultAsync(x => x.Id == reply.TopicId);

            topic.UpdateLastReply(reply.Id);
            topic.IncreaseRepliesCount();
            topic.Forum.UpdateLastPost(reply.Id);
            topic.Forum.IncreaseRepliesCount();
            topic.Forum.Category.IncreaseRepliesCount();

            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == reply.CreatedBy);

            user.IncreaseRepliesCount();

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(topic.ForumId));
        }
Exemplo n.º 7
0
        public async Task <string> UpdateAsync(UpdateTopic command)
        {
            await _updateValidator.ValidateCommandAsync(command);

            var topic = await _dbContext.Posts
                        .FirstOrDefaultAsync(x =>
                                             x.Id == command.Id &&
                                             x.TopicId == null &&
                                             x.ForumId == command.ForumId &&
                                             x.Forum.Category.SiteId == command.SiteId &&
                                             x.Status != StatusType.Deleted);

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

            var title = Regex.Replace(command.Title, @"\s+", " "); // Remove multiple spaces from title

            var slug = string.IsNullOrWhiteSpace(command.Slug)
                ? await GenerateSlugAsync(command.ForumId, title)
                : command.Slug;

            topic.UpdateDetails(command.UserId, title, slug, command.Content, command.Status);

            _dbContext.Events.Add(new Event(command.SiteId,
                                            command.UserId,
                                            EventType.Updated,
                                            typeof(Post),
                                            command.Id,
                                            new
            {
                title,
                Slug = slug,
                command.Content,
                command.Status
            }));

            await _dbContext.SaveChangesAsync();

            _cacheManager.Remove(CacheKeys.Forum(topic.ForumId));

            return(slug);
        }
Exemplo n.º 8
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);
        }