LoadingFromDBDoesntUpdateModified()
        {
            Setup();
            string name = Guid.NewGuid().ToString("N");
            DatabaseCourseRepository r;
            Course         course;
            DateTimeOffset modified;

            using (var db = new DemoDbContext())
            {
                r        = new DatabaseCourseRepository(db);
                course   = new Course(name);
                modified = course.Modified;
                r.Add(course);
                db.SaveChanges();
            }
            //
            // Only check down to the second because .NET and the RDBMS DateTime
            // resolutions probably aren't the same.  And even if they were the same
            // for some RDBMS', they might not be for others.
            //
            Thread.Sleep(1500);
            using (var db = new DemoDbContext())
            {
                r      = new DatabaseCourseRepository(db);
                course = r.All.Where(c => c.Name == name).Single();
                Assert(
                    course.Modified.Minute == modified.Minute &&
                    course.Modified.Second == modified.Second);
            }
        }
        RemoveModule()
        {
            Setup();
            string name = Guid.NewGuid().ToString("N");
            DatabaseCourseRepository r;
            Course course;

            using (var db = new DemoDbContext())
            {
                r      = new DatabaseCourseRepository(db);
                course = new Course(name);
                course.AddModule(new Module("m1"));
                course.AddModule(new Module("m2"));
                r.Add(course);
                Assert(r.All.Where(c => c.Name == name).Single().Modules.Count == 2);
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r      = new DatabaseCourseRepository(db);
                course = r.All.Where(c => c.Name == name).Single();
                course.RemoveModule(course.Modules.First());
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r      = new DatabaseCourseRepository(db);
                course = r.All.Where(c => c.Name == name).Single();
                Assert(course.Modules.Count == 1);
            }
        }
        All()
        {
            Setup();
            DatabaseCourseRepository r;
            string name1 = Guid.NewGuid().ToString("N");
            string name2 = Guid.NewGuid().ToString("N");

            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                r.Add(new Course(name1));
                r.Add(new Course(name2));
                Assert(r.All.Any(c => c.Name == name1));
                Assert(r.All.Any(c => c.Name == name2));
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                Assert(r.All.Any(c => c.Name == name1));
                Assert(r.All.Any(c => c.Name == name2));
            }
        }
        Remove()
        {
            DatabaseCourseRepository r;

            Setup();
            string name = Guid.NewGuid().ToString("N");

            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                var course = new Course(name);
                r.Add(course);
                r.Remove(course);
                Assert(!r.All.Any(c => c.Name == name));
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                var course = new Course(name);
                r.Add(course);
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                var course = r.All.Single(c => c.Name == name);
                r.Remove(course);
                db.SaveChanges();
            }
            using (var db = new DemoDbContext())
            {
                r = new DatabaseCourseRepository(db);
                Assert(!r.All.Any(c => c.Name == name));
            }
        }
        public ActionResult Course(Course course)
        {
            string courseName = (RouteData.Values["courseName"] ?? "").ToString().Trim();
            string action     = (Request.Form["action"] ?? "").Trim().ToLowerInvariant();

            bool deleting   = (action == "delete" && courseName != "");
            bool creating   = (!deleting && courseName == "");
            bool updating   = (!deleting && !creating);
            bool cancelling = (action == "cancel");

            ViewBag.CourseName = courseName;
            ViewBag.Creating   = creating;
            ViewBag.Updating   = updating;

            if (cancelling)
            {
                if (creating)
                {
                    return(RedirectToRoute("Courses"));
                }
                else
                {
                    return(RedirectToRoute("Course", new { courseName = courseName }));
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(course));
            }

            using (var db = new DemoDbContext())
            {
                ICourseRepository courses  = new DatabaseCourseRepository(db);
                Course            existing =
                    !creating
                ? courses.All.Single(c => c.Name == courseName)
                : null;

                if (deleting)
                {
                    courses.Remove(existing);
                    db.SaveChanges();
                    return(RedirectToRoute("Courses"));
                }

                try
                {
                    if (creating)
                    {
                        courses.Add(course);
                    }
                    else
                    {
                        course.CopyShallowTo(existing);
                    }
                }
                catch (InvalidOperationException ioe)
                {
                    ModelState.AddModelError("", ioe.Message);
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);
                }

                if (!ModelState.IsValid)
                {
                    return(View(course));
                }

                db.SaveChanges();
                return(RedirectToRoute("Course", new { courseName = course.Name }));
            }
        }