CollegesController.cs 5.9 KB

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