public DataTransformer(SnappetChallengeContext context)
 {
     _context = context;
     domains = new List<Domain>();
     students = new List<Student>();
     exercises = new List<Exercise>();
     subjects = new List<Subject>();
     objectives = new List<Objective>();
 }
        public override void Import(SnappetChallengeContext context)
        {
            var assembly = Assembly.GetExecutingAssembly();
            const string resourceName = "SnappetChallenge.DAL.DataImport.work.json";  // serialized JSON is embedded resource
            var itemsToInsert = new List<StudentAnswer>();

            using (var streamFromEmbeddedResource = assembly.GetManifestResourceStream(resourceName))
            using (var sr = new StreamReader(streamFromEmbeddedResource))
            using (var reader = new JsonTextReader(sr))
            {
                while (reader.Read())
                {
                    if (reader.TokenType != JsonToken.StartObject) continue;
                    var obj = JObject.Load(reader);

                    var studentAnswer = new StudentAnswer
                    {
                        Correct = obj["Correct"].Value<int>() == 1, // explicit cast to bool
                        DateAdded = DateTime.Now,
                        Difficulty =
                            obj["Difficulty"].Value<string>().Equals("NULL") // handle weird NULL values, they serialize as string "NULL"
                                ? null
                                : obj["Difficulty"].Value<double?>(),
                        Domain = obj["Domain"].Value<string>(),
                        ExerciseId = obj["ExerciseId"].Value<long>(),
                        LearningObjective = obj["LearningObjective"].Value<string>(),
                        Progress = obj["Progress"].Value<int>(),
                        Subject = obj["Subject"].Value<string>(),
                        SubmitDateTime = obj["SubmitDateTime"].Value<DateTime>(),
                        SubmittedAnswerId = obj["SubmittedAnswerId"].Value<long>(),
                        UserId = obj["UserId"].Value<long>(),
                        Id = obj["SubmittedAnswerId"].Value<long>() // this is doubled in SubmittedAnswerId..
                    };

                    itemsToInsert.Add(studentAnswer);
                }
            }
            context.BulkInsert(itemsToInsert);
            context.SaveChanges();
        }
 public abstract void Import(SnappetChallengeContext context);
        private static void CreateDatabase(IList<SnappetChallengeRecord> data)
        {
            var subjects =
                (
                    from record in data
                    group record by record.Subject
                    into grp
                    select new Subject { Description = grp.Key }
                ).ToList();

            var domains =
                (
                   from record in data
                   group record by new { record.Subject, record.Domain }
                   into grp
                   select new Domain.Entities.Domain { Description = grp.Key.Domain, Subject = subjects.FirstOrDefault(s => s.Description == grp.Key.Subject) }
                ).ToList();

            // Include subject in the group by, because domain name is not guaranteed to be unique
            var objectives =
                (
                    from record in data
                    group record by new { record.Subject, record.Domain, record.LearningObjective }
                    into grp
                    select new LearningObjective
                    {
                        Description = grp.Key.LearningObjective,
                        Domain = domains.FirstOrDefault(d => d.Description == grp.Key.Domain && d.Subject.Description == grp.Key.Subject)
                    }
                ).ToList();

            var exercises =
                (
                    from record in data
                    group record by new { record.LearningObjective, record.ExerciseId }
                    into grp
                    select new Exercise
                    {
                        Difficulty = grp.First().Difficulty,
                        LearningObjective = objectives.FirstOrDefault(o => o.Description == grp.Key.LearningObjective),
                        ExternalId = grp.First().ExerciseId
                    }
                ).ToList();

            int i = 0;

            var users =
                (
                    from record in data
                    group record by new { record.UserId }
                    into grp
                    select new User
                    {
                        ExternalId = grp.Key.UserId,
                        Name = names[i++]
                    }
                )
                .ToList();

            var submittedAnswers =
                (
                    from record in data
                    group record by new { record.ExerciseId, record.SubmittedAnswerId, record.UserId }
                    into grp
                    select new SubmittedAnswer
                    {
                        IsCorrect = grp.First().Correct,
                        SubmittedOn = grp.First().SubmitDateTime,
                        Progress = grp.First().Progress,
                        Exercise = exercises.First(e => e.ExternalId == grp.Key.ExerciseId),
                        SubmittedBy = users.First(u => u.ExternalId == grp.Key.UserId)
                    }
                )
                .ToList();

            // for the sake of brevity I'm skipping the dependency inversion here
            using (var context = new SnappetChallengeContext())
            {
                context.GetRepository<Subject>().AddRange(subjects);
                context.GetRepository<Domain.Entities.Domain>().AddRange(domains);
                context.GetRepository<LearningObjective>().AddRange(objectives);
                context.GetRepository<Exercise>().AddRange(exercises);
                context.GetRepository<User>().AddRange(users);
                context.GetRepository<SubmittedAnswer>().AddRange(submittedAnswers);

                context.Commit();
            }

            Console.WriteLine("Database created succesfully");
        }