public async void Should_CreateCategory() { //初始化的分类条数 var initCategoryCount = base.UsingDbContext(context => context.Category.Count()); //增加三条分类条数 var input1 = new CreateCategoryInput { ParentId = base.UsingDbContext(context => { return context.Category.FirstOrDefault().Id; }), CategoryName = "人文历史", Description = "这个是用来介绍人文历史的", Taxonomy = TaxonomyEnum.Category }; var task1 = _categoryService.CreateCategory(input1); var input2 = new CreateCategoryInput() { CategoryName = "社会科学", Description = "这个就是介绍社会科学的啦", Taxonomy = TaxonomyEnum.Category }; var task2 = _categoryService.CreateCategory(input2); var input3 = new CreateCategoryInput() { CategoryName = "新技术", Taxonomy = TaxonomyEnum.PostTag }; var task3 = _categoryService.CreateCategory(input3); await Task.WhenAll(task1, task2, task3); //校验规则 base.UsingDbContext(context => { context.Category.Count().ShouldBe(initCategoryCount + 3); var c1 = context.Category.Where(w => w.CategoryName == "人文历史").FirstOrDefault(); c1.ParentId.HasValue.ShouldBe(true); c1.Taxonomy.ShouldBe(TaxonomyEnum.Category); c1.Description.IsNullOrEmpty().ShouldBe(false); var c2 = context.Category.FirstOrDefault(w => w.CategoryName == "社会科学"); c2.ParentId.HasValue.ShouldBe(false); c2.Taxonomy.ShouldBe(TaxonomyEnum.Category); c2.Description.IsNullOrEmpty().ShouldBe(false); var c3 = context.Category.FirstOrDefault(w => w.CategoryName == "新技术"); c3.ParentId.HasValue.ShouldBe(false); c3.Taxonomy.ShouldBe(TaxonomyEnum.PostTag); c3.Description.IsNullOrEmpty().ShouldBe(true); }); }
public async Task<RenderMessageOutput> CreateCategory(CreateCategoryInput input) { //校验分类是否已经存在 var isExist = await _categoryRepository.CheckCategoryIsExist(input.CategoryName, input.ParentId, input.Taxonomy); if (isExist) { return new RenderMessageOutput { Success = false, Message = "操作失败,已经存在" }; } Logger.Info("Creating a category for input : " + input); var category = new Category { Id = Guid.NewGuid(), CategoryName = input.CategoryName, Description = input.Description, Taxonomy = input.Taxonomy }; if (input.ParentId.HasValue) { category.ParentId = input.ParentId; } await _categoryRepository.InsertAsync(category); return new RenderMessageOutput(); //操作成功 }