Пример #1
0
        /// <summary>
        /// Toes the article edit model.
        /// </summary>
        /// <param name="article">The article.</param>
        /// <returns>Article View Model</returns>
        public static ArticleEditModel ToArticleEditModel(Article article)
        {
            ArticleEditModel model = new ArticleEditModel
            {
                ArticleId = article.ArticleId,
                Username = article.Username,
                Title = article.Title,
                Content = article.Content
            };

            return model;
        }
Пример #2
0
        /// <summary>
        /// Froms the article edit model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>New Article</returns>
        public static Article FromArticleEditModel(ArticleEditModel model)
        {
            Article article = new Article
            {
                ArticleId = model.ArticleId,
                Modified = DateTime.Now,
                Title = model.Title,
                Username = model.Username,
                Content = model.Content
            };

            return article;
        }
Пример #3
0
        public void SaveArticle(Article article)
        {
            if (context.Articles.Count(b => b.ArticleId == article.ArticleId) != 0)
            {
                context.Articles.Single(b => b.ArticleId == article.ArticleId).Update(article);
            }
            else
            {
                context.Articles.Add(article);
            }

            context.SaveChanges();
        }
Пример #4
0
        public ActionResult Edit(ArticleEditModel model)
        {
            if (ModelState.IsValid)
            {
                Article article = new Article();

                if (repository.Articles.Count(b => b.ArticleId == model.ArticleId) > 0)
                {
                    article = repository.Articles.FirstOrDefault(b => b.ArticleId == model.ArticleId);    
                }
                else
                {
                    article = ArticleMapping.FromArticleEditModel(model);
                }
                
                repository.SaveArticle(article);
                TempData["message"] = string.Format("{0} has been saved", model.Title);
                return RedirectToAction("Index");
            }
            else
            {
                if (model.Content == null || model.Content.Length <= 0)
                {
                    ModelState.AddModelError("", "Please enter some content.");    
                }
                if (model.Title == null || model.Title.Length <= 0)
                {
                    ModelState.AddModelError("", "Please enter a Title.");
                }
                else if(model.Title == null || model.Title.Length > 255)
                {
                    ModelState.AddModelError("", "Title is too long, maximum length is 255 characters.");
                }
                
                return View(model);
            }
        }
Пример #5
0
 /// <summary>
 /// Updates the specified new article.
 /// </summary>
 /// <param name="newArticle">The new article.</param>
 public void Update(Article newArticle)
 {
     this.Modified = DateTime.Now;
     this.Title = newArticle.Title;
     this.Username = newArticle.Username;
     this.Content = newArticle.Content;
 }
Пример #6
0
 public void DeleteArticle(Article article)
 {
     context.Articles.Remove(article);
     context.SaveChanges();
 }