public ActionResult EditPost(Worker worker, bool isQuarantined, int? CloudID, int?[] ServicesList) { if (!CloudID.HasValue) ModelState.AddModelError(String.Empty, "Please choose a cloud"); if (ModelState.IsValid) { db.Entry(worker).State = EntityState.Modified; db.SaveChanges(); //Link To Cloud var CloudList = db.Clouds.ToList(); for (int CloudIndex = 0; CloudIndex < CloudList.Count(); CloudIndex++) if (CloudList[CloudIndex].Workers.Where(x => x.WorkerId == worker.WorkerId).Count() > 0) db.Clouds.ToList()[CloudIndex].Workers.Remove(worker); db.SaveChanges(); if (CloudID.HasValue) { db.Clouds.Find(CloudID).Workers.Add(worker); db.SaveChanges(); } //Services List var worker2 = db.Workers.Include(x => x.Services).Where(x => x.WorkerId == worker.WorkerId).First(); if (worker2.Services == null) worker2.Services = new List<Service>(); foreach (var service in db.Services.ToList()) worker2.Services.Remove(service); db.SaveChanges(); if (ServicesList != null) { foreach (int serviceID in ServicesList) { var service = db.Services.Find(serviceID); worker2.Services.Add(service); } db.SaveChanges(); } WorkerContainersManagement workerContainersManagement = new WorkerContainersManagement(); workerContainersManagement.RefreshWorker(worker2.WorkerId); return RedirectToAction("Dashboard", "Home"); } return View("~/Views/Home/Dashboard.cshtml", worker); }
public ActionResult CreatePost(Worker worker, bool isQuarantined, int? CloudID, int?[] ServicesList, int? MachineID) { if (!CloudID.HasValue) ModelState.AddModelError(String.Empty, "Please choose a cloud"); if (!MachineID.HasValue) ModelState.AddModelError(String.Empty, "Please choose a machine"); if (!CloudID.HasValue) ModelState.AddModelError(String.Empty, "Please choose a cloud"); if (ServicesList == null) ModelState.AddModelError(String.Empty, "Please choose at least one service"); if (ModelState.IsValid) { //Add the worker it self worker.isQuarantined = isQuarantined; db.Workers.Add(worker); db.SaveChanges(); //Add Services to the worker worker.Services = new List<Service>(); foreach (int serviceID in ServicesList) { var service = db.Services.Find(serviceID); worker.Services.Add(service); } db.SaveChanges(); //Add this worker to a cloud db.Clouds.Find(CloudID).Workers.Add(worker); db.SaveChanges(); //Add this worker to a machine db.Machines.Find(MachineID).Workers.Add(worker); db.SaveChanges(); CreateContainer(worker.WorkerId); return RedirectToAction("Dashboard", "Home"); } return View("~/Views/Home/Dashboard.cshtml", worker); }