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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = new BlogViewModel();
  26. // The blog is the main site's blog
  27. if (string.IsNullOrEmpty(username))
  28. {
  29. ViewBag.Title = "Teknik Blog - " + Config.Title;
  30. var blogs = db.Blogs.Include("User").Where(p => (p.BlogId == Constants.SERVERBLOGID));
  31. if (blogs != null && blogs.Any())
  32. {
  33. blog = blogs.First();
  34. blog.Title = Config.BlogConfig.Title;
  35. blog.Description = Config.BlogConfig.Description;
  36. }
  37. }
  38. else // A user specific blog
  39. {
  40. var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Constants.SERVERBLOGID);
  41. if (blogs.Any())
  42. {
  43. blog = blogs.First();
  44. ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title;
  45. }
  46. }
  47. // find the blog specified
  48. if (blog != null)
  49. {
  50. var foundPosts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId))
  51. : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId) &&
  52. (p.Published || p.Blog.User.Username == User.Identity.Name));
  53. model = new BlogViewModel();
  54. model.BlogId = blog.BlogId;
  55. model.UserId = blog.UserId;
  56. model.User = blog.User;
  57. model.Title = blog.Title;
  58. model.Description = blog.Description;
  59. model.HasPosts = (foundPosts != null && foundPosts.Any());
  60. return View(model);
  61. }
  62. model.Error = true;
  63. return View(model);
  64. }
  65. #region Posts
  66. // GET: Blogs/Details/5
  67. [AllowAnonymous]
  68. public ActionResult Post(string username, int id)
  69. {
  70. if (string.IsNullOrEmpty(username))
  71. {
  72. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  73. }
  74. // find the post specified
  75. var posts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.User.Username == username && p.PostId == id))
  76. : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.User.Username == username && p.PostId == id) &&
  77. (p.Published || p.Blog.User.Username == User.Identity.Name));
  78. if (posts != null && posts.Any())
  79. {
  80. PostViewModel model = new PostViewModel(posts.First());
  81. ViewBag.Title = model.Title + " - " + username + "'s Blog - " + Config.Title;
  82. return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", model);
  83. }
  84. return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", null);
  85. }
  86. [HttpPost]
  87. [AllowAnonymous]
  88. public ActionResult GetPosts(int blogID, int startPostID, int count)
  89. {
  90. var posts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => p.BlogId == blogID).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList()
  91. : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blogID) && (p.Published || p.Blog.User.Username == User.Identity.Name)
  92. ).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList();
  93. List<PostViewModel> postViews = new List<PostViewModel>();
  94. if (posts != null)
  95. {
  96. foreach (Post post in posts)
  97. {
  98. postViews.Add(new PostViewModel(post));
  99. }
  100. }
  101. return PartialView("~/Areas/Blog/Views/Blog/Posts.cshtml", postViews);
  102. }
  103. [HttpPost]
  104. [AllowAnonymous]
  105. public ActionResult GetPostTitle(int postID)
  106. {
  107. string title = string.Empty;
  108. Post post = (User.IsInRole("Admin")) ? db.Posts.Find(postID)
  109. : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.PostId == postID) && (p.Published || p.Blog.User.Username == User.Identity.Name)).First();
  110. if (post != null)
  111. {
  112. return Json(new { result = post.Title });
  113. }
  114. return Json(new { error = "No title found" });
  115. }
  116. [HttpPost]
  117. [AllowAnonymous]
  118. public ActionResult GetPostArticle(int postID)
  119. {
  120. string title = string.Empty;
  121. Post post = (User.IsInRole("Admin")) ? db.Posts.Find(postID) : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.PostId == postID) &&
  122. (p.Published || p.Blog.User.Username == User.Identity.Name)).First();
  123. if (post != null)
  124. {
  125. return Json(new { result = post.Article });
  126. }
  127. return Json(new { error = "No article found" });
  128. }
  129. [HttpPost]
  130. [ValidateAntiForgeryToken]
  131. public ActionResult CreatePost(int blogID, string title, string article)
  132. {
  133. if (ModelState.IsValid)
  134. {
  135. Post post = db.Posts.Create();
  136. post.BlogId = blogID;
  137. post.Title = title;
  138. post.Article = article;
  139. post.DatePosted = DateTime.Now;
  140. post.DatePublished = DateTime.Now;
  141. db.Posts.Add(post);
  142. db.SaveChanges();
  143. return Json(new { result = true });
  144. }
  145. return Json(new { error = "No post created" });
  146. }
  147. [HttpPost]
  148. [ValidateAntiForgeryToken]
  149. public ActionResult EditPost(int postID, string title, string article)
  150. {
  151. if (ModelState.IsValid)
  152. {
  153. Post post = db.Posts.Find(postID);
  154. if (post != null)
  155. {
  156. post.Title = title;
  157. post.Article = article;
  158. db.Entry(post).State = EntityState.Modified;
  159. db.SaveChanges();
  160. return Json(new { result = true });
  161. }
  162. }
  163. return Json(new { error = "No post found" });
  164. }
  165. [HttpPost]
  166. [ValidateAntiForgeryToken]
  167. public ActionResult PublishPost(int postID, bool publish)
  168. {
  169. if (ModelState.IsValid)
  170. {
  171. Post post = db.Posts.Find(postID);
  172. if (post != null)
  173. {
  174. post.Published = publish;
  175. if (publish)
  176. post.DatePublished = DateTime.Now;
  177. db.Entry(post).State = EntityState.Modified;
  178. db.SaveChanges();
  179. return Json(new { result = true });
  180. }
  181. }
  182. return Json(new { error = "No post found" });
  183. }
  184. [HttpPost]
  185. [ValidateAntiForgeryToken]
  186. public ActionResult DeletePost(int postID)
  187. {
  188. if (ModelState.IsValid)
  189. {
  190. Post post = db.Posts.Find(postID);
  191. if (post != null)
  192. {
  193. db.Posts.Remove(post);
  194. db.SaveChanges();
  195. return Json(new { result = true });
  196. }
  197. }
  198. return Json(new { error = "No post found" });
  199. }
  200. #endregion
  201. #region Comments
  202. [HttpPost]
  203. [AllowAnonymous]
  204. public ActionResult GetComments(int postID, int startCommentID, int count)
  205. {
  206. var comments = db.BlogComments.Include("Post").Include("Post.Blog").Include("Post.Blog.User").Where(p => (p.PostId == postID)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
  207. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  208. if (comments != null)
  209. {
  210. foreach (Comment comment in comments)
  211. {
  212. commentViews.Add(new CommentViewModel(comment));
  213. }
  214. }
  215. return PartialView("~/Areas/Blog/Views/Blog/Comments.cshtml", commentViews);
  216. }
  217. [HttpPost]
  218. [AllowAnonymous]
  219. public ActionResult GetCommentArticle(int commentID)
  220. {
  221. Comment comment = db.BlogComments.Include("Post").Include("Post.Blog").Include("Post.Blog.User").Where(p => (p.CommentId == commentID)).First();
  222. if (comment != null)
  223. {
  224. return Json(new { result = comment.Article });
  225. }
  226. return Json(new { error = "No article found" });
  227. }
  228. [HttpPost]
  229. [ValidateAntiForgeryToken]
  230. public ActionResult CreateComment(int postID, string article)
  231. {
  232. if (ModelState.IsValid)
  233. {
  234. Comment comment = db.BlogComments.Create();
  235. comment.PostId = postID;
  236. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  237. comment.Article = article;
  238. comment.DatePosted = DateTime.Now;
  239. db.BlogComments.Add(comment);
  240. db.SaveChanges();
  241. return Json(new { result = true });
  242. }
  243. return Json(new { error = "No comment created" });
  244. }
  245. [HttpPost]
  246. [ValidateAntiForgeryToken]
  247. public ActionResult EditComment(int commentID, string article)
  248. {
  249. if (ModelState.IsValid)
  250. {
  251. Comment comment = db.BlogComments.Find(commentID);
  252. if (comment != null)
  253. {
  254. comment.Article = article;
  255. db.Entry(comment).State = EntityState.Modified;
  256. db.SaveChanges();
  257. return Json(new { result = true });
  258. }
  259. }
  260. return Json(new { error = "No comment found" });
  261. }
  262. [HttpPost]
  263. [ValidateAntiForgeryToken]
  264. public ActionResult DeleteComment(int commentID)
  265. {
  266. if (ModelState.IsValid)
  267. {
  268. Comment comment = db.BlogComments.Find(commentID);
  269. if (comment != null)
  270. {
  271. db.BlogComments.Remove(comment);
  272. db.SaveChanges();
  273. return Json(new { result = true });
  274. }
  275. }
  276. return Json(new { error = "No comment found" });
  277. }
  278. #endregion
  279. }
  280. }