コード例 #1
0
        private List <Keyword> StringToKeywordList(string keywordStr)
        {
            List <Keyword> keywords = new List <Keyword>();

            String[] sKeywords = keywordStr.Split(new char[] { ',' });
            foreach (string kw in sKeywords)
            {
                var     kwKey   = kw.Trim();
                Keyword keyword = _keywordRepository.FindValue(kwKey);
                if (keyword == null)
                {
                    keyword = new Keyword
                    {
                        Value = kwKey
                    };
                    _keywordRepository.InsertOrUpdate(keyword);
                    _keywordRepository.Save();
                }
                keywords.Add(keyword);
            }
            return(keywords);
        }
コード例 #2
0
        public ActionResult Create(HttpPostedFileBase thumbnail, string returnUrl, ArticleModel articleModel)
        {
            if (thumbnail != null && thumbnail.ContentLength > 0)
            {
                string fileName      = Convert.ToInt32((DateTime.Now - new DateTime(2010, 01, 01)).TotalSeconds) + "_" + thumbnail.FileName.Replace(' ', '-');
                string currentDomain = System.Configuration.ConfigurationManager.AppSettings["CurrentDomain"];
                string folder        = "UploadedImages/Article";
                string filePath      = System.Configuration.ConfigurationManager.AppSettings[currentDomain] + @"\" + folder + @"\" + fileName;

                using (new Impersonator("uploaduser", "", "Upload@@123"))
                {
                    thumbnail.SaveAs(filePath);
                }
                articleModel.Thumbnail = currentDomain + "/" + folder + "/" + fileName;
            }
            else if (string.IsNullOrEmpty(articleModel.Thumbnail))
            {
                articleModel.Thumbnail = "/Images/NoImage.png";
            }

            ModelState.Clear();
            TryValidateModel(articleModel);

            if (ModelState.IsValid)
            {
                var article = AutoMapper.Mapper.Map <ArticleModel, Article>(articleModel);
                if (article.ArticleContent == null)
                {
                    article.ArticleContent = "";
                }
                _articleRepository.Load(article, v => v.Reporters);
                article.Reporters.Clear();
                if (!string.IsNullOrWhiteSpace(articleModel.ReporterIds))
                {
                    var reporters = articleModel.ReporterIds.Split(',');
                    foreach (string rpId in reporters)
                    {
                        if (!string.IsNullOrWhiteSpace(rpId))
                        {
                            var rpIdNormalize = rpId.Trim();
                            var reporter      = _reporterRepository.Find(Convert.ToInt32(rpIdNormalize));
                            if (reporter != null)
                            {
                                article.Reporters.Add(reporter);
                            }
                        }
                    }
                }

                _articleRepository.Load(article, a => a.Keywords);
                article.Keywords.Clear();
                if (!string.IsNullOrWhiteSpace(articleModel.Keywords))
                {
                    var keywords = articleModel.Keywords.Split(',');
                    foreach (string kw in keywords)
                    {
                        if (!string.IsNullOrWhiteSpace(kw))
                        {
                            var kwNormalize = kw.Trim();
                            var keyword     = _keywordRepository.FindValue(kwNormalize);
                            if (keyword == null)
                            {
                                keyword = new Keyword()
                                {
                                    Value = kwNormalize
                                }
                            }
                            ;
                            article.Keywords.Add(keyword);
                        }
                    }
                }

                _articleRepository.Load(article, a => a.Categories);
                article.Categories.Clear();
                if (!string.IsNullOrWhiteSpace(articleModel.CategoryIds))
                {
                    var cats = articleModel.CategoryIds.Split(',');
                    foreach (string ct in cats)
                    {
                        var ctId = Convert.ToInt32(ct);
                        var cat  = _articleCategoryRepository.Find(ctId);
                        if (cat != null)
                        {
                            article.Categories.Add(cat);
                        }
                    }
                }
                article.CreatedTime = DateTime.Now;

                if (articleModel.Published)
                {
                    article.IsPublished    = true;
                    article.ArticleStatus  = (int)ArticleStatus.Good;
                    article.ArticleContent = NormalizeContent(articleModel.ArticleDraft);
                    article.PublishedTime  = DateTime.Now;
                }
                else
                {
                    article.IsPublished   = false;                         //Mac dinh la false
                    article.ArticleStatus = (int)ArticleStatus.NeedReview; //Mặc định bài đăng chưa được duyệt
                }

                if (article.ArticleSeriesId.HasValue)
                {
                    article.SeriesOrder = _articleRepository.GetLastOrder(article.ArticleSeriesId.Value);
                }
                _articleRepository.InsertOrUpdate(article);
                _articleRepository.Save();

                UpdateRevision(article.Id);

                return(RedirectToAction("Management", "Article"));
                //return Json(new { success = true, time = DateTime.Now.ToString("dd/MM/yyyy HH:mm"), id = article.Id, UniqueTitle = article.UniqueTitle, date = article.PublishedTime.ToString("dd-MM-yyyy") });
            }

            ViewBag.PossibleReporters = _reporterRepository.All.ToList();
            ViewBag.PossibleKeywords  = _keywordRepository.All;
            ViewBag.User = context.Users.Find(User.Identity.GetUserId());
            return(View(articleModel));
        }