CollegesController.cs 5.4 KB

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