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 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));
        }
Ejemplo n.º 5
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.º 6
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.º 7
0
        public async Task <IList <CurrentForumModel> > CurrentForumsAsync()
        {
            var site = await CurrentSiteAsync();

            return(await _cacheManager.GetOrSetAsync(CacheKeys.CurrentForums(site.Id), async() =>
            {
                var forums = await _dbContext.Forums
                             .Where(x => x.Category.SiteId == site.Id && x.Status == ForumStatusType.Published)
                             .Select(x => new
                {
                    x.Id,
                    PermissionSetId = x.PermissionSetId ?? x.Category.PermissionSetId
                })
                             .ToListAsync();

                return forums.Select(forum => new CurrentForumModel
                {
                    Id = forum.Id,
                    PermissionSetId = forum.PermissionSetId
                }).ToList();
            }));
        }