The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BlogController.cs 11KB

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