コード例 #1
0
        public ActionResult AddPost(PostSubmissionVM post, string Categories)
        {
            Post postSubmit = new Post();

            postSubmit.CategoryId = int.Parse(Categories);
            postSubmit.PostBody   = post.Body;
            postSubmit.PostTitle  = post.Title;
            PostsManager manager = new PostsManager();

            Post virtaPost = postSubmit;

            virtaPost.PostId = manager.GetAll().Posts.Count() + 1;
            var regex   = new Regex(@"(?<=#)\w+");
            var matches = regex.Matches(post.Body);

            foreach (Match m in matches)
            {
                postSubmit.Tags.Add(new Tag {
                    TagName = "#" + m.Value, Posts = new List <Post> {
                        virtaPost
                    }
                });
            }

            if (User.IsInRole("Admin"))
            {
                postSubmit.IsApproved = true;
            }
            else
            {
                postSubmit.IsApproved = false;
            }
            manager.Add(postSubmit);
            return(RedirectToAction("Index", "Home"));
        }