Esempio n. 1
0
            public async Task Should_return_all_videos()
            {
                //Arrange
                GenData.RepeatCount = 3;
                List <Video> expectedVideos = new List <Video>(GenData.RepeatCount);

                GenData.AddManyTo(expectedVideos, TestDataFactory.CreateVideo);

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.AddRange(expectedVideos);
                    context.SaveChanges();
                }

                //Act
                IEnumerable <Video> result = await RepoUnderTest.GetAsync();

                //Assert
                result.Should().BeEquivalentTo(expectedVideos, o =>
                                               o.Excluding(x => x.Kind)
                                               .Excluding(x => x.Genre));
            }
Esempio n. 2
0
            public void Should_update_and_add_new_properties_then_return_the_specified_video()
            {
                // Arrange
                Video oldVideo = TestDataFactory.CreateVideo();

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.Add(oldVideo);
                    context.SaveChanges();
                }

                Kind  newKind  = TestDataFactory.CreateKind();
                Genre newGenre = TestDataFactory.CreateGenre();
                Video newVideo = TestDataFactory.CreateVideo(newKind, newGenre);

                newVideo.Id = oldVideo.Id;

                //Act
                Video result = RepoUnderTest.Update(newVideo);

                //Assert
                result.Should().BeEquivalentTo(newVideo);
            }
Esempio n. 3
0
            public async Task Should_return_empty_collection()
            {
                //Arrange
                Genre notIncludedGenre = TestDataFactory.CreateGenre();

                GenData.RepeatCount = 3;
                List <Video> videos = new List <Video>(GenData.RepeatCount);

                GenData.AddManyTo(videos, TestDataFactory.CreateVideo);

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.AddRange(videos);
                    context.Genres.Add(notIncludedGenre);
                    context.SaveChanges();
                }

                //Act
                IEnumerable <Video> result = await RepoUnderTest.GetVideosByGenreIdAsync(notIncludedGenre.Id);

                //Assert
                Assert.Empty(result);
            }
Esempio n. 4
0
            public async Task Should_not_return_inactive_videos()
            {
                //Arrange
                GenData.RepeatCount = 3;
                List <Video> videos = new List <Video>(GenData.RepeatCount);

                GenData.AddManyTo(videos, TestDataFactory.CreateVideo);
                videos.Last().IsInactive = true;

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.AddRange(videos);
                    context.SaveChanges();
                }

                List <Video> expectedVideos = videos.Take(2).ToList();

                //Act
                IEnumerable <Video> result = await RepoUnderTest.GetAsync();

                //Assert
                result.Any(x => x.IsInactive).Should().BeFalse();
            }
Esempio n. 5
0
            public async Task Should_return_videos_with_specified_title()
            {
                //Arrange
                string expectedTitle = "Expected Title";
                string titleToSearch = "EXPected";

                Video expectedVideo0 = TestDataFactory.CreateVideo();

                expectedVideo0.Title = expectedTitle;
                Video expectedVideo1 = TestDataFactory.CreateVideo();

                expectedVideo1.Title = expectedTitle;
                List <Video> videos = new List <Video>(2)
                {
                    expectedVideo0,
                    expectedVideo1
                };

                GenData.RepeatCount = 3;
                GenData.AddManyTo(videos, () => TestDataFactory.CreateVideo());

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.AddRange(videos);
                    context.SaveChanges();
                }

                List <Video> expectedVideos = videos.Take(2).ToList();

                //Act
                IEnumerable <Video> result = await RepoUnderTest.GetVideosContainTitleAsync(titleToSearch);

                //Assert
                result.Should().BeEquivalentTo(expectedVideos, o =>
                                               o.Excluding(x => x.Kind)
                                               .Excluding(x => x.Genre));
            }
Esempio n. 6
0
            public async Task Should_return_empty_collection()
            {
                //Arrange
                Kind         kindWithoutVideos = TestDataFactory.CreateKind();
                Kind         kind   = TestDataFactory.CreateKind();
                List <Video> videos = new List <Video>()
                {
                    TestDataFactory.CreateVideo(kind),
                    TestDataFactory.CreateVideo(kind)
                };

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Videos.AddRange(videos);
                    context.Kinds.Add(kindWithoutVideos);
                    context.SaveChanges();
                }

                //Act
                IEnumerable <Video> result = await RepoUnderTest.GetVideosByKindIdAsync(kindWithoutVideos.Id);

                //Assert
                Assert.Empty(result);
            }
Esempio n. 7
0
            public void Should_update_and_return_the_specified_genre()
            {
                // Arrange
                Genre oldGenre = TestDataFactory.CreateGenre();

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Genres.Add(oldGenre);
                    context.SaveChanges();
                }

                Genre newGenre = new Genre()
                {
                    Id     = oldGenre.Id,
                    Name   = "new genre",
                    Videos = null
                };

                // Act
                Genre result = RepoUnderTest.Update(newGenre);

                // Assert
                result.Should().BeEquivalentTo(newGenre);
            }
Esempio n. 8
0
            public void Should_update_and_return_the_specified_Kind()
            {
                // Arrange
                Kind oldKind = TestDataFactory.CreateKind();

                using (VODContext context = ContextFactory.CreateContext())
                {
                    context.Kinds.Add(oldKind);
                    context.SaveChanges();
                }

                Kind newKind = new Kind()
                {
                    Id     = oldKind.Id,
                    Name   = "new Kind",
                    Videos = null
                };

                // Act
                Kind result = RepoUnderTest.Update(newKind);

                // Assert
                result.Should().BeEquivalentTo(newKind);
            }
Esempio n. 9
0
        public static void Initialize(VODContext context)

        {
            var description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";

            var email = "*****@*****.**";

            var adminRoleId = string.Empty;

            var userId = string.Empty;



            if (context.Users.Any(r => r.Email.Equals(email)))
            {
                userId = context.Users.First(r => r.Email.Equals(email)).Id;
            }



            if (!userId.Equals(string.Empty))

            {
                if (!context.Instructors.Any())

                {
                    var instructors = new List <Instructor>

                    {
                        new Instructor {
                            Name = "Mohd Asif",

                            Description = description.Substring(20, 50),

                            Thumbnail = "/images/Ice-Age-Scrat-icon.png"
                        },

                        new Instructor {
                            Name = "Mohd Asif",

                            Description = description.Substring(30, 40),

                            Thumbnail = "/images/Ice-Age-Scrat-icon.png"
                        }
                    };

                    context.Instructors.AddRange(instructors);

                    context.SaveChanges();
                }



                if (!context.Courses.Any())

                {
                    var instructorId1 = context.Instructors.First().Id;

                    var instructorId2 = int.MinValue;

                    var instructor = context.Instructors.Skip(1).FirstOrDefault();

                    if (instructor != null)
                    {
                        instructorId2 = instructor.Id;
                    }

                    else
                    {
                        instructorId2 = instructorId1;
                    }



                    var courses = new List <Course>

                    {
                        new Course {
                            InstructorId = instructorId1,

                            Title = "Course 1",

                            Description = description,

                            ImageUrl = "/images/course.jpg",

                            MarqueeImageUrl = "/images/laptop.jpg"
                        },

                        new Course {
                            InstructorId = instructorId2,

                            Title = "Course 2",

                            Description = description,

                            ImageUrl = "/images/course2.jpg",

                            MarqueeImageUrl = "/images/laptop.jpg"
                        },

                        new Course {
                            InstructorId = instructorId1,

                            Title = "Course 3",

                            Description = description,

                            ImageUrl = "/images/course3.jpg",

                            MarqueeImageUrl = "/images/laptop.jpg"
                        }
                    };

                    context.Courses.AddRange(courses);

                    context.SaveChanges();
                }



                var courseId1 = int.MinValue;

                var courseId2 = int.MinValue;

                var courseId3 = int.MinValue;

                if (context.Courses.Any())

                {
                    courseId1 = context.Courses.First().Id;



                    var course = context.Courses.Skip(1).FirstOrDefault();

                    if (course != null)
                    {
                        courseId2 = course.Id;
                    }



                    course = context.Courses.Skip(2).FirstOrDefault();

                    if (course != null)
                    {
                        courseId3 = course.Id;
                    }
                }



                if (!context.UserCourses.Any())

                {
                    if (!courseId1.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse

                        {
                            UserId = userId, CourseId = courseId1
                        });
                    }



                    if (!courseId2.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse

                        {
                            UserId = userId, CourseId = courseId2
                        });
                    }



                    if (!courseId3.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse

                        {
                            UserId = userId, CourseId = courseId3
                        });
                    }



                    context.SaveChanges();
                }



                if (!context.Modules.Any())

                {
                    var modules = new List <Module>

                    {
                        new Module {
                            CourseId = courseId1, Title = "Modeule 1"
                        },

                        new Module {
                            CourseId = courseId1, Title = "Modeule 2"
                        },

                        new Module {
                            CourseId = courseId2, Title = "Modeule 3"
                        }
                    };

                    context.Modules.AddRange(modules);

                    context.SaveChanges();
                }



                var moduleId1 = int.MinValue;

                var moduleId2 = int.MinValue;

                var moduleId3 = int.MinValue;

                if (context.Modules.Any())

                {
                    moduleId1 = context.Modules.First().Id;



                    var module = context.Modules.Skip(1).FirstOrDefault();

                    if (module != null)
                    {
                        moduleId2 = module.Id;
                    }

                    else
                    {
                        moduleId2 = moduleId1;
                    }



                    module = context.Modules.Skip(2).FirstOrDefault();

                    if (module != null)
                    {
                        moduleId3 = module.Id;
                    }

                    else
                    {
                        moduleId3 = moduleId1;
                    }
                }



                if (!context.Videos.Any())

                {
                    var videos = new List <Video>

                    {
                        new Video {
                            ModuleId = moduleId1, CourseId = courseId1,

                            Position = 1, Title = "Video 1 Title",

                            Description = description.Substring(1, 35),

                            Duration = 50, Thumbnail = "/images/video1.jpg",

                            Url = "https://www.youtube.com/embed/NOUoPLNKf0I"
                        },

                        new Video

                        {
                            ModuleId = moduleId1,

                            CourseId = courseId1,

                            Position = 2,

                            Title = "Video 2 Title",

                            Description = description.Substring(5, 35),

                            Duration = 45,

                            Thumbnail = "/images/video2.jpg",

                            Url = "https://www.youtube.com/embed/NOUoPLNKf0I"
                        },

                        new Video

                        {
                            ModuleId = moduleId1,

                            CourseId = courseId1,

                            Position = 3,

                            Title = "Video 3 Title",

                            Description = description.Substring(10, 35),

                            Duration = 41,

                            Thumbnail = "/images/video3.jpg",

                            Url = "https://www.youtube.com/embed/NOUoPLNKf0I"
                        },

                        new Video

                        {
                            ModuleId = moduleId3,

                            CourseId = courseId2,

                            Position = 1,

                            Title = "Video 4 Title",

                            Description = description.Substring(15, 35),

                            Duration = 41,

                            Thumbnail = "/images/video4.jpg",

                            Url = "https://www.youtube.com/embed/NOUoPLNKf0I"
                        },

                        new Video

                        {
                            ModuleId = moduleId2,

                            CourseId = courseId1,

                            Position = 1,

                            Title = "Video 5 Title",

                            Description = description.Substring(20, 35),

                            Duration = 42,

                            Thumbnail = "/images/video5.jpg",

                            Url = "https://www.youtube.com/embed/NOUoPLNKf0I"
                        }
                    };

                    context.Videos.AddRange(videos);

                    context.SaveChanges();
                }



                if (!context.Downloads.Any())

                {
                    var downloads = new List <Download>

                    {
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,

                            Title = "ADO.NET 1 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        },

                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,

                            Title = "ADO.NET 2 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        },

                        new Download {
                            ModuleId = moduleId3, CourseId = courseId2,

                            Title = "ADO.NET 1 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        }
                    };



                    context.Downloads.AddRange(downloads);

                    context.SaveChanges();
                }
            }
        }
Esempio n. 10
0
        public static void Initialize(VODContext context)
        {
            #region Lorem Ipsum - Dummy Data
            var description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
            #endregion

            #region Admin Credentials Properties

            /*
             *  The email address should be in the AspNetUsers table; if not,
             *  then register a user with that email address or change the variable
             *  value to an email address in the table. The user should be an
             *  administrator; if not, open the AspNetUserRoles table and add
             *  a record using the user id and 1 (or the id you gave the Admin
             *  role in the AspNetRoles table) in the RoleId column.
             */
            var email       = "[email protected]";
            var adminRoleId = string.Empty;
            var userId      = string.Empty;
            #endregion

            // Fetch the User Data
            if (context.Users.Any(r => r.Email.Equals(email)))
            {
                userId = context.Users.First(r => r.Email.Equals(email)).Id;
            }

            if (!userId.Equals(string.Empty))
            {
                #region Add Instructors if they don't already exist
                if (!context.Instructors.Any())
                {
                    var instructors = new List <Instructor>
                    {
                        new Instructor {
                            Name        = "John Doe",
                            Description = description.Substring(20, 50),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        },
                        new Instructor {
                            Name        = "Jane Doe",
                            Description = description.Substring(30, 40),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        }
                    };
                    context.Instructors.AddRange(instructors);
                    context.SaveChanges();
                }
                #endregion

                #region Add Courses if they don't already exist
                if (!context.Courses.Any())
                {
                    var instructorId1 = context.Instructors.First().Id;
                    var instructorId2 = int.MinValue;
                    var instructor    = context.Instructors.Skip(1).FirstOrDefault();
                    if (instructor != null)
                    {
                        instructorId2 = instructor.Id;
                    }
                    else
                    {
                        instructorId2 = instructorId1;
                    }

                    var courses = new List <Course>
                    {
                        new Course {
                            InstructorId    = instructorId1,
                            Title           = "Course 1",
                            Description     = description,
                            ImageUrl        = "/images/course1.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course {
                            InstructorId    = instructorId2,
                            Title           = "Course 2",
                            Description     = description,
                            ImageUrl        = "/images/course2.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course {
                            InstructorId    = instructorId1,
                            Title           = "Course 3",
                            Description     = description,
                            ImageUrl        = "/images/course3.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        }
                    };
                    context.Courses.AddRange(courses);
                    context.SaveChanges();
                }
                #endregion

                #region Fetch Course ids if any courses exists
                var courseId1 = int.MinValue;
                var courseId2 = int.MinValue;
                var courseId3 = int.MinValue;
                if (context.Courses.Any())
                {
                    courseId1 = context.Courses.First().Id;

                    var course = context.Courses.Skip(1).FirstOrDefault();
                    if (course != null)
                    {
                        courseId2 = course.Id;
                    }

                    course = context.Courses.Skip(2).FirstOrDefault();
                    if (course != null)
                    {
                        courseId3 = course.Id;
                    }
                }
                #endregion

                #region Add UserCourses connections if they don't already exist
                if (!context.UserCourses.Any())
                {
                    if (!courseId1.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse {
                            UserId = userId, CourseId = courseId1
                        });
                    }

                    if (!courseId2.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse {
                            UserId = userId, CourseId = courseId2
                        });
                    }

                    if (!courseId3.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse {
                            UserId = userId, CourseId = courseId3
                        });
                    }

                    context.SaveChanges();
                }
                #endregion

                #region Add Modules if they don't already exist
                if (!context.Modules.Any())
                {
                    var modules = new List <Module>
                    {
                        new Module {
                            Course = context.Find <Course>(courseId1), Title = "Modeule 1"
                        },
                        new Module {
                            Course = context.Find <Course>(courseId1), Title = "Modeule 2"
                        },
                        new Module {
                            Course = context.Find <Course>(courseId2), Title = "Modeule 3"
                        }
                    };
                    context.Modules.AddRange(modules);
                    context.SaveChanges();
                }
                #endregion

                #region Fetch Module ids if any modules exist
                var moduleId1 = int.MinValue;
                var moduleId2 = int.MinValue;
                var moduleId3 = int.MinValue;
                if (context.Modules.Any())
                {
                    moduleId1 = context.Modules.First().Id;

                    var module = context.Modules.Skip(1).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId2 = module.Id;
                    }
                    else
                    {
                        moduleId2 = moduleId1;
                    }

                    module = context.Modules.Skip(2).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId3 = module.Id;
                    }
                    else
                    {
                        moduleId3 = moduleId1;
                    }
                }
                #endregion

                #region Add Videos if they don't already exist
                if (!context.Videos.Any())
                {
                    var videos = new List <Video>
                    {
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 1 Title",
                            Description = description.Substring(1, 35),
                            Duration    = 50, Thumbnail = "/images/video1.jpg",
                            Url         = "https://www.youtube.com/embed/BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 2 Title",
                            Description = description.Substring(5, 35),
                            Duration    = 45, Thumbnail = "/images/video2.jpg",
                            Url         = "https://www.youtube.com/embed/BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 3 Title",
                            Description = description.Substring(10, 35),
                            Duration    = 41, Thumbnail = "/images/video3.jpg",
                            Url         = "https://www.youtube.com/embed/BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId3, CourseId = courseId2,
                            Title       = "Video 4 Title",
                            Description = description.Substring(15, 35),
                            Duration    = 41, Thumbnail = "/images/video4.jpg",
                            Url         = "https://www.youtube.com/embed/BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId2, CourseId = courseId1,
                            Title       = "Video 5 Title",
                            Description = description.Substring(20, 35),
                            Duration    = 42, Thumbnail = "/images/video5.jpg",
                            Url         = "https://www.youtube.com/embed/BJFyzpBcaCY"
                        }
                    };
                    context.Videos.AddRange(videos);
                    context.SaveChanges();
                }
                #endregion

                #region Add Downloads if they don't already exist
                if (!context.Downloads.Any())
                {
                    var downloads = new List <Download>
                    {
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://some-url"
                        },

                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 2 (PDF)", Url = "https://some-url"
                        },

                        new Download {
                            ModuleId = moduleId3, CourseId = courseId2,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://some-url"
                        }
                    };

                    context.Downloads.AddRange(downloads);
                    context.SaveChanges();
                }
                #endregion
            }
        }
Esempio n. 11
0
        public static void Initialize(VODContext context)
        {
            var email       = "[email protected]";
            var adminRoleId = string.Empty;
            var userId      = string.Empty;

            var description = string.Empty;

            if (context.Users.Any(r => r.Email.Equals(email)))
            {
                userId = context.Users.First(r => r.Email.Equals(email)).Id;
            }

            if (!userId.Equals(string.Empty))
            {
                if (!context.Instructors.Any())
                {
                    var instructors = new List <Instructor>
                    {
                        new Instructor {
                            Name        = "John Doe",
                            Description = description.Substring(20, 50),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        },
                        new Instructor {
                            Name        = "Jane Doe",
                            Description = description.Substring(30, 40),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        }
                    };
                    context.Instructors.AddRange(instructors);
                    context.SaveChanges();
                }

                // Course
                if (!context.Courses.Any())
                {
                    var instructorId1 = context.Instructors.First().Id;
                    var instructorId2 = int.MinValue;
                    var instructor    = context.Instructors.Skip(1).FirstOrDefault();
                    if (instructor != null)
                    {
                        instructorId2 = instructor.Id;
                    }
                    else
                    {
                        instructorId2 = instructorId1;
                    }
                    var courses = new List <Course>
                    {
                        new Course
                        {
                            InstructorId    = instructorId1,
                            Title           = "Course 1",
                            Description     = description,
                            ImageUrl        = "/images/course1.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course
                        {
                            InstructorId    = instructorId2,
                            Title           = "Course 2",
                            Description     = description,
                            ImageUrl        = "/images/course2.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course
                        {
                            InstructorId    = instructorId1,
                            Title           = "Course 3",
                            Description     = description,
                            ImageUrl        = "/images/course3.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        }
                    };
                    context.Courses.AddRange(courses);
                    context.SaveChanges();
                }

                //

                var courseId1 = int.MinValue;
                var courseId2 = int.MinValue;
                var courseId3 = int.MinValue;
                if (context.Courses.Any())
                {
                    courseId1 = context.Courses.First().Id;
                    var course = context.Courses.Skip(1).FirstOrDefault();
                    if (course != null)
                    {
                        courseId2 = course.Id;
                    }
                    course = context.Courses.Skip(2).FirstOrDefault();
                    if (course != null)
                    {
                        courseId3 = course.Id;
                    }
                }


                if (!context.UserCourses.Any())
                {
                    if (!courseId1.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId1
                        });
                    }
                    if (!courseId2.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId2
                        });
                    }
                    if (!courseId3.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId3
                        });
                    }
                    context.SaveChanges();
                }


                // Modules

                if (!context.Modules.Any())
                {
                    var modules = new List <Module>
                    {
                        new Module {
                            Course = context.Find <Course>(courseId1),
                            Title  = "Module 1"
                        },
                        new Module {
                            Course = context.Find <Course>(courseId1),
                            Title  = "Module 2"
                        },
                        new Module {
                            Course = context.Find <Course>(courseId2),
                            Title  = "Module 3"
                        }
                    };
                    context.Modules.AddRange(modules);
                    context.SaveChanges();
                }

                //?????? chlee

                // 14번 New Module
                var moduleId1 = int.MinValue;
                var moduleId2 = int.MinValue;
                var moduleId3 = int.MinValue;
                if (context.Modules.Any())
                {
                    moduleId1 = context.Modules.First().Id;
                    var module = context.Modules.Skip(1).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId2 = module.Id;
                    }
                    else
                    {
                        moduleId2 = moduleId1;
                    }
                    module = context.Modules.Skip(2).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId3 = module.Id;
                    }
                    else
                    {
                        moduleId3 = moduleId1;
                    }
                }


                //15

                if (!context.Videos.Any())
                {
                    //int courseId1 = 0;
                    //int courseId2 = 0;
                    var videos = new List <Video>
                    {
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 1 Title",
                            Description = description.Substring(1, 35),
                            Duration    = 50, Thumbnail = "/images/video1.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 2 Title",
                            Description = description.Substring(5, 35),
                            Duration    = 45, Thumbnail = "/images/video2.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Title       = "Video 3 Title",
                            Description = description.Substring(10, 35),
                            Duration    = 41, Thumbnail = "/images/video3.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId3, CourseId = courseId2,
                            Title       = "Video 4 Title",
                            Description = description.Substring(15, 35),
                            Duration    = 41, Thumbnail = "/images/video4.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video {
                            ModuleId    = moduleId2, CourseId = courseId1,
                            Title       = "Video 5 Title",
                            Description = description.Substring(20, 35),
                            Duration    = 42, Thumbnail = "/images/video5.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        }
                    };
                    context.Videos.AddRange(videos);
                    context.SaveChanges();
                }


                //16
                if (!context.Downloads.Any())
                {
                    // int courseId1 = 0;
                    // int courseId2 = 0;
                    var downloads = new List <Download>
                    {
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://some-url"
                        },
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 2 (PDF)", Url = "https://some-url"
                        },
                        new Download {
                            ModuleId = moduleId3, CourseId = courseId2,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://some-url"
                        }
                    };
                    context.Downloads.AddRange(downloads);
                    context.SaveChanges();
                }
            }
        }
Esempio n. 12
0
        // To add data to a table, you create a list of the entity type and add instances to it.
        // add that list to the entity collection (the DbSet for that entity), in the VODContext class, using the context object passed into the Initialize method.
        public static void Initialize(VODContext context)
        {
            var description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
            var email       = "*****@*****.**";
            var adminRoleId = string.Empty;
            var userId      = string.Empty;

            // Try to fetch the user id from the AspNetUsers table using the Users entity.
            if (context.Users.Any(r => r.Email.Equals(email)))
            {
                userId = context.Users.First(r => r.Email.Equals(email)).Id;
            }

            // if the user id was successfully fetched
            if (!userId.Equals(string.Empty))
            {
                // Use the Instructors entity to add instructor data to the Instructors table in the database if no data has been added.
                if (!context.Instructors.Any())
                {
                    var instructors = new List <Instructor>
                    {
                        new Instructor {
                            Name        = "John Doe",
                            Description = description.Substring(20, 50),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        },
                        new Instructor {
                            Name        = "Jane Doe",
                            Description = description.Substring(30, 40),
                            Thumbnail   = "/images/Ice-Age-Scrat-icon.png"
                        }
                    };
                    context.Instructors.AddRange(instructors);
                    context.SaveChanges();
                }

                // Use the Courses entity to add course data to the Courses table in the database if no data has been added.
                if (!context.Courses.Any())
                {
                    var instructorId1 = context.Instructors.First().Id;
                    var instructorId2 = int.MinValue;
                    var instructor    = context.Instructors.Skip(1).FirstOrDefault();
                    if (instructor != null)
                    {
                        instructorId2 = instructor.Id;
                    }
                    else
                    {
                        instructorId2 = instructorId1;
                    }

                    var courses = new List <Course>
                    {
                        new Course {
                            InstructorId    = instructorId1,
                            Title           = "Course 1",
                            Description     = description,
                            ImageUrl        = "/images/course.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course {
                            InstructorId    = instructorId2,
                            Title           = "Course 2",
                            Description     = description,
                            ImageUrl        = "/images/course2.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        },
                        new Course {
                            InstructorId    = instructorId1,
                            Title           = "Course 3",
                            Description     = description,
                            ImageUrl        = "/images/course3.jpg",
                            MarqueeImageUrl = "/images/laptop.jpg"
                        }
                    };
                    context.Courses.AddRange(courses);
                    context.SaveChanges();
                }

                // Try to fetch the course ids from the newly added courses. These ids will be used in other tables when referencing courses.
                var courseId1 = int.MinValue;
                var courseId2 = int.MinValue;
                var courseId3 = int.MinValue;
                if (context.Courses.Any())
                {
                    courseId1 = context.Courses.First().Id;

                    var course = context.Courses.Skip(1).FirstOrDefault();
                    if (course != null)
                    {
                        courseId2 = course.Id;
                    }

                    course = context.Courses.Skip(2).FirstOrDefault();
                    if (course != null)
                    {
                        courseId3 = course.Id;
                    }
                }

                // 13. Use the UserCourses entity to connect users and courses
                if (!context.UserCourses.Any())
                {
                    if (!courseId1.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId1
                        });
                    }

                    if (!courseId2.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId2
                        });
                    }

                    if (!courseId3.Equals(int.MinValue))
                    {
                        context.UserCourses.Add(new UserCourse
                        {
                            UserId = userId, CourseId = courseId3
                        });
                    }

                    context.SaveChanges();
                }

                // Use the Modules entity to add module data to the Modules table in the database if no data has been added.
                if (!context.Modules.Any())
                {
                    var modules = new List <Module>
                    {
                        new Module {
                            CourseId = courseId1, Title = "Modeule 1"
                        },
                        new Module {
                            CourseId = courseId1, Title = "Modeule 2"
                        },
                        new Module {
                            CourseId = courseId2, Title = "Modeule 3"
                        }
                    };
                    context.Modules.AddRange(modules);
                    context.SaveChanges();
                }

                // Try to fetch the module ids from the newly added modules. These ids will be used in other tables when referencing modules.
                var moduleId1 = int.MinValue;
                var moduleId2 = int.MinValue;
                var moduleId3 = int.MinValue;
                if (context.Modules.Any())
                {
                    moduleId1 = context.Modules.First().Id;

                    var module = context.Modules.Skip(1).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId2 = module.Id;
                    }
                    else
                    {
                        moduleId2 = moduleId1;
                    }

                    module = context.Modules.Skip(2).FirstOrDefault();
                    if (module != null)
                    {
                        moduleId3 = module.Id;
                    }
                    else
                    {
                        moduleId3 = moduleId1;
                    }
                }

                // Use the Videos entity to add video data to the Videos table in the database if no data has been added
                if (!context.Videos.Any())
                {
                    var videos = new List <Video>
                    {
                        new Video {
                            ModuleId    = moduleId1, CourseId = courseId1,
                            Position    = 1, Title = "Video 1 Title",
                            Description = description.Substring(1, 35),
                            Duration    = 50, Thumbnail = "/images/video1.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video
                        {
                            ModuleId    = moduleId1,
                            CourseId    = courseId1,
                            Position    = 2,
                            Title       = "Video 2 Title",
                            Description = description.Substring(5, 35),
                            Duration    = 45,
                            Thumbnail   = "/images/video2.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video
                        {
                            ModuleId    = moduleId1,
                            CourseId    = courseId1,
                            Position    = 3,
                            Title       = "Video 3 Title",
                            Description = description.Substring(10, 35),
                            Duration    = 41,
                            Thumbnail   = "/images/video3.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video
                        {
                            ModuleId    = moduleId3,
                            CourseId    = courseId2,
                            Position    = 1,
                            Title       = "Video 4 Title",
                            Description = description.Substring(15, 35),
                            Duration    = 41,
                            Thumbnail   = "/images/video4.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        },
                        new Video
                        {
                            ModuleId    = moduleId2,
                            CourseId    = courseId1,
                            Position    = 1,
                            Title       = "Video 5 Title",
                            Description = description.Substring(20, 35),
                            Duration    = 42,
                            Thumbnail   = "/images/video5.jpg",
                            Url         = "https://www.youtube.com/watch?v=BJFyzpBcaCY"
                        }
                    };
                    context.Videos.AddRange(videos);
                    context.SaveChanges();
                }

                // 17. Use the Downloads entity to add download data to the Downloads table in the database if no data has been added
                if (!context.Downloads.Any())
                {
                    var downloads = new List <Download>
                    {
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        },
                        new Download {
                            ModuleId = moduleId1, CourseId = courseId1,
                            Title    = "ADO.NET 2 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        },
                        new Download {
                            ModuleId = moduleId3, CourseId = courseId2,
                            Title    = "ADO.NET 1 (PDF)", Url = "https://1drv.ms/b/s!AuD5OaH0ExAwn48rX9TZZ3kAOX6Peg"
                        }
                    };
                    context.Downloads.AddRange(downloads);
                    context.SaveChanges();
                }
            }
        }