CollegesController.cs 6.1 KB

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