private static void SaveReferenceDataPayload(ManageCoursesDbContext context)
        {
            var users = new List <User>
            {
                new User
                {
                    FirstName = "FirstName_1",
                    LastName  = "LastName_1",
                    Email     = TestUserEmail1
                },
                new User
                {
                    FirstName = "FirstName_2",
                    LastName  = "LastName_2",
                    Email     = TestUserEmail2
                },
                new User
                {
                    FirstName = "FirstName_3",
                    LastName  = "LastName_3",
                    Email     = TestUserEmail3
                }
            };
            const string orgId2        = "OrgId_2";
            var          organisations = new List <Organisation> {
                new Organisation {
                    OrgId = OrgId1
                },
                new Organisation {
                    OrgId = orgId2
                }
            };

            var providers = new List <Provider>
            {
                new ProviderBuilder().WithCode(InstCode1),
                new ProviderBuilder().WithCode(InstCode2),
            };

            var organisationProviders = new List <OrganisationProvider>
            {
                new OrganisationProvider {
                    Provider     = providers[1],
                    Organisation = organisations[1],
                }
            };
            var organisationUsers = new List <OrganisationUser>
            {
                new OrganisationUser {
                    User = users[1],
                },
                new OrganisationUser {
                    User         = users[2],
                    Organisation = organisations[0]
                }
            };

            context.Users.AddRange(users);
            context.Organisations.AddRange(organisations);
            context.Providers.AddRange(providers);
            context.OrganisationUsers.AddRange(organisationUsers);
            context.OrganisationProviders.AddRange(organisationProviders);
            context.Save();
        }
Esempio n. 2
0
        /// <summary>
        /// Processes data in the payload object into the database as an upsert/delta
        /// </summary>
        public void UpdateUcasData()
        {
            var allCampusesGrouped = payload.Campuses.GroupBy(x => x.InstCode).ToDictionary(x => x.Key);
            var ucasSubjects       = payload.Subjects.ToList();
            var pgdeCourses        = new List <PgdeCourse>();

            var ucasCourseGroupings        = payload.Courses.GroupBy(x => x.InstCode).ToDictionary(x => x.Key);
            var ucasCourseSubjectGroupings = payload.CourseSubjects.GroupBy(x => x.InstCode).ToDictionary(x => x.Key);

            _logger.Warning("Beginning UCAS import");
            _logger.Information($"Upserting {payload.Institutions.Count()} institutions");

            var allSubjects = new Dictionary <string, Subject>();

            MigrateOnce("upsert subjects", () =>
            {
                foreach (var s in payload.Subjects)
                {
                    var savedSubject = UpsertSubject(new Subject
                    {
                        SubjectName = s.SubjectDescription,
                        SubjectCode = s.SubjectCode
                    });
                    allSubjects[savedSubject.SubjectCode] = savedSubject;
                }
            });

            var providerCache = _context.Providers
                                .Include(x => x.Sites)
                                .Include(x => x.Courses)
                                .ToDictionary(p => p.ProviderCode);

            MigratePerProvider("upsert providers", providerCache, inst =>
            {
                var savedProvider = UpsertProvider(ToProvider(inst), providerCache);
                _context.Save();
                // update/add cached copy
                providerCache[savedProvider.ProviderCode] = savedProvider;
            });

            var courseLoader = new CourseLoader(providerCache, allSubjects, pgdeCourses, _clock);


            MigratePerProvider("drop-and-create sites and courses", providerCache, ucasInst =>
            {
                var inst = providerCache[ucasInst.InstCode];

                DeleteForProvider(inst.ProviderCode);
                _context.Save();

                var campuses             = allCampusesGrouped.ContainsKey(inst.ProviderCode) ? allCampusesGrouped[inst.ProviderCode] : null;
                IEnumerable <Site> sites = new List <Site>();
                if (campuses != null)
                {
                    inst.Sites = inst.Sites ?? new Collection <Site>();
                    sites      = campuses.Select(x => ToSite(x)).ToList();
                    foreach (var site in (IEnumerable <Site>)sites)
                    {
                        inst.Sites.Add(site);
                        site.Provider = inst;
                    }
                    _context.Save();
                }

                var allCoursesForThisProvider = courseLoader.LoadCourses(
                    inst,
                    ucasCourseGroupings.GetValueOrDefault(inst.ProviderCode).AsEnumerable() ?? new List <UcasCourse>(),
                    ucasCourseSubjectGroupings.GetValueOrDefault(inst.ProviderCode).AsEnumerable() ?? new List <UcasCourseSubject>(),
                    sites);

                inst.Courses = new Collection <Course>(allCoursesForThisProvider.ToList());
                _context.Save();
            });

            _logger.Warning("Completed UCAS import");
        }