The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BlogController.cs 15KB

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