Пример #1
0
        private async Task SaveCategory(List <Guid> newCategoryIDs, Bookmark bookmark)
        {
            //删除旧的未选择的分类
            foreach (var oldCategory in bookmark.Categorys)
            {
                var category = await _categoryRepository.GetAsync(oldCategory.CategoryId);

                //旧的分类在新分类中是否存在
                var oldCategoryNameInNewCategorys = newCategoryIDs.FirstOrDefault(t => t == category.Id);

                if (oldCategoryNameInNewCategorys == null)
                {
                    bookmark.RemoveCategory(oldCategory.CategoryId);

                    category.DecreaseUsageCount();

                    await _categoryRepository.UpdateAsync(category);
                }
                else
                {
                    newCategoryIDs.Remove(oldCategoryNameInNewCategorys);
                }
            }

            //添加新选中的分类
            foreach (var newCategoryID in newCategoryIDs)
            {
                var category = await _categoryRepository.GetAsync(newCategoryID);

                bookmark.AddCategory(newCategoryID);

                category.IncreaseUsageCount();

                await _categoryRepository.UpdateAsync(category);
            }
        }