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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.ViewModels;
  10. using Teknik.Controllers;
  11. using Teknik.Helpers;
  12. using Teknik.Models;
  13. namespace Teknik.Areas.Blog.Controllers
  14. {
  15. public class BlogController : DefaultController
  16. {
  17. private TeknikEntities db = new TeknikEntities();
  18. // GET: Blogs
  19. [AllowAnonymous]
  20. public ActionResult Index()
  21. {
  22. ViewBag.Title = "Teknik Blog - " + Config.Title;
  23. // by default, view the teknik blog
  24. Models.Blog blog = db.Blogs.Find(Constants.SERVERBLOGID);
  25. BlogViewModel model = new BlogViewModel();
  26. model.BlogId = Constants.SERVERBLOGID;
  27. if (blog != null)
  28. {
  29. model.UserId = blog.UserId;
  30. model.User = blog.User;
  31. model.Posts = blog.Posts;
  32. }
  33. return View(model);
  34. }
  35. // GET: Blogs/Details/5
  36. [AllowAnonymous]
  37. public ActionResult Details(string username, int? id)
  38. {
  39. if (string.IsNullOrEmpty(username))
  40. {
  41. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  42. }
  43. if (id == null)
  44. {
  45. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  46. }
  47. Models.Blog blog = db.Blogs.Find(id);
  48. if (blog == null)
  49. {
  50. return HttpNotFound();
  51. }
  52. return View(blog);
  53. }
  54. // GET: Blogs/Create
  55. public ActionResult Create()
  56. {
  57. return View();
  58. }
  59. // POST: Blogs/Create
  60. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  61. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  62. [HttpPost]
  63. [ValidateAntiForgeryToken]
  64. public ActionResult Create([Bind(Include = "BlogId,UserId")] Models.Blog blog)
  65. {
  66. if (ModelState.IsValid)
  67. {
  68. db.Blogs.Add(blog);
  69. db.SaveChanges();
  70. return RedirectToAction("Index");
  71. }
  72. return View(blog);
  73. }
  74. // GET: Blogs/Edit/5
  75. public ActionResult Edit(int? id)
  76. {
  77. if (id == null)
  78. {
  79. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  80. }
  81. Models.Blog blog = db.Blogs.Find(id);
  82. if (blog == null)
  83. {
  84. return HttpNotFound();
  85. }
  86. return View(blog);
  87. }
  88. // POST: Blogs/Edit/5
  89. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  90. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  91. [HttpPost]
  92. [ValidateAntiForgeryToken]
  93. public ActionResult Edit([Bind(Include = "BlogId,UserId")] Models.Blog blog)
  94. {
  95. if (ModelState.IsValid)
  96. {
  97. db.Entry(blog).State = EntityState.Modified;
  98. db.SaveChanges();
  99. return RedirectToAction("Index");
  100. }
  101. return View(blog);
  102. }
  103. // GET: Blogs/Delete/5
  104. public ActionResult Delete(int? id)
  105. {
  106. if (id == null)
  107. {
  108. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  109. }
  110. Models.Blog blog = db.Blogs.Find(id);
  111. if (blog == null)
  112. {
  113. return HttpNotFound();
  114. }
  115. return View(blog);
  116. }
  117. // POST: Blogs/Delete/5
  118. [HttpPost, ActionName("Delete")]
  119. [ValidateAntiForgeryToken]
  120. public ActionResult DeleteConfirmed(int id)
  121. {
  122. Models.Blog blog = db.Blogs.Find(id);
  123. db.Blogs.Remove(blog);
  124. db.SaveChanges();
  125. return RedirectToAction("Index");
  126. }
  127. protected override void Dispose(bool disposing)
  128. {
  129. if (disposing)
  130. {
  131. db.Dispose();
  132. }
  133. base.Dispose(disposing);
  134. }
  135. }
  136. }