コード例 #1
0
        public IActionResult AddArticle([FromForm] AddArticleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var authorExists = authorService.VerifyIfAuthorExistsByEmail(model.Email);

            if (authorExists)
            {
                MatchCollection extractedHashtags = tagService.FilterHashtags(model.Tags);

                tagService.AddTags(extractedHashtags);

                string imageUploadDirectory = Path.Combine(webHostEnvironment.WebRootPath, "ArticleImages");

                articleService.AddArticle(model.Title, model.Content, model.Email, model.Image, imageUploadDirectory);

                var recentlyCreatedArticle = articleService.GetNewestAddedArticle(model.Title, model.Content, model.Email);

                foreach (var hashtag in extractedHashtags)
                {
                    var tag = tagService.GetTagByName(hashtag.ToString());

                    articleTagsService.Add(tag, recentlyCreatedArticle);
                }
            }

            var newestArticle = articleService.GetNewestAddedArticle(model.Title, model.Content, model.Email);

            return(Redirect(Url.Action("ViewArticle", "Article", new { id = newestArticle.Id })));
        }
コード例 #2
0
        public ActionResult Create(PostViewModel post)
        {
            try
            {
                // TODO: Add insert logic here
                post.AuthorId = int.Parse(Session["StudentId"].ToString());
                post.Created  = DateTime.Now;
                service.CreatePost(post);

                var        newPost    = service.GetPosts().Where(t => t.Content == post.Content).SingleOrDefault();
                TagService tagService = new TagService();
                tagService.AddTags(newPost.PostId, Request.Form["Tags"]);

                return(RedirectToAction("Index"));
            }
            catch (Exception exception)
            {
                return(View());
            }
        }