The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlogController.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Teknik.Areas.Blog.Models;
  10. using Teknik.Areas.Blog.ViewModels;
  11. using Teknik.Areas.Profile.Models;
  12. using Teknik.Controllers;
  13. using Teknik.Helpers;
  14. using Teknik.Models;
  15. namespace Teknik.Areas.Blog.Controllers
  16. {
  17. public class BlogController : DefaultController
  18. {
  19. private TeknikEntities db = new TeknikEntities();
  20. // GET: Blogs/Details/5
  21. [AllowAnonymous]
  22. public ActionResult Blog(string username)
  23. {
  24. Models.Blog blog = null;
  25. BlogViewModel model = null;
  26. if (string.IsNullOrEmpty(username))
  27. {
  28. ViewBag.Title = "Teknik Blog - " + Config.Title;
  29. blog = db.Blogs.Find(Constants.SERVERBLOGID);
  30. }
  31. else
  32. {
  33. var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username);
  34. if (blogs.Any())
  35. {
  36. blog = blogs.First();
  37. ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title;
  38. }
  39. }
  40. // find the post specified
  41. if (blog != null)
  42. {
  43. var foundPosts = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.BlogId == blog.BlogId) &&
  44. (p.Published || p.Blog.User.Username == User.Identity.Name)
  45. ).OrderByDescending(p => p.DatePosted);
  46. model = new BlogViewModel();
  47. model.BlogId = blog.BlogId;
  48. model.UserId = blog.UserId;
  49. model.User = blog.User;
  50. model.Title = blog.Title;
  51. model.Description = blog.Description;
  52. model.Posts = (foundPosts != null && foundPosts.Any()) ? foundPosts.ToList() : null;
  53. return View(model);
  54. }
  55. return View(model);
  56. }
  57. // GET: Blogs/Details/5
  58. [AllowAnonymous]
  59. public ActionResult Post(string username, int id)
  60. {
  61. if (string.IsNullOrEmpty(username))
  62. {
  63. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  64. }
  65. // find the post specified
  66. Post post = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.User.Username == username && p.PostId == id) &&
  67. (p.Published || p.Blog.User.Username == User.Identity.Name)
  68. ).First();
  69. if (post != null)
  70. {
  71. PostViewModel model = new PostViewModel(post);
  72. ViewBag.Title = model.Title + " - " + username + "'s Blog - " + Config.Title;
  73. return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", model);
  74. }
  75. return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", null);
  76. }
  77. [HttpPost]
  78. [AllowAnonymous]
  79. public ActionResult GetPosts(int blogID, int startPostID, int count)
  80. {
  81. var posts = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blogID && p.PostId > startPostID) &&
  82. (p.Published || p.Blog.User.Username == User.Identity.Name)
  83. ).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList();
  84. List<PostViewModel> postViews = new List<PostViewModel>();
  85. if (posts != null)
  86. {
  87. foreach (Post post in posts)
  88. {
  89. postViews.Add(new PostViewModel(post));
  90. }
  91. }
  92. return PartialView("~/Areas/Blog/Views/Blog/Posts.cshtml", postViews);
  93. }
  94. [HttpPost]
  95. [AllowAnonymous]
  96. public ActionResult GetPostTitle(int postID)
  97. {
  98. string title = string.Empty;
  99. Post post = (User.IsInRole("Admin")) ? db.Posts.Find(postID) : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.PostId == postID) &&
  100. (p.Published || p.Blog.User.Username == User.Identity.Name)).First();
  101. if (post != null)
  102. {
  103. return Json(new { result = post.Title });
  104. }
  105. return Json(new { error = "No title found" });
  106. }
  107. [HttpPost]
  108. [AllowAnonymous]
  109. public ActionResult GetPostArticle(int postID)
  110. {
  111. string title = string.Empty;
  112. Post post = (User.IsInRole("Admin")) ? db.Posts.Find(postID) : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.PostId == postID) &&
  113. (p.Published || p.Blog.User.Username == User.Identity.Name)).First();
  114. if (post != null)
  115. {
  116. return Json(new { result = post.Article });
  117. }
  118. return Json(new { error = "No article found" });
  119. }
  120. [HttpPost]
  121. [ValidateAntiForgeryToken]
  122. public ActionResult CreatePost(int blogID, string title, string article)
  123. {
  124. if (ModelState.IsValid)
  125. {
  126. Post post = db.Posts.Create();
  127. post.BlogId = blogID;
  128. post.Title = title;
  129. post.Article = article;
  130. post.DatePosted = DateTime.Now;
  131. post.DatePublished = DateTime.Now;
  132. db.Posts.Add(post);
  133. db.SaveChanges();
  134. return Json(new { result = true });
  135. }
  136. return Json(new { error = "No post found" });
  137. }
  138. [HttpPost]
  139. [ValidateAntiForgeryToken]
  140. public ActionResult EditPost(int postID, string title, string article)
  141. {
  142. if (ModelState.IsValid)
  143. {
  144. Post post = db.Posts.Find(postID);
  145. if (post != null)
  146. {
  147. post.Title = title;
  148. post.Article = article;
  149. db.Entry(post).State = EntityState.Modified;
  150. db.SaveChanges();
  151. return Json(new { result = true });
  152. }
  153. }
  154. return Json(new { error = "No post found" });
  155. }
  156. [HttpPost]
  157. [ValidateAntiForgeryToken]
  158. public ActionResult PublishPost(int postID, bool publish)
  159. {
  160. if (ModelState.IsValid)
  161. {
  162. Post post = db.Posts.Find(postID);
  163. if (post != null)
  164. {
  165. post.Published = publish;
  166. if (publish)
  167. post.DatePublished = DateTime.Now;
  168. db.Entry(post).State = EntityState.Modified;
  169. db.SaveChanges();
  170. return Json(new { result = true });
  171. }
  172. }
  173. return Json(new { error = "No post found" });
  174. }
  175. [HttpPost]
  176. [ValidateAntiForgeryToken]
  177. public ActionResult DeletePost(int postID)
  178. {
  179. if (ModelState.IsValid)
  180. {
  181. Post post = db.Posts.Find(postID);
  182. if (post != null)
  183. {
  184. db.Posts.Remove(post);
  185. db.SaveChanges();
  186. return Json(new { result = true });
  187. }
  188. }
  189. return Json(new { error = "No post found" });
  190. }
  191. }
  192. }