コード例 #1
0
 public ActionResult AddNewPost(PostViewModel model)
 {
     var post = new Post
     {
         Id = model.ID,
         Body = model.Body,
         Meta = model.Meta,
         PostedOn = DateTime.Now,
         Published = true,
         ShortDescription = model.ShortDescription,
         Title = model.Title,
         UrlSeo = model.UrlSeo
     };
     _blogRepository.AddNewPost(post);
     return RedirectToAction("EditPost", "Blog", new { slug = model.UrlSeo });
 }
コード例 #2
0
        public ActionResult Comments(PostViewModel model, Post post, string sortOrder)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.DateSortParm = string.IsNullOrEmpty(sortOrder) ? "date_asc" : "";
            ViewBag.BestSortParm = sortOrder == "Best" ? "best_desc" : "Best";

            var postComments = _blogRepository.GetPostComments(post).OrderByDescending(d => d.DateTime).ToList();

            foreach (var comment in postComments)
            {
                var likes = LikeDislikeCount("commentlike", comment.Id);
                var dislikes = LikeDislikeCount("commentdislike", comment.Id);
                comment.NetLikeCount = likes - dislikes;
                if (comment.Replies != null) comment.Replies.Clear();
                List<CommentViewModel> replies = _blogRepository.GetParentReplies(comment);
                foreach (var reply in replies)
                {
                    var rep = _blogRepository.GetReplyById(reply.Id);
                    comment.Replies.Add(rep);
                }
            }


            switch (sortOrder)
            {
                case "date_asc":
                    postComments = postComments.OrderBy(x => x.DateTime).ToList();
                    ViewBag.DateSortLink = "active";
                    break;
                case "Best":
                    postComments = postComments.OrderByDescending(x => x.NetLikeCount).ToList();
                    ViewBag.BestSortLink = "active";
                    break;
                case "best_desc":
                    postComments = postComments.OrderBy(x => x.NetLikeCount).ToList();
                    ViewBag.BestSortLink = "active";
                    break;
                default:
                    postComments = postComments.OrderByDescending(x => x.DateTime).ToList();
                    ViewBag.DateSortLink = "active";
                    break;
            }

            model.UrlSeo = post.UrlSeo;
            model.Comments = postComments;
            return PartialView(model);
        }
コード例 #3
0
 public ActionResult DeletePost(PostViewModel model, string postid)
 {
     model.ID = postid;
     return View(model);
 }
コード例 #4
0
 public ActionResult AddNewPost()
 {
     List<int> numlist = new List<int>();
     int num = 0;
     var posts = _blogRepository.GetPosts();
     if (posts.Count != 0)
     {
         foreach (var post in posts)
         {
             var postid = post.Id;
             Int32.TryParse(postid, out num);
             numlist.Add(num);
         }
         numlist.Sort();
         num = numlist.Last();
         num++;
     }
     else
     {
         num = 1;
     }
     var newid = num.ToString();
     PostViewModel model = new PostViewModel();
     model.ID = newid;
     return View(model);
 }
コード例 #5
0
 public ActionResult AddTagToPost(PostViewModel model)
 {
     var post = _blogRepository.GetPostById(model.ID);
     var postTags = _blogRepository.GetPostTags(post);
     List<string> pTagIds = new List<string>();
     foreach (var pTag in postTags)
     {
         pTagIds.Add(pTag.Id);
     }
     var newTags = model.Tags.Where(x => x.Checked == true).ToList();
     List<string> nTagIds = new List<string>();
     foreach (var pTag in newTags)
     {
         nTagIds.Add(pTag.Id);
     }
     if (!pTagIds.SequenceEqual(nTagIds))
     {
         foreach (var pTag in postTags)
         {
             _blogRepository.RemovePostTags(model.ID, pTag.Id);
         }
         foreach (var tag in model.Tags)
         {
             PostTag postTag = new PostTag();
             if (tag.Checked == true)
             {
                 postTag.PostId = model.ID;
                 postTag.TagId = tag.Id;
                 postTag.Checked = true;
                 _blogRepository.AddPostTags(postTag);
             }
         }
         _blogRepository.Save();
     }
     return RedirectToAction("EditPost", new { slug = post.UrlSeo });
 }
コード例 #6
0
 public ActionResult AddNewTag(string postid, bool callfrompost)
 {
     if (postid != null && callfrompost)
     {
         PostViewModel model = new PostViewModel();
         model.ID = postid;
         return View(model);
     }
     else
     {
         return View();
     }
 }
コード例 #7
0
 public ActionResult AddCategoryToPost(PostViewModel model)
 {
     var post = _blogRepository.GetPostById(model.ID);
     var postCats = _blogRepository.GetPostCategories(post);
     List<string> pCatIds = new List<string>();
     foreach (var pCat in postCats)
     {
         pCatIds.Add(pCat.Id);
     }
     var newCats = model.Categories.Where(x => x.Checked == true).ToList();
     List<string> nCatIds = new List<string>();
     foreach (var pCat in newCats)
     {
         nCatIds.Add(pCat.Id);
     }
     if (!pCatIds.SequenceEqual(nCatIds))
     {
         foreach (var pCat in postCats)
         {
             _blogRepository.RemovePostCategories(model.ID, pCat.Id);
         }
         foreach (var cat in model.Categories)
         {
             PostCategory postCategory = new PostCategory();
             if (cat.Checked == true)
             {
                 postCategory.PostId = model.ID;
                 postCategory.CategoryId = cat.Id;
                 postCategory.Checked = true;
                 _blogRepository.AddPostCategories(postCategory);
             }
         }
         _blogRepository.Save();
     }
     return RedirectToAction("EditPost", new { slug = post.UrlSeo });
 }
コード例 #8
0
 public ActionResult AddTagToPost(string postid)
 {
     PostViewModel model = new PostViewModel();
     model.ID = postid;
     model.Tags = _blogRepository.GetTags();
     return View(model);
 }
コード例 #9
0
 public ActionResult AddCategoryToPost(string postid)
 {
     PostViewModel model = new PostViewModel();
     model.ID = postid;
     model.Categories = _blogRepository.GetCategories();
     return View(model);
 }
コード例 #10
0
 public ActionResult AddVideoToPost(string postid, string slug)
 {
     PostViewModel model = new PostViewModel();
     model.ID = postid;
     model.UrlSeo = slug;
     return View(model);
 }
コード例 #11
0
        public ActionResult EditPost(PostViewModel model)
        {
            var post = _blogRepository.GetPostById(model.ID);
            post.Body = model.Body;
            post.Title = model.Title;
            post.Meta = model.Meta;
            post.UrlSeo = model.UrlSeo;
            post.ShortDescription = model.ShortDescription;
            post.Modified = DateTime.Now;
            _blogRepository.Save();

            return RedirectToAction("Post", new { slug = model.UrlSeo });
        }
コード例 #12
0
 public ActionResult Post(string sortOrder, string slug)
 {
     PostViewModel model = new PostViewModel();
     var posts = GetPosts();
     var postid = _blogRepository.GetPostIdBySlug(slug);
     var post = _blogRepository.GetPostById(postid);
     var videos = GetPostVideos(post);
     var firstPostId = posts.OrderBy(i => i.PostedOn).First().Id;
     var lastPostId = posts.OrderBy(i => i.PostedOn).Last().Id;
     var nextId = posts.OrderBy(i => i.PostedOn).SkipWhile(i => i.Id != postid).Skip(1).Select(i => i.Id).FirstOrDefault();
     var previousId = posts.OrderBy(i => i.PostedOn).TakeWhile(i => i.Id != postid).Select(i => i.Id).LastOrDefault();
     model.FirstPostId = firstPostId;
     model.LastPostId = lastPostId;
     model.PreviousPostSlug = posts.Where(x => x.Id == previousId).Select(x => x.UrlSeo).FirstOrDefault();
     model.NextPostSlug = posts.Where(x => x.Id == nextId).Select(x => x.UrlSeo).FirstOrDefault();
     model.ID = post.Id;
     model.PostCount = posts.Count();
     model.UrlSeo = post.UrlSeo;
     model.Videos = videos;
     model.Title = post.Title;
     model.Body = post.Body;
     model.PostLikes = _blogRepository.LikeDislikeCount("postlike", post.Id);
     model.PostDislikes = _blogRepository.LikeDislikeCount("postdislike", post.Id);
     Comments(model, post, sortOrder);
     return View(model);
 }
コード例 #13
0
 public PostViewModel CreatePostViewModel(string slug)
 {
     PostViewModel model = new PostViewModel();
     var postid = _blogRepository.GetPostIdBySlug(slug);
     var post = _blogRepository.GetPostById(postid);
     model.ID = postid;
     model.Title = post.Title;
     model.Body = post.Body;
     model.Meta = post.Meta;
     model.UrlSeo = post.UrlSeo;
     model.Videos = _blogRepository.GetPostVideos(post).ToList();
     model.PostCategories = _blogRepository.GetPostCategories(post).ToList();
     model.PostTags = _blogRepository.GetPostTags(post).ToList();
     model.ShortDescription = post.ShortDescription;
     return model;
 }