private static void ImportUsers(JToken userObj)
        {
            var context = new MoviesContext();
            var user = new User();

            user.UserName = userObj["username"].Value<string>();

            if (userObj["age"].Type != JTokenType.Null)
            {
                user.Age = userObj["age"].Value<int>();
            }

            if (userObj["email"].Type != JTokenType.Null)
            {
                user.Email = userObj["email"].Value<string>();
            }

            if (userObj["country"].Type != JTokenType.Null)
            {
                var countryName = userObj["country"].Value<string>();
                user.Country = context.Countries.FirstOrDefault(c => c.Name == countryName);
            }

            context.Users.Add(user);
            context.SaveChanges();
        }
 static void Main()
 {
     Database.SetInitializer(new MoviesMigrationStrategy());
     var context = new MoviesContext();
     var moviesCount = context.Movies.Count();
     Console.WriteLine(moviesCount);
 }
        static void Main()
        {
            var context = new MoviesContext();
            Console.WriteLine(context.Movies.Count());

            //1.Adult Movies
            var adultMovies = context.Movies
                .Where(m => m.AgeRestriction == AgeRestriction.Adult)
                .OrderBy(m => m.Title)
                .ThenBy(m => m.Ratings.Count)
                .Select(m => new
                {
                    title = m.Title,
                    ratingsGiven = m.Ratings.Count
                })
                .ToList();

            var jsonAdultMovies = new JavaScriptSerializer().Serialize(adultMovies);
            File.WriteAllText("../../adult-movies.json", jsonAdultMovies);

            //2.Rated Movies by User
            var ratedMoviesUser = context.Users
                .Where(u => u.UserName == "jmeyery")
                .Select(u => new
                {
                    username = u.UserName,
                    ratedMovies = u.Ratings
                        .OrderBy(r => r.Movie.Title)
                        .Select(r => new
                        {
                            title = r.Movie.Title,
                            userRating = r.Stars,
                            averageRating = r.Movie.Ratings.Average(s => s.Stars)
                        })
                })
                .ToList();

            string ratedMoviesUserJson = new JavaScriptSerializer().Serialize(ratedMoviesUser);
            File.WriteAllText("../../rated-movies-by-jmeyery.json", ratedMoviesUserJson);

            //3.Top 10 Favourite Movies
            var topMovies = context.Movies
                .Where(m => m.AgeRestriction == AgeRestriction.Teen)
                .OrderByDescending(m => m.Users.Count)
                .ThenBy(m => m.Title)
                .Select(m => new
                {
                    isbn = m.Isbn,
                    title = m.Title,
                    favouritedBy = m.Users
                        .Select(u => u.UserName)
                })
                .Take(10)
                .ToList();

            string topFavouriteMoviesJson = new JavaScriptSerializer().Serialize(topMovies);
            File.WriteAllText("../../top-10-favourite-movies.json", topFavouriteMoviesJson);
        }
        static void Main()
        {
            var context = new MoviesContext();
            var jsSerializer = new JavaScriptSerializer();

            // Adult Movies
            var adultMovies = context.Movies
                .Where(m => m.AgeRestriction == AgeRestriction.Adult)
                .Select(m => new
                {
                    title = m.Title,
                    ratingsGiven = m.Ratings.Count()
                })
                .ToList();

            var adultMoviesJson = jsSerializer.Serialize(adultMovies);
            File.WriteAllText("adult-movies.json", adultMoviesJson);
            Console.WriteLine("Adult Movies exported to adult-movies.json.");

            // Rated Movies by User jmeyery
            var ratedMoviesByUser = context.Users
                .Where(u => u.UserName == "jmeyery")
                .Select(u => new
                {
                    username = u.UserName,
                    ratedMovies = u.Ratings
                        .Select(r => new
                        {
                            title = r.Movie.Title,
                            userRaiting = r.Stars,
                            averageRating = r.Movie.Ratings.Average(m => m.Stars)
                        })
                });

            var jsonMovies = jsSerializer.Serialize(ratedMoviesByUser);
            File.WriteAllText("rated-movies-by-jmeyery.json", jsonMovies);
            Console.WriteLine("Rated Movies by User jmeyery exported to rated-movies-by-jmeyery.json.");

            // Top 10 Favourite Movies
            var topFavMovies = context.Movies
                .Where(m => m.AgeRestriction == AgeRestriction.Teen)
                .OrderByDescending(m => m.Users.Count())
                .ThenBy(m => m.Title)
                .Select(m => new
                {
                    isbn = m.Isbn,
                    title = m.Title,
                    favouritedBy = m.Users.Select(u => u.UserName)
                })
                .Take(10)
                .ToList();

            var jsonTopFavMovies = jsSerializer.Serialize(topFavMovies);
            File.WriteAllText("top-10-favourite-movies.json", jsonTopFavMovies);
            Console.WriteLine("Top 10 Favourite Movies exported to top-10-favourite-movies.json.");
        }
        private static void ImportCountries(JToken countryObj)
        {
            var context = new MoviesContext();
            var country = new Country();

            country.Name = countryObj["name"].Value<string>();

            context.Countries.Add(country);
            context.SaveChanges();
        }
        private static void ImportMovies(JToken movieObj)
        {
            var context = new MoviesContext();
            var movie = new Movie();

            movie.Isbn = movieObj["isbn"].Value<string>();
            movie.Title = movieObj["title"].Value<string>();
            movie.AgeRestriction = (AgeRestriction)Enum.Parse(typeof(AgeRestriction),
                movieObj["ageRestriction"].ToString());
            context.Movies.Add(movie);
            context.SaveChanges();
        }
        private static void ImportMovieRatings(JToken elementObj)
        {
            var context = new MoviesContext();

            string userName = elementObj["user"].Value<string>();
            string movie = elementObj["movie"].Value<string>();
            int stars = int.Parse(elementObj["rating"].ToString());

            context.Ratings.Add(new Rating()
            {
                Stars = stars,
                MovieId = context.Movies.FirstOrDefault(m => m.Isbn == movie).Id,
                UserId = context.Users.FirstOrDefault(u => u.UserName == userName).Id
            });

            context.SaveChanges();
        }
        private static void ImportFavouriteMovies(JToken favouriteMovieObj)
        {
            var context = new MoviesContext();

            string userName = favouriteMovieObj["username"].Value<string>();
            var dbUser = context.Users.FirstOrDefault(u => u.UserName == userName);

            Console.WriteLine("User {0} movies imported: ", favouriteMovieObj["username"].Value<string>());
            var moviesIsbn = JArray.Parse(favouriteMovieObj["favouriteMovies"].ToString());

            foreach (var movie in moviesIsbn)
            {
                string currMovie = movie.ToString();
                Movie newMovie = context.Movies.FirstOrDefault(m => m.Isbn == currMovie);
                dbUser.Movies.Add(newMovie);
                Console.WriteLine(newMovie.Title);
            }

            context.SaveChanges();
        }
        static void Main()
        {
            var context = new MoviesContext();

            Console.WriteLine(context.Countries.Count());

            var countriesJson = File.ReadAllText(@"..\..\countries.json");
            var countries = JArray.Parse(countriesJson);
            foreach (var country in countries)
            {
                ImportCountries(country);
                Console.WriteLine("Country {0} imported", country["name"]);
            }

            var usersJson = File.ReadAllText(@"..\..\users.json");
            var users = JArray.Parse(usersJson);
            foreach (var user in users)
            {
                ImportUsers(user);
                Console.WriteLine("User {0} imported", user["username"]);
            }

            var moviesJson = File.ReadAllText(@"..\..\movies.json");
            var movies = JArray.Parse(moviesJson);
            foreach (var movie in movies)
            {
                ImportMovies(movie);
                Console.WriteLine("Movie {0} imported", movie["title"]);
            }

            var favouriteMoviesJson = File.ReadAllText(@"..\..\users-and-favourite-movies.json");
            var favouriteMovies = JArray.Parse(favouriteMoviesJson);
            foreach (var favouriteMovie in favouriteMovies)
            {
                ImportFavouriteMovies(favouriteMovie);
                Console.WriteLine();
            }

            var movieRatingsJson = File.ReadAllText(@"..\..\movie-ratings.json");
            var movieRatings = JArray.Parse(movieRatingsJson);
            foreach (var element in movieRatings)
            {
                ImportMovieRatings(element);
                Console.WriteLine("Imported data to database");
            }
        }