using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using CD67.FicheCollege.Entity; using CD67.FicheCollege.Factory; namespace CD67.FicheCollege.MVC.Controllers { public class CollegesController : Controller { private Entities db = new Entities(); // GET: College public ActionResult Index() { CollegeFactory collegeFactory = new CollegeFactory(db); return View(collegeFactory.getAll()); } // GET: College/Details/5 public ActionResult Details(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } return View(GetViewModel(college, Models.ModeAcces.Lecture)); } // GET: College/Create public ActionResult Create() { Entity.College college = new Entity.College(); return View("Edit", GetViewModel(college, Models.ModeAcces.Creation)); } // POST: College/Create // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Entity.College Contenu) { if (ModelState.IsValid) { CollegeFactory collegeFactory = new CollegeFactory(db); collegeFactory.add(ref Contenu); return RedirectToAction("Index"); } return View("Edit", GetViewModel(Contenu, Models.ModeAcces.Creation)); } // GET: College/Edit/5 public ActionResult Edit(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } return View(GetViewModel(college, Models.ModeAcces.Modification)); } // POST: College/Edit/5 // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Entity.College Contenu) { if (ModelState.IsValid) { CollegeFactory collegeFactory = new CollegeFactory(db); collegeFactory.update(ref Contenu); return RedirectToAction("Details", new { Id = Contenu.Id }); } return View(GetViewModel(Contenu, Models.ModeAcces.Modification)); } // GET: College/Delete/5 public ActionResult Delete(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } return View("Details", GetViewModel(college, Models.ModeAcces.Suppression)); } // POST: College/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(string id) { CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); collegeFactory.delete(ref college); return RedirectToAction("Index"); } private Models.CollegeViewModel GetViewModel(Entity.College entity, Models.ModeAcces Acces) { Dictionary listes = new Dictionary(); //On prépare les listes de choix en création ou modification if (Acces == Models.ModeAcces.Creation || Acces == Models.ModeAcces.Modification) { TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db); SelectList listeTypeCollege = new SelectList(typeCollegeFactory.getAll("Ordre"), "Id", "Libelle", entity.TypeCollege_Id); listes.Add("TypeCollege_Id", listeTypeCollege); SelectList listeCodesPostaux = new SelectList(Internal.Referentiel.GetCodesPostaux(entity.Commune_Insee), entity.Code_Postal); listes.Add("Code_Postal", listeCodesPostaux); } return new Models.CollegeViewModel() { College_Id = entity.Id, College_Libelle = entity.Libelle, Contenu = entity, Acces = Acces, Listes = listes }; } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }