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

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