/// <summary> /// 新增文章分类 /// </summary> /// <param name="model"></param> public void Insert(InsertArticleCategoryInput model) { if (string.IsNullOrWhiteSpace(model.Name)) { throw new JIFException("分类名称不能为空"); } if (_articleCategoryRepository.Table.Any(d => d.Name == model.Name)) { throw new JIFException("分类名称已经存在"); } // 新增时排序默认排到所属分类最末 var orderIndex = _articleCategoryRepository.Table.Where(d => d.ParentId == model.ParentId).Max(d => d.OrderIndex) + 1; var entity = new ArticleCategory { Name = model.Name, ParentId = model.ParentId, CoverImg = model.CoverImg, Description = model.Description, OrderIndex = orderIndex }; _articleCategoryRepository.Insert(entity); }
/// <summary> /// 修改分类信息 /// </summary> /// <param name="id"></param> /// <param name="model"></param> public void Update(int id, InsertArticleCategoryInput model) { if (_articleCategoryRepository.Table.Any(d => d.Name == model.Name && d.Id != id)) { throw new JIFException("分类名称已经存在"); } var entity = _articleCategoryRepository.Get(id); if (entity == null) { throw new JIFException("分类不存在"); } // 若更改所属父级, 则组内排序初始化为组内末尾 if (entity.ParentId != model.ParentId) { entity.OrderIndex = _articleCategoryRepository.Table.Where(d => d.ParentId == model.ParentId).Max(d => d.OrderIndex) + 1; } entity.Name = model.Name; entity.ParentId = model.ParentId; entity.CoverImg = model.CoverImg; entity.Description = model.Description; _articleCategoryRepository.Update(entity); }
public JsonResult SaveCategory(int id, InsertArticleCategoryInput model) { if (id == 0) { _articleService.Insert(model); } else { _articleService.Update(id, model); } return(JsonOk()); }