public ActionResult Delete(int id, FormCollection collection) { using (db_gmEntities dbModel = new db_gmEntities()) { t_plante plant = dbModel.t_plante.Where(x => x.plaId == id).FirstOrDefault(); dbModel.t_plante.Remove(plant); dbModel.SaveChanges(); } return(RedirectToAction("Index")); }
// GET: Plant/Edit/5 public ActionResult Edit(int id) { t_plante plant = new t_plante(); using (db_gmEntities dbModel = new db_gmEntities()) { if (dbModel.t_plante.Any(x => x.plaId == id)) { plant = dbModel.t_plante.Where(x => x.plaId == id).FirstOrDefault(); } else { return(RedirectToAction("Index")); } } return(View(plant)); }
public ActionResult Edit(t_plante plant) { if (string.IsNullOrEmpty(plant.plaName)) { ModelState.AddModelError("plaName", "Un nom de plante est requis"); } if (string.IsNullOrEmpty(plant.plaType)) { ModelState.AddModelError("plaType", "Un type de plante est requis"); } if (plant.plaType == plant.plaName) { ModelState.AddModelError("plaType", "Le type de la plante ne peut pas être son nom"); } using (db_gmEntities dbModel = new db_gmEntities()) { if (dbModel.t_plante.Where(x => x.plaId != plant.plaId).Any(x => x.plaName == plant.plaName)) { ModelState.AddModelError("plaName", "Cette plante existe déjà"); } } if (ModelState.IsValid) { using (db_gmEntities dbModel = new db_gmEntities()) { dbModel.Entry(plant).State = System.Data.EntityState.Modified; dbModel.SaveChanges(); } return(RedirectToAction("Index")); } else { return(View()); } }
public ActionResult Create(t_jardin garden) { // Variables for FK ID long gardenId; long tileId; // Variable protocol string tileProtocol; // Save garden using (db_gmEntities4 DbModel = new db_gmEntities4()) { DbModel.t_jardin.Add(garden); DbModel.SaveChanges(); gardenId = garden.jarId; } // Save tiles of the garden string[] strList = garden.jarDisposition.Split('X'); foreach (string s in strList) { string[] tabStr = s.Split(','); t_tile newTile = new t_tile(); newTile.tilDate = DateTime.Now; newTile.jarId = gardenId; newTile.tilPosition = tabStr[0]; newTile.tilPlant = tabStr[1]; using (db_gmEntities4 DbModel = new db_gmEntities4()) { DbModel.t_tile.Add(newTile); DbModel.SaveChanges(); tileId = newTile.tilId; } // Save protocols of tile // Get the plant protocol using (db_gmEntities dbModel = new db_gmEntities()) { t_plante plant = dbModel.t_plante.Where(x => x.plaName == newTile.tilPlant).FirstOrDefault(); tileProtocol = plant.plaProtocol; } // Make protocol objects from protocols string int proNumber = tileProtocol.Split(')').Length - 1; var proTab = new String[proNumber]; for (int i = 0; i < proTab.Length; i++) { int start = tileProtocol.IndexOf((i + 1 + ")").ToString()); int end; if (i == proTab.Length - 1) { end = tileProtocol.Length; } else { end = tileProtocol.IndexOf((i + 2 + ")").ToString()); } string proString = tileProtocol.Substring(start + 2, end - start - 2); proTab[i] = proString; } //Months array: String[] months = { "janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre" }; //Today month CultureInfo culture = new CultureInfo("fr-Fr"); string todayMonth = culture.DateTimeFormat.GetMonthName(DateTime.Now.Month).ToLower(); //Tasks array: String[] tasks; using (db_gmEntities1 DbModel = new db_gmEntities1()) { var taskName = from m in DbModel.t_tache select m.tacName.ToLower(); tasks = taskName.ToArray(); } //Time unit array: String[] timeUnit = { "jours", "semaines", "mois" }; //first protocol t_protocole newProt = new t_protocole(); foreach (string sMonth in months) { if (proTab[0].Contains(sMonth)) { newProt.proIsDone = false; newProt.tilId = tileId; newProt.proDateTask = DateTime.Now; newProt.proToDo = false; } } foreach (string sTask in tasks) { if (proTab[0].Contains(sTask)) { newProt.proTask = sTask; } } using (db_gmEntities4 DbModel = new db_gmEntities4()) { DbModel.t_protocole.Add(newProt); DbModel.SaveChanges(); } //All protocols between start and end for (int i = 1; i < proTab.Length - 1; i++) { t_protocole newProtx = new t_protocole(); newProtx.tilId = tileId; newProtx.proIsDone = false; newProtx.proToDo = false; foreach (string sTask in tasks) { if (proTab[i].Contains(sTask)) { newProtx.proTask = sTask; } } var timeNbr = 1; foreach (char c in proTab[i]) { if (Char.IsDigit(c)) { timeNbr = int.Parse(c.ToString()); } } string tUnit = ""; foreach (string sTimeUnit in timeUnit) { if (proTab[i].Contains(sTimeUnit)) { tUnit = sTimeUnit; } } DateTime today = DateTime.Now; switch (tUnit) { case "": break; case "jours": TimeSpan daySpan = new TimeSpan(timeNbr, 0, 0, 0, 0); newProtx.proDateTask = today.Add(daySpan); break; case "semaines": TimeSpan weekSpan = new TimeSpan(timeNbr * 7, 0, 0, 0, 0); newProtx.proDateTask = DateTime.Now.Add(weekSpan); break; case "mois": TimeSpan monthSpan = new TimeSpan(timeNbr * 30, 0, 0, 0, 0); newProtx.proDateTask = DateTime.Now.Add(monthSpan); break; default: break; } using (db_gmEntities4 DbModel = new db_gmEntities4()) { DbModel.t_protocole.Add(newProtx); DbModel.SaveChanges(); } } //Last Protocol t_protocole newProtEnd = new t_protocole(); newProtEnd.tilId = tileId; newProtEnd.proIsDone = false; newProtEnd.proToDo = false; newProtEnd.proTask = "récolter"; int lpTimeNbr = 1; string lptUnit = ""; foreach (string sTimeUnit in timeUnit) { if (proTab[proTab.Length - 1].Contains(sTimeUnit)) { lptUnit = sTimeUnit; } } foreach (char c in proTab[proTab.Length - 1]) { if (Char.IsDigit(c)) { lpTimeNbr = int.Parse(c.ToString()); } } switch (lptUnit) { case "": break; case "semaines": TimeSpan weekSpan = new TimeSpan(lpTimeNbr * 7, 0, 0, 0, 0); newProtEnd.proDateTask = DateTime.Now.Add(weekSpan); break; case "mois": TimeSpan monthSpan = new TimeSpan(lpTimeNbr * 30, 0, 0, 0, 0); newProtEnd.proDateTask = DateTime.Now.Add(monthSpan); break; default: break; } using (db_gmEntities4 DbModel = new db_gmEntities4()) { DbModel.t_protocole.Add(newProtEnd); DbModel.SaveChanges(); } } return(RedirectToAction("Index")); }