public async Task<RenderMessageOutput> UpdateCategory(UpdateCategoryInput input) { //写入日志 Logger.Info("Updating a category for input : " + input); var category = await _categoryRepository.GetAsync(input.Id); if (category != null) { //校验分类是否已经存在 var isExist = await _categoryRepository.CheckCategoryIsExist(category.CategoryName, category.ParentId, category.Taxonomy); if (isExist && category.CategoryName == input.CategoryName ) { return new RenderMessageOutput { Success = false, Message = "操作失败,已经存在" }; } if (!input.CategoryName.IsNullOrEmpty()) { category.CategoryName = input.CategoryName; } if (input.Order.HasValue) { category.Order = (int)input.Order; } if (input.ParentId.HasValue) { category.ParentId = input.ParentId; } if (!input.Description.IsNullOrEmpty()) { category.Description = input.Description; } if (input.Taxonomy.HasValue) { category.Taxonomy = (TaxonomyEnum)input.Taxonomy; } if (input.Order.HasValue) { category.Order = (int)input.Order; } await _categoryRepository.UpdateAsync(category); return new RenderMessageOutput(); } else { return new RenderMessageOutput { Success = false, Message = "操作失败,分类不存在" }; } }
public async void Should_UpdateCategory() { var categoryDto = new UpdateCategoryInput() { Id = Guid.Parse("81f79e33-e179-4038-bbb5-1a90be081ca9"), CategoryName = "修改后的" }; await _categoryService.UpdateCategory(categoryDto); //测试是否修改成功 UsingDbContext(context => { context.Category.Count().ShouldBe(1); var category = context.Category.FirstOrDefault(); category.CategoryName.ShouldBe("修改后的"); category.Id.ShouldBe(Guid.Parse("81f79e33-e179-4038-bbb5-1a90be081ca9")); category.Description.ShouldBe("用来发布文章的"); category.Order.ShouldBe(0); category.ParentId.ShouldBe(null); }); }