The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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 15KB

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