CollegesController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Net;
  2. using System.Web.Mvc;
  3. using CD67.FicheCollege.Entity;
  4. using CD67.FicheCollege.Factory;
  5. using CD67.FicheCollege.MVC.Models;
  6. using CD67.FicheCollege.MVC.Internal;
  7. namespace CD67.FicheCollege.MVC.Controllers
  8. {
  9. public class CollegesController : Controller
  10. {
  11. private Entities db = new Entities();
  12. // GET: Colleges
  13. public ActionResult Index(int? annee_id)
  14. {
  15. if (annee_id == null)
  16. {
  17. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  18. }
  19. AnneeFactory fact = new AnneeFactory(db);
  20. Annee annee = fact.getById(annee_id);
  21. AnneeViewModel model = new AnneeViewModel(annee, ModeAcces.Lecture);
  22. return View(model);
  23. }
  24. // GET: Colleges/Details/5
  25. public ActionResult Details(int? id)
  26. {
  27. if (id == null)
  28. {
  29. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  30. }
  31. CollegeFactory collegeFactory = new CollegeFactory(db);
  32. Entity.College college = collegeFactory.getById(id);
  33. if (college == null)
  34. {
  35. return HttpNotFound();
  36. }
  37. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Lecture);
  38. return View(model);
  39. }
  40. // GET: Colleges/Create
  41. public ActionResult Create(int? annee_id)
  42. {
  43. if (annee_id == null)
  44. {
  45. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  46. }
  47. Entity.College college = new Entity.College();
  48. college.Annee_Id = annee_id.Value;
  49. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Creation);
  50. return View("Edit", model);
  51. }
  52. // POST: Colleges/Create
  53. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  54. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  55. [HttpPost]
  56. [ValidateAntiForgeryToken]
  57. public ActionResult Create(Entity.College college)
  58. {
  59. if (ModelState.IsValid)
  60. {
  61. CollegeFactory collegeFactory = new CollegeFactory(db);
  62. collegeFactory.add(ref college);
  63. return RedirectToAction("Details", new { id = college.Id });
  64. }
  65. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Creation);
  66. return View("Edit", model);
  67. }
  68. // GET: Colleges/Edit/5
  69. public ActionResult Edit(int? id)
  70. {
  71. CollegeFactory collegeFactory = new CollegeFactory(db);
  72. Entity.College college = collegeFactory.getById(id);
  73. if (college == null)
  74. {
  75. return HttpNotFound();
  76. }
  77. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Modification);
  78. return View(model);
  79. }
  80. // POST: Colleges/Edit/5
  81. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  82. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  83. [HttpPost]
  84. [ValidateAntiForgeryToken]
  85. public ActionResult Edit(Entity.College college)
  86. {
  87. if (ModelState.IsValid)
  88. {
  89. CollegeFactory collegeFactory = new CollegeFactory(db);
  90. collegeFactory.update(ref college);
  91. return RedirectToAction("Details", new { Id = college.Id });
  92. }
  93. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Modification);
  94. return View(model);
  95. }
  96. // GET: College/Delete/5
  97. public ActionResult Delete(int? id)
  98. {
  99. if (id == null)
  100. {
  101. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  102. }
  103. CollegeFactory collegeFactory = new CollegeFactory(db);
  104. Entity.College college = collegeFactory.getById(id);
  105. if (college == null)
  106. {
  107. return HttpNotFound();
  108. }
  109. CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Suppression);
  110. return View("Details", model);
  111. }
  112. // POST: College/Delete/5
  113. [HttpPost, ActionName("Delete")]
  114. [ValidateAntiForgeryToken]
  115. public ActionResult DeleteConfirmed(int? id)
  116. {
  117. if (id == null)
  118. {
  119. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  120. }
  121. CollegeFactory collegeFactory = new CollegeFactory(db);
  122. Entity.College college = collegeFactory.getById(id);
  123. int annee_id = college.Annee_Id;
  124. collegeFactory.delete(ref college);
  125. return RedirectToAction("Index", new { annee_id = annee_id });
  126. }
  127. protected override void Dispose(bool disposing)
  128. {
  129. if (disposing)
  130. {
  131. db.Dispose();
  132. }
  133. base.Dispose(disposing);
  134. }
  135. }
  136. }