public ActionResult Add(string name, int type) { try { Category category = new Category() { Name = name, Type = type }; _categoryService.AddCategory(category); } catch (DomainException ex) { ViewBag.Message = ex.Errors.First().Value; } return RedirectToAction("Index", new { Message = ViewBag.Message }); }
public ActionResult Add(string name, int parentId, int type) { try { Category parent = null; if (parentId > 0) { parent = _categoryService.GetCategoryById(parentId); } Category category = new Category() { Name = name, Parent = parent, Type = type }; _categoryService.AddCategory(category); } catch (DomainException ex) { ViewBag.Message = ex.Errors.First().Value; } return RedirectToAction("Index", new { Message = ViewBag.Message }); }
public int AddCategory(Category category) { if (!_roleService.AdminPower()) { throw new DomainException("NoPower", "对不起,您没有权限!"); } if (string.IsNullOrEmpty(category.Name)) { throw new DomainException("Name", "分类名称不能为空!"); } if (category.Parent != null && GetCategoryById(category.Parent.CategoryId) == null) { throw new DomainException("NoCategory", "没有找到父分类!"); } if ((int)_categoryRepository.Single(query => query.Where(c => (c.Name == category.Name) && (c.Parent == category.Parent) && (c.Type == category.Type)).Count()) > 0) { throw new DomainException("NoCategory", "发现同名分类!"); } int lint = _categoryRepository.Add(category); _categoryRepository.Save(); _categorys = null; return lint; }
public void UpdateCategory(Category category) { if (!_roleService.AdminPower()) { throw new DomainException("NoPower", "对不起,您没有权限!"); } if (GetCategoryById(category.CategoryId) == null) { throw new DomainException("NoFind", "没有找到该分类!"); } _categorys = null; _categoryRepository.Update(category); }
public JsonResult AjaxAdd(string name, int parentId, int articleId) { string message = null; try { Category parent = null; if (parentId > 0) { parent = _categoryService.GetCategoryById(parentId); if (parent == null) { throw new Exception("上级分类不存在!"); } } Category category = new Category() { Name = name, Type = CategoryType.Category, Parent = parent }; _categoryService.AddCategory(category); Article article = articleId > 0 ? _articleService.GetArticle(articleId) : new Article(); MvcHtmlString categoryList = CategoryHelper.BulidArticleCategory(null, article); return Json(new { success = true, data = categoryList.ToString(), data2 = CategoryHelper.BulidCategoryList(null).ToString() }); } catch (Domain.DomainException ex) { message = ex.Errors.First().Value; } catch (Exception ex) { message = ex.Message; } return Json(new { success = false, message = message }); }
public JsonResult AjaxAdd(ArticleAjaxModel model) { bool success = false; string message = null; object data = null; try { if (ModelState.IsValid) { Article article = new Article() { Author = _userSrvice.GetLoginUser(), Content = model.Content, Title = model.Title, Status = ArticleStatus.Temp, CreateDate = DateTime.Now, Type = ArticleType.Blog, ModifyDate = DateTime.Now, Categorys = new List<Category>() }; if (model.Categorys != null) { foreach (int categoryId in model.Categorys) { Category cat = _categoryService.GetCategoryById(categoryId); if (cat == null) { throw new Exception("不存在该分类!"); } cat.Count++; _categoryService.UpdateCategory(cat); article.Categorys.Add(cat); } } if (model.Tags != null) { foreach (string tag in model.Tags) { Category category = _categoryService.FindTagByName(tag); if (category == null) { category = new Category() { Name = tag, Count = 1, Type = CategoryType.Tag }; _categoryService.AddCategory(category); } else { category.Count++; _categoryService.UpdateCategory(category); } article.Categorys.Add(category); } } data = _articleService.AddArticle(article); success = true; } else { foreach (var value in ModelState.Values) { foreach (var error in value.Errors) { message = error.ErrorMessage; break; } if (message != null) { break; } } } } catch (DomainException ex) { message = ex.Errors.First().Value; } catch (Exception ex) { message = ex.Message; } return Json(new { success = success, message = message, data = data }); }
public JsonResult AjaxUpdate(ArticleAjaxModel model) { try { Article old = _articleService.GetArticle(model.ArticleId); if (!string.IsNullOrEmpty(model.Title)) { old.Title = model.Title; } if (model.Categorys != null) { if (model.Categorys.Length == 1 && model.Categorys[0] == 0) { IList<Category> list = old.Categorys.Where(c => c.Type == CategoryType.Category).ToList(); for (int i = 0; i < list.Count; i++) { old.Categorys.Remove(list[i]); } } else { foreach (int categoryId in model.Categorys) { if (old.Categorys.Where(c => c.CategoryId == categoryId).Count() == 0) { Category cat = _categoryService.GetCategoryById(categoryId); cat.Count++; _categoryService.UpdateCategory(cat); } } IList<Category> list = old.Categorys.Where(c => c.Type == CategoryType.Category).ToList(); for (int i = 0; i < list.Count; i++) { old.Categorys.Remove(list[i]); } foreach (int categoryId in model.Categorys) { old.Categorys.Add(_categoryService.GetCategoryById(categoryId)); } } } if (!string.IsNullOrEmpty(model.Content)) { model.Content = model.Content.Replace("../../../Scripts/", "/Scripts/"); old.Content = model.Content; } if (model.Tags != null) { if (string.IsNullOrEmpty(model.Tags[0])) { IList<Category> list = old.Categorys.Where(c => c.Type == CategoryType.Tag).ToList(); for (int i = 0; i < list.Count; i++) { old.Categorys.Remove(list[i]); } } else { foreach (string tag in model.Tags) { Category category = _categoryService.FindTagByName(tag); if (category == null) { category = new Category() { Name = tag, Count = 0, Type = CategoryType.Tag }; _categoryService.AddCategory(category); } if (old.Categorys.Where(t => t.Name == tag && t.Type == CategoryType.Tag).Count() == 0) { category.Count++; _categoryService.UpdateCategory(category); } } IList<Category> list = old.Categorys.Where(c => c.Type == CategoryType.Tag).ToList(); for (int i = 0; i < list.Count; i++) { old.Categorys.Remove(list[i]); } foreach (string tag in model.Tags) { Category category = _categoryService.FindTagByName(tag); old.Categorys.Add(category); } } } if (model.Status != null) { old.Status = model.Status.Value; ViewBag.Message = ArticleStatus.Names[model.Status.Value]; } if (model.CreateDate != null) { old.CreateDate = Convert.ToDateTime(model.CreateDate); ViewBag.Message = old.CreateDate.ToString("yyyy 年 M 月 d 日 h:mm"); } _articleService.UpdateArticle(old); ViewBag.Error = null; } catch (DomainException ex) { ViewBag.Error = ex.Errors.First().Value; } catch (Exception ex) { ViewBag.Error = ex.Message; } return Json(new { success = ViewBag.Error == null ? true : false, message = ViewBag.Error == null ? ViewBag.Message : ViewBag.Error }); }