CollegesController.cs 5.5 KB

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