Beispiel #1
0
        public ActionResult Create(CreateArticleViewModel articleVM)
        {
            if (ModelState.IsValid)
            {
                Article article = Mapper.MapToArticle(articleVM);
                db.Articles.Add(article);
                var userId = User.Identity.GetUserId();
                article.UserId = userId;
                article.TimeCreated = DateTime.Now;

                //add category
                Category category = db.Categories.FirstOrDefault(x => x.Name == articleVM.CategoryName.ToString());
                if (category != null)
                {
                    article.Category = category;
                    category.Articles.Add(article);
                }
                else
                {
                    ModelState.AddModelError("", "This category does not exist.");
                    return View(articleVM);
                }

                // add tags
                if (articleVM.Tags != null)
                {
                    RegexOptions options = RegexOptions.None;
                    Regex regex = new Regex(@"[ ]{2,}", options);
                    articleVM.Tags = regex.Replace(articleVM.Tags, @" ");
                    string[] tags = Regex.Split(articleVM.Tags, " ");
                    article.Tags = new List<Tag>();
                    foreach (string item in tags)
                    {
                        Tag tag = db.Tags.FirstOrDefault(x => x.Name == item);
                        if (tag != null)
                        {
                            if (!article.Tags.Contains(tag))
                            {
                                article.Tags.Add(tag);
                                tag.Articles.Add(article);
                            }
                        }
                        else
                        {
                            Tag newTag = new Tag();
                            newTag.Name = item;
                            db.Tags.Add(newTag);
                            article.Tags.Add(newTag);
                            newTag.Articles = new List<Article>();
                            newTag.Articles.Add(article);
                        }
                    }
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "Check your input.");
            }

            return View(articleVM);
        }
Beispiel #2
0
        public ActionResult Edit(CreateArticleViewModel articleVM)
        {
            if (ModelState.IsValid)
            {
                Article article = db.Articles.Find(articleVM.Id);
                // set new category
                if (article.Category.Name != articleVM.CategoryName.ToString())
                {
                    Category category = db.Categories.FirstOrDefault(x => x.Name == articleVM.CategoryName.ToString());
                    if (category != null)
                    {
                        article.Category = category;
                        category.Articles.Add(article);
                    }
                    else
                    {
                        ModelState.AddModelError("", "This category does not exist.");
                        return View(articleVM);
                    }
                }

                // set new tags
                RegexOptions options = RegexOptions.None;
                Regex regex = new Regex(@"[ ]{2,}", options);
                articleVM.Tags = regex.Replace(articleVM.Tags, @" ");
                string[] tags = Regex.Split(articleVM.Tags, " ");
                foreach (string item in tags)
                {
                    if (article.Tags.FirstOrDefault(x => x.Name == item) == null)
                    {
                        Tag tag = db.Tags.FirstOrDefault(x => x.Name == item);
                        if (tag != null)
                        {
                            article.Tags.Add(tag);
                            tag.Articles.Add(article);
                        }
                        else
                        {
                            Tag newTag = new Tag();
                            newTag.Name = item;
                            db.Tags.Add(newTag);
                            newTag.Articles = new List<Article>();
                            newTag.Articles.Add(article);
                            article.Tags.Add(newTag);
                        }
                    }
                }

                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(articleVM);
        }