/// <summary>
 /// Initializes a new instance of the <see cref="CoursesController"/> class for use live.
 /// </summary>
 public CoursesController()
 {
     this.db = new LabinatorContext();
     this.st = new SkyTap();
     this.template = new Template(this.st);
 }
        public void PopulateTemp(int courseId, string templateId, string sessionId)
        {
            Template t = new Template(this.st);
            if (templateId == string.Empty)
            {
                ////    t.VMs = new Vm[0];
            }
            else
            {
                t = t.GetTemplate(templateId);
            }

            List<CourseMachineTemp> cmts = this.db.Query<CourseMachineTemp>()
                                                    .Where(c => c.SessionId == sessionId)
                                                    .ToList();
            if (cmts != null)
            {
                foreach (CourseMachineTemp cmt in cmts)
                {
                    this.db.Remove<CourseMachineTemp>(cmt);
                }
            }

            if ((courseId == 0) || (this.db.Query<Course>()
                                            .Where(c => c.CourseId == courseId)
                                            .First()
                                            .Template != templateId))
            {
                if (t.VMs != null)
                {
                    foreach (Vm v in t.VMs)
                    {
                        CourseMachineTemp cmt = new CourseMachineTemp();
                        cmt.VMId = v.id;
                        cmt.VMName = v.name;
                        cmt.SessionId = sessionId;
                        cmt.TimeStamp = DateTime.Now;
                        cmt.IsActive = true;
                        this.db.Add<CourseMachineTemp>(cmt);
                    }
                }

                this.db.SaveChanges();
            }
            else
            {
                List<CourseMachine> cms = this.db.Query<CourseMachine>()
                                                    .Where(cm => cm.CourseId == courseId)
                                                    .ToList();
                if (cms != null)
                {
                    foreach (CourseMachine cm in cms)
                    {
                        CourseMachineTemp cmt = new CourseMachineTemp();
                        cmt.VMId = cm.VMId;
                        cmt.SessionId = sessionId;
                        cmt.TimeStamp = DateTime.Now;
                        cmt.IsActive = cm.IsActive;
                        this.db.Add<CourseMachineTemp>(cmt);
                    }

                    this.db.SaveChanges();
                }

                cmts = this.db.Query<CourseMachineTemp>()
                                .Where(c => c.SessionId == sessionId)
                                .ToList();
                foreach (Vm v in t.VMs)
                {
                    CourseMachineTemp cmt = cmts.Where(cm => cm.VMId == v.id).FirstOrDefault();
                    if (cmt == null)
                    {
                        cmt = new CourseMachineTemp();
                        cmt.VMId = v.id;
                        cmt.VMName = v.name;
                        cmt.SessionId = sessionId;
                        cmt.TimeStamp = DateTime.Now;
                        cmt.IsActive = true;
                        this.db.Add<CourseMachineTemp>(cmt);
                    }
                    else
                    {
                        cmt.VMName = v.name;
                        this.db.Update<CourseMachineTemp>(cmt);
                    }

                    this.db.SaveChanges();
                }

                foreach (CourseMachineTemp cmt in cmts)
                {
                    Vm v = t.VMs.Where(vm => vm.id == cmt.VMId).FirstOrDefault();
                    if (v == null)
                    {
                        this.db.Remove(cmt);
                    }
                }

                this.db.SaveChanges();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CoursesController"/> class for use in testing.
 /// </summary>
 /// <param name="db">The Fake database</param>
 /// <param name="st">The Fake SkyTap</param>
 public CoursesController(ILabinatorDb db, ISkyTap st)
 {
     this.db = db;
     this.st = st;
     this.template = new Template(this.st);
 }
        public ActionResult Edit(int id)
        {
            Template template = new Template(this.st);
            Course course = null;
            if (id == 0)
            {
                course = new Course() { CourseId = 0, Name = "New", Days = 1, Hours = 8, Template = string.Empty, StartTime = new DateTime(2015, 12, 31, 8, 30, 00) };
            }
            else
            {
                course = this.db.Query<Course>().Where(c => c.CourseId == id).FirstOrDefault();
                if (course == null)
                {
                    return this.HttpNotFound();
                }
            }

            ViewBag.Session = Guid.NewGuid().ToString();
            this.PopulateTemp(id, course.Template, ViewBag.Session);
            List<Template> templates = template.GetList();
            templates.Insert(0, new Template() { name = "Please Select Template...", id = string.Empty });
            ViewBag.Template = new SelectList(templates, "Id", "Name", course.Template);
            return this.View(course);
        }