Esempio n. 1
0
        public virtual async Task <int> AddAsync(TblPostCategories record)
        {
            _dbContext.PostCategories.Add(record);
            await _dbContext.SaveChangesAsync();

            QueryCacheManager.ExpireTag(CacheTags.PostCategory);

            _eventPublisher.EntityInserted(record);

            return(record.Id);
        }
        public virtual async Task <PostCategoryModel> PreparePostCategoryModelAsync(TblPostCategories category)
        {
            PostCategoryModel result;

            if (category == null)
            {
                result = new PostCategoryModel();
            }
            else
            {
                result = category.Adapt <PostCategoryModel>();
                await category.LoadAllLocalizedStringsToModelAsync(result);
            }
            return(result);
        }
Esempio n. 3
0
        public virtual async Task UpdateAsync(TblPostCategories record)
        {
            var allCategories = (await GetAsEnumerableAsync()).ToList();
            var oldRecord     = allCategories.FirstOrDefault(p => p.Id == record.Id);

            allCategories.RemoveWhere(p => p.Id == record.Id);
            allCategories.Add(record);
            if (DetectLoop(allCategories, record, null))
            {
                throw new Exception("Self referencing loop detected");
            }

            _dbContext.PostCategories.AddOrUpdate(record);
            await _dbContext.SaveChangesAsync();

            QueryCacheManager.ExpireTag(CacheTags.PostCategory);

            _eventPublisher.EntityUpdated(record, oldRecord);
        }
Esempio n. 4
0
        protected virtual bool DetectLoop(List <TblPostCategories> categories, TblPostCategories category, HashSet <TblPostCategories> visited)
        {
            visited = visited ?? new HashSet <TblPostCategories>();
            var parentCategory = categories.FirstOrDefault(p => p.Id == category.ParentCategoryId);

            if (parentCategory != null)
            {
                if (!visited.Contains(parentCategory))
                {
                    visited.Add(parentCategory);
                    return(DetectLoop(categories, parentCategory, visited));
                }
                else
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
        protected virtual string GetCategoryName(List <TblPostCategories> allCategories, string categoryName, TblPostCategories currentCategory)
        {
            if (currentCategory.ParentCategoryId == null)
            {
                return(categoryName);
            }

            return(GetCategoryName(allCategories, "——" + categoryName,
                                   allCategories.FirstOrDefault(p => p.Id == currentCategory.ParentCategoryId.Value)));
        }