CollegesController.cs 5.4 KB

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