GroupesController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using CD67.FicheCollege.Entity;
  2. using CD67.FicheCollege.Factory;
  3. using CD67.FicheCollege.MVC.Models;
  4. using System.Net;
  5. using System.Web.Mvc;
  6. namespace CD67.FicheCollege.MVC.Controllers
  7. {
  8. public class GroupesController : Controller
  9. {
  10. private Entities db = new Entities();
  11. // GET: Groupes
  12. public ActionResult Index()
  13. {
  14. GroupeFactory fact = new GroupeFactory(db);
  15. GroupeIndexViewModel model = new GroupeIndexViewModel(fact.getAll());
  16. return View(model);
  17. }
  18. // GET: Groupe/Create
  19. public ActionResult Create()
  20. {
  21. Groupe groupe = new Groupe();
  22. GroupeViewModel model = new GroupeViewModel(groupe, ModeAcces.Creation);
  23. return View("Edit", model);
  24. }
  25. // POST: Groupe/Create
  26. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  27. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  28. [HttpPost]
  29. [ValidateAntiForgeryToken]
  30. public ActionResult Create(Groupe groupe)
  31. {
  32. if (ModelState.IsValid)
  33. {
  34. GroupeFactory fact = new GroupeFactory(db);
  35. fact.add(ref groupe);
  36. return RedirectToAction("Index");
  37. }
  38. GroupeViewModel model = new GroupeViewModel(groupe, ModeAcces.Creation);
  39. return View("Edit", model);
  40. }
  41. // GET: ActionEduAxe/Edit/5
  42. public ActionResult Edit(int? id)
  43. {
  44. if (id == null)
  45. {
  46. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  47. }
  48. GroupeFactory fact = new GroupeFactory(db);
  49. Groupe groupe = fact.getById(id.Value);
  50. if (groupe == null)
  51. {
  52. return HttpNotFound();
  53. }
  54. GroupeViewModel model = new GroupeViewModel(groupe, ModeAcces.Modification);
  55. return View(model);
  56. }
  57. // POST: ActionEduAxe/Edit/5
  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 Edit(Groupe groupe)
  63. {
  64. if (ModelState.IsValid)
  65. {
  66. GroupeFactory fact = new GroupeFactory(db);
  67. fact.update(ref groupe);
  68. return RedirectToAction("Index");
  69. }
  70. GroupeViewModel model = new GroupeViewModel(groupe, ModeAcces.Modification);
  71. return View(model);
  72. }
  73. // GET: ActionEduAxe/Delete/5
  74. public ActionResult Delete(int? id)
  75. {
  76. if (id == null)
  77. {
  78. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  79. }
  80. GroupeFactory fact = new GroupeFactory(db);
  81. Groupe groupe = fact.getById(id.Value);
  82. if (groupe == null)
  83. {
  84. return HttpNotFound();
  85. }
  86. DeleteViewModel model = new DeleteViewModel(groupe, "Groupe", groupe.Nom);
  87. return View("~/Views/Shared/_AdminDeleteWarning.cshtml", model);
  88. }
  89. // POST: ActionEduAxe/Delete/5
  90. [HttpPost, ActionName("Delete")]
  91. [ValidateAntiForgeryToken]
  92. public ActionResult DeleteConfirmed(int id)
  93. {
  94. GroupeFactory fact = new GroupeFactory(db);
  95. Groupe groupe = fact.getById(id);
  96. fact.delete(ref groupe);
  97. return RedirectToAction("Index");
  98. }
  99. protected override void Dispose(bool disposing)
  100. {
  101. if (disposing)
  102. {
  103. db.Dispose();
  104. }
  105. base.Dispose(disposing);
  106. }
  107. }
  108. }