CollegesController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, db, 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, db, 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. AnneeFactory fact = new AnneeFactory(db);
  50. college.Annee = fact.getById(annee_id);
  51. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  52. return View("Edit", model);
  53. }
  54. // POST: Colleges/Create
  55. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  56. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  57. [HttpPost]
  58. [ValidateAntiForgeryToken]
  59. public ActionResult Create(Entity.College college)
  60. {
  61. if (ModelState.IsValid)
  62. {
  63. CollegeFactory collegeFactory = new CollegeFactory(db);
  64. collegeFactory.add(ref college);
  65. return RedirectToAction("Details", new { id = college.Id });
  66. }
  67. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
  68. return View("Edit", model);
  69. }
  70. // GET: Colleges/Edit/5
  71. public ActionResult Edit(int? id)
  72. {
  73. CollegeFactory collegeFactory = new CollegeFactory(db);
  74. Entity.College college = collegeFactory.getById(id);
  75. if (college == null)
  76. {
  77. return HttpNotFound();
  78. }
  79. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  80. return View(model);
  81. }
  82. // POST: Colleges/Edit/5
  83. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  84. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  85. [HttpPost]
  86. [ValidateAntiForgeryToken]
  87. public ActionResult Edit(Entity.College college)
  88. {
  89. if (ModelState.IsValid)
  90. {
  91. CollegeFactory collegeFactory = new CollegeFactory(db);
  92. collegeFactory.update(ref college);
  93. return RedirectToAction("Details", new { Id = college.Id });
  94. }
  95. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
  96. return View(model);
  97. }
  98. // GET: College/Delete/5
  99. public ActionResult Delete(int? id)
  100. {
  101. if (id == null)
  102. {
  103. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  104. }
  105. CollegeFactory collegeFactory = new CollegeFactory(db);
  106. Entity.College college = collegeFactory.getById(id);
  107. if (college == null)
  108. {
  109. return HttpNotFound();
  110. }
  111. CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Suppression);
  112. return View("Details", model);
  113. }
  114. // POST: College/Delete/5
  115. [HttpPost, ActionName("Delete")]
  116. [ValidateAntiForgeryToken]
  117. public ActionResult DeleteConfirmed(int? id)
  118. {
  119. if (id == null)
  120. {
  121. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  122. }
  123. CollegeFactory collegeFactory = new CollegeFactory(db);
  124. Entity.College college = collegeFactory.getById(id);
  125. int annee_id = college.Annee_Id;
  126. collegeFactory.delete(ref college);
  127. return RedirectToAction("Index", new { annee_id = annee_id });
  128. }
  129. protected override void Dispose(bool disposing)
  130. {
  131. if (disposing)
  132. {
  133. db.Dispose();
  134. }
  135. base.Dispose(disposing);
  136. }
  137. }
  138. }