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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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
  21. [AllowAnonymous]
  22. public ActionResult Index()
  23. {
  24. ViewBag.Title = "Teknik Blog - " + Config.Title;
  25. // by default, view the teknik blog
  26. Models.Blog blog = db.Blogs.Find(Constants.SERVERBLOGID);
  27. BlogViewModel model = new BlogViewModel();
  28. model.BlogId = Constants.SERVERBLOGID;
  29. if (blog != null)
  30. {
  31. model.UserId = blog.UserId;
  32. model.User = blog.User;
  33. model.Posts = blog.Posts;
  34. }
  35. return View(model);
  36. }
  37. // GET: Blogs/Details/5
  38. [AllowAnonymous]
  39. public ActionResult Blog(string username)
  40. {
  41. Models.Blog blog = null;
  42. if (string.IsNullOrEmpty(username))
  43. {
  44. blog = db.Blogs.Find(Constants.SERVERBLOGID);
  45. }
  46. else
  47. {
  48. var blogs = db.Blogs.Include("Blog.User").Where(p => p.User.Username == username);
  49. if (blogs.Any())
  50. {
  51. blog = blogs.First();
  52. }
  53. }
  54. // find the post specified
  55. if (blog != null)
  56. {
  57. BlogViewModel model = new BlogViewModel();
  58. model.BlogId = blog.BlogId;
  59. model.UserId = blog.UserId;
  60. model.User = blog.User;
  61. model.Posts = blog.Posts;
  62. return View(model);
  63. }
  64. return new HttpStatusCodeResult(HttpStatusCode.NotFound);
  65. }
  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 post = db.Posts.Include("Blog").Include("Blog.User").Where(p => p.Blog.User.Username == username && p.PostId == id);
  76. if (post.Any())
  77. {
  78. Post curPost = post.First();
  79. PostViewModel model = new PostViewModel();
  80. model.BlogId = curPost.BlogId;
  81. model.PostId = curPost.PostId;
  82. model.DatePosted = curPost.DatePosted;
  83. model.Published = curPost.Published;
  84. model.DatePublished = curPost.DatePublished;
  85. model.Title = curPost.Title;
  86. model.Tags = curPost.Tags;
  87. model.Article = curPost.Article;
  88. return View(model);
  89. }
  90. return new HttpStatusCodeResult(HttpStatusCode.NotFound);
  91. }
  92. public ActionResult GetPosts(int blogID, int startPostID, int count)
  93. {
  94. object model = null;
  95. return PartialView("Post", model);
  96. }
  97. // GET: Blogs/Create
  98. public ActionResult Create()
  99. {
  100. return View();
  101. }
  102. // POST: Blogs/Create
  103. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  104. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  105. [HttpPost]
  106. [ValidateAntiForgeryToken]
  107. public ActionResult Create([Bind(Include = "BlogId,UserId")] Models.Blog blog)
  108. {
  109. if (ModelState.IsValid)
  110. {
  111. db.Blogs.Add(blog);
  112. db.SaveChanges();
  113. return RedirectToAction("Index");
  114. }
  115. return View(blog);
  116. }
  117. // GET: Blogs/Edit/5
  118. public ActionResult Edit(int? id)
  119. {
  120. if (id == null)
  121. {
  122. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  123. }
  124. Models.Blog blog = db.Blogs.Find(id);
  125. if (blog == null)
  126. {
  127. return HttpNotFound();
  128. }
  129. return View(blog);
  130. }
  131. // POST: Blogs/Edit/5
  132. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  133. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  134. [HttpPost]
  135. [ValidateAntiForgeryToken]
  136. public ActionResult Edit([Bind(Include = "BlogId,UserId")] Models.Blog blog)
  137. {
  138. if (ModelState.IsValid)
  139. {
  140. db.Entry(blog).State = EntityState.Modified;
  141. db.SaveChanges();
  142. return RedirectToAction("Index");
  143. }
  144. return View(blog);
  145. }
  146. // GET: Blogs/Delete/5
  147. public ActionResult Delete(int? id)
  148. {
  149. if (id == null)
  150. {
  151. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  152. }
  153. Models.Blog blog = db.Blogs.Find(id);
  154. if (blog == null)
  155. {
  156. return HttpNotFound();
  157. }
  158. return View(blog);
  159. }
  160. // POST: Blogs/Delete/5
  161. [HttpPost, ActionName("Delete")]
  162. [ValidateAntiForgeryToken]
  163. public ActionResult DeleteConfirmed(int id)
  164. {
  165. Models.Blog blog = db.Blogs.Find(id);
  166. db.Blogs.Remove(blog);
  167. db.SaveChanges();
  168. return RedirectToAction("Index");
  169. }
  170. }
  171. }