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.1KB

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