CollegesController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using CD67.FicheCollege.Entity;
  10. using CD67.FicheCollege.Factory;
  11. namespace CD67.FicheCollege.MVC.Controllers
  12. {
  13. public class CollegesController : Controller
  14. {
  15. private Entities db = new Entities();
  16. // GET: College
  17. public ActionResult Index()
  18. {
  19. CollegeFactory collegeFactory = new CollegeFactory(db);
  20. return View(collegeFactory.getAll());
  21. }
  22. // GET: College/Details/5
  23. public ActionResult Details(int? id)
  24. {
  25. if (id == null)
  26. {
  27. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  28. }
  29. CollegeFactory collegeFactory = new CollegeFactory(db);
  30. Entity.College college = collegeFactory.getById(id);
  31. if (college == null)
  32. {
  33. return HttpNotFound();
  34. }
  35. return View(GetViewModel(college, Models.ModeAcces.Lecture));
  36. }
  37. // GET: College/Create
  38. public ActionResult Create()
  39. {
  40. Entity.College college = new Entity.College();
  41. return View("Edit", GetViewModel(college, Models.ModeAcces.Creation));
  42. }
  43. // POST: College/Create
  44. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  45. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  46. [HttpPost]
  47. [ValidateAntiForgeryToken]
  48. public ActionResult Create(Entity.College Contenu, string commentaire)
  49. {
  50. if (ModelState.IsValid)
  51. {
  52. CollegeFactory collegeFactory = new CollegeFactory(db);
  53. collegeFactory.add(ref Contenu);
  54. return RedirectToAction("Index");
  55. }
  56. return View("Edit", GetViewModel(Contenu, Models.ModeAcces.Creation));
  57. }
  58. // GET: College/Edit/5
  59. public ActionResult Edit(int? id)
  60. {
  61. CollegeFactory collegeFactory = new CollegeFactory(db);
  62. Entity.College college = collegeFactory.getById(id);
  63. if (college == null)
  64. {
  65. return HttpNotFound();
  66. }
  67. return View(GetViewModel(college, Models.ModeAcces.Modification));
  68. }
  69. // POST: College/Edit/5
  70. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  71. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  72. [HttpPost]
  73. [ValidateAntiForgeryToken]
  74. public ActionResult Edit(Entity.College Contenu)
  75. {
  76. if (ModelState.IsValid)
  77. {
  78. CollegeFactory collegeFactory = new CollegeFactory(db);
  79. collegeFactory.update(ref Contenu);
  80. return RedirectToAction("Details", new { Id = Contenu.Id });
  81. }
  82. return View(GetViewModel(Contenu, Models.ModeAcces.Modification));
  83. }
  84. // GET: College/Delete/5
  85. public ActionResult Delete(int? id)
  86. {
  87. if (id == null)
  88. {
  89. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  90. }
  91. CollegeFactory collegeFactory = new CollegeFactory(db);
  92. Entity.College college = collegeFactory.getById(id);
  93. if (college == null)
  94. {
  95. return HttpNotFound();
  96. }
  97. return View("Details", GetViewModel(college, Models.ModeAcces.Suppression));
  98. }
  99. // POST: College/Delete/5
  100. [HttpPost, ActionName("Delete")]
  101. [ValidateAntiForgeryToken]
  102. public ActionResult DeleteConfirmed(string id)
  103. {
  104. CollegeFactory collegeFactory = new CollegeFactory(db);
  105. Entity.College college = collegeFactory.getById(id);
  106. collegeFactory.delete(ref college);
  107. return RedirectToAction("Index");
  108. }
  109. private Models.CollegeViewModel GetViewModel(Entity.College entity, Models.ModeAcces Acces)
  110. {
  111. Dictionary<string, SelectList> listes = new Dictionary<string, SelectList>();
  112. //On prépare les listes de choix en création ou modification
  113. if (Acces == Models.ModeAcces.Creation || Acces == Models.ModeAcces.Modification)
  114. {
  115. TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
  116. SelectList listeTypeCollege = new SelectList(typeCollegeFactory.getAll("Ordre"), "Id", "Libelle", entity.TypeCollege_Id);
  117. listes.Add("TypeCollege_Id", listeTypeCollege);
  118. SelectList listeCodesPostaux = new SelectList(Internal.Referentiel.GetCodesPostaux(entity.Commune_Insee), entity.Code_Postal);
  119. listes.Add("Code_Postal", listeCodesPostaux);
  120. }
  121. return new Models.CollegeViewModel()
  122. {
  123. College_Id = entity.Id,
  124. College_Libelle = entity.Libelle,
  125. Contenu = entity,
  126. Acces = Acces,
  127. Listes = listes
  128. };
  129. }
  130. protected override void Dispose(bool disposing)
  131. {
  132. if (disposing)
  133. {
  134. db.Dispose();
  135. }
  136. base.Dispose(disposing);
  137. }
  138. }
  139. }