Esempio n. 1
0
        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 });
        }
Esempio n. 2
0
        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 });
        }