Beispiel #1
0
 public static Article MapToArticle(CreateArticleViewModel articleVM)
 {
     Article article = new Article();
     article.Name = articleVM.Name;
     article.Text = articleVM.Text;
     return article;
 }
Beispiel #2
0
 public static CreateArticleViewModel MapToEditAticle(Article article, string tags)
 {
     CreateArticleViewModel articleVM = new CreateArticleViewModel();
     articleVM.Id = article.Id;
     articleVM.Name = article.Name;
     articleVM.Text = article.Text;
     EnumCategory choice;
     if (Enum.TryParse(article.Category.Name, out choice))
         articleVM.CategoryName = choice;
     articleVM.Tags = tags;
     return articleVM;
 }
Beispiel #3
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 #4
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);
        }