VikingController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Net;
  2. using System.Web.Mvc;
  3. using CD67.ModeleMVC.Entity;
  4. using CD67.ModeleMVC.Factory;
  5. using System.Collections.Generic;
  6. using System;
  7. using System.Linq;
  8. using CD67.ModeleMVC.MVC.Internal;
  9. using CD67.ModeleMVC.Solr;
  10. namespace CD67.ModeleMVC.MVC.Controllers
  11. {
  12. public class VikingController : Controller
  13. {
  14. private ModeleMVCEntities db = new ModeleMVCEntities();
  15. // GET: Viking
  16. public ActionResult Index()
  17. {
  18. VikingFactory vikingsFactory = new VikingFactory(db);
  19. return View(vikingsFactory.getAll());
  20. }
  21. // GET: Viking/Details/5
  22. public ActionResult Details(int? id)
  23. {
  24. if (id == null)
  25. {
  26. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  27. }
  28. VikingFactory vikingsFactory = new VikingFactory(db);
  29. Viking viking = vikingsFactory.getById(id.Value);
  30. if (viking == null)
  31. {
  32. return HttpNotFound();
  33. }
  34. ViewBag.id = id;
  35. return View(viking);
  36. }
  37. // GET: Viking/Create
  38. public ActionResult Create()
  39. {
  40. ViewBag.ListeTypesViking = getListeTypesViking(string.Empty);
  41. Viking viking = new Viking();
  42. FillSelect(viking);
  43. return View(viking);
  44. }
  45. // POST: Viking/Create
  46. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  47. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  48. [HttpPost]
  49. [ValidateAntiForgeryToken]
  50. public ActionResult Create([Bind(Include = "Id,Nom,TypeVikingId,CasqueCornu,NombreVictoires,Description")] Viking viking)
  51. {
  52. if (ModelState.IsValid)
  53. {
  54. VikingFactory vikingsFactory = new VikingFactory(db);
  55. viking.DateEdition = DateTime.Now;
  56. viking.DateCreation = DateTime.Now;
  57. vikingsFactory.add(ref viking);
  58. db.Entry(viking).Reference(i => i.TypeViking).Load();
  59. IndexeurSolr.addViking(viking);
  60. // Ajout d'un message flash
  61. this.Success("Viking créé avec succès.");
  62. return RedirectToAction("Index");
  63. }
  64. FillSelect(viking);
  65. return View(viking);
  66. }
  67. // GET: Viking/Edit/5
  68. public ActionResult Edit(int? id)
  69. {
  70. if (id == null)
  71. {
  72. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  73. }
  74. ViewBag.ListeTypesViking = getListeTypesViking(string.Empty);
  75. VikingFactory vikingsFactory = new VikingFactory(db);
  76. Viking viking = vikingsFactory.getById(id.Value);
  77. if (viking == null)
  78. {
  79. return HttpNotFound();
  80. }
  81. FillSelect(viking);
  82. return View(viking);
  83. }
  84. // POST: Viking/Edit/5
  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 Edit([Bind(Include = "Id,Nom,TypeVikingId,CasqueCornu,NombreVictoires,Description,DateCreation")] Viking viking)
  90. {
  91. if (ModelState.IsValid)
  92. {
  93. VikingFactory vikingsFactory = new VikingFactory(db);
  94. viking.DateEdition = DateTime.Now;
  95. vikingsFactory.update(ref viking);
  96. db.Entry(viking).Reference(i => i.TypeViking).Load();
  97. IndexeurSolr.addViking(viking);
  98. // Ajout d'un message flash
  99. this.Success("Viking édité avec succès.");
  100. return RedirectToAction("Index");
  101. }
  102. FillSelect(viking);
  103. return View(viking);
  104. }
  105. // GET: Viking/Delete/5
  106. public ActionResult Delete(int? id)
  107. {
  108. if (id == null)
  109. {
  110. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  111. }
  112. VikingFactory vikingsFactory = new VikingFactory(db);
  113. Viking viking = vikingsFactory.getById(id.Value);
  114. if (viking == null)
  115. {
  116. return HttpNotFound();
  117. }
  118. return View(viking);
  119. }
  120. // POST: Viking/Delete/5
  121. [HttpPost, ActionName("Delete")]
  122. [ValidateAntiForgeryToken]
  123. public ActionResult DeleteConfirmed(int id)
  124. {
  125. VikingFactory vikingsFactory = new VikingFactory(db);
  126. Viking viking = vikingsFactory.getById(id);
  127. vikingsFactory.delete(ref viking);
  128. IndexeurSolr.delete(viking.Id.ToString());
  129. // Ajout d'un message flash
  130. this.Success("Viking supprimé avec succès.");
  131. return RedirectToAction("Index");
  132. }
  133. protected override void Dispose(bool disposing)
  134. {
  135. if (disposing)
  136. {
  137. db.Dispose();
  138. }
  139. base.Dispose(disposing);
  140. }
  141. private void FillSelect(Viking viking)
  142. {
  143. TypeVikingFactory typeVikingFactory = new TypeVikingFactory(db);
  144. ViewBag.ID_TYPE = new SelectList(typeVikingFactory.getAll(), "Id", "TypeVikingId", viking.TypeViking);
  145. //Chargement d'une liste vide à la création
  146. TypeVikingFactory sousTypeFactory = new TypeVikingFactory(db);
  147. ViewBag.ID_SOUS_TYPE = new SelectList(sousTypeFactory.getManyBy(viking.TypeVikingId).OrderBy(i => i.Value), "Key", "Value", viking.TypeViking);
  148. }
  149. //Mise à jour Ajax de la liste imbriquée
  150. public JsonResult listeSousType(int Id, int? defaultSelected)
  151. {
  152. TypeVikingFactory sousTypeFactory = new TypeVikingFactory(db);
  153. List<SelectListItem> listType = new List<SelectListItem>();
  154. Dictionary<int, string> infoType = sousTypeFactory.getManyBy(Id); //le dictionnaire doit devenir une liste de types
  155. List<Tuple<int, string>> listeType = new List<Tuple<int, string>>();
  156. foreach (var type in infoType)
  157. listeType.Add(new Tuple<int, string>(int.Parse(type.Key.ToString()), type.Value));
  158. foreach (Tuple<int, string> liste in listeType)
  159. listType.Add(new SelectListItem { Text = liste.Item2, Value = liste.Item1.ToString() });
  160. return Json(new SelectList(listType, "Value", "Text", defaultSelected), JsonRequestBehavior.AllowGet);
  161. }
  162. public SelectList getListeTypesViking(string defaultSelected)
  163. {
  164. TypeVikingFactory typeVikingfactory = new TypeVikingFactory(db);
  165. List<TypeViking> getlist = typeVikingfactory.getAll().ToList();
  166. List<SelectListItem> listTypeViking = new List<SelectListItem>();
  167. foreach (TypeViking item in getlist)
  168. {
  169. listTypeViking.Add(new SelectListItem { Text = item.Id + " - " + item.Libelle, Value = item.Id.ToString() });
  170. }
  171. if (defaultSelected == string.Empty)
  172. defaultSelected = "";
  173. return new SelectList(listTypeViking, "Value", "Text", defaultSelected);
  174. }
  175. }
  176. }