AnneesController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Web.Mvc;
  2. using System.Net;
  3. using CD67.FicheCollege.Factory;
  4. using CD67.FicheCollege.Entity;
  5. using CD67.FicheCollege.MVC.Models;
  6. using System.Collections.Generic;
  7. using System.Web;
  8. using System;
  9. namespace CD67.FicheCollege.MVC.Controllers
  10. {
  11. public class AnneesController : Controller
  12. {
  13. private Entities db = new Entities();
  14. // GET: Annees
  15. public ActionResult Index()
  16. {
  17. AnneeFactory fact = new AnneeFactory(db);
  18. AnneeIndexViewModel model = new AnneeIndexViewModel(fact.getAll());
  19. return View(model);
  20. }
  21. // GET: Home
  22. public ActionResult Details(int? id)
  23. {
  24. if (id == null)
  25. {
  26. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  27. }
  28. HttpCookie cookie = new HttpCookie("last_selected_year");
  29. cookie.Value = id.ToString();
  30. cookie.Expires = DateTime.Now.AddMonths(1);
  31. Response.SetCookie(cookie);
  32. AnneeFactory anneeFactory = new AnneeFactory(db);
  33. Entity.Annee annee = anneeFactory.getById(id);
  34. if (annee == null)
  35. {
  36. return HttpNotFound();
  37. }
  38. AnneeViewModel model = new AnneeViewModel(annee, db);
  39. return View(model);
  40. }
  41. public ActionResult Previous(int? id)
  42. {
  43. if (id == null)
  44. {
  45. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  46. }
  47. AnneeFactory anneeFactory = new AnneeFactory(db);
  48. Entity.Annee annee = anneeFactory.getById(id);
  49. if (annee == null)
  50. {
  51. return HttpNotFound();
  52. }
  53. AnneeViewModel model = new AnneeViewModel(annee, db);
  54. return View(model);
  55. }
  56. public ActionResult Next(int? id)
  57. {
  58. if (id == null)
  59. {
  60. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  61. }
  62. AnneeFactory anneeFactory = new AnneeFactory(db);
  63. Entity.Annee annee = anneeFactory.getById(id);
  64. if (annee == null)
  65. {
  66. return HttpNotFound();
  67. }
  68. AnneeViewModel model = new AnneeViewModel(annee, db);
  69. return View(model);
  70. }
  71. [HttpGet]
  72. public JsonResult GetList()
  73. {
  74. AnneeFactory fact = new AnneeFactory(db);
  75. List<Annee> annees = new List<Annee>();
  76. int max_id = 0;
  77. foreach (Annee annee in fact.getManyBy(a => a.Colleges.Count > 0))
  78. {
  79. annees.Add(annee.flat());
  80. if (annee.Id > max_id) { max_id = annee.Id; }
  81. }
  82. annees.Add(fact.getById(max_id + 1).flat());
  83. annees.Add(fact.getById(max_id + 2).flat());
  84. JsonResult res = Json(annees);
  85. res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
  86. return res;
  87. }
  88. }
  89. }