CollegesController.cs 7.1 KB

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