Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/CatalogSync/FastSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public static async Task SynchronizeAsync(IScraper scraper, ApplicationDbContext
private Dictionary<string, DatabaseCampus> dbCachedCampuses =
new Dictionary<string, DatabaseCampus>();

private Dictionary<(string number, string title), DatabaseCourse> dbCachedCourses =
new Dictionary<(string number, string title), DatabaseCourse>();
private Dictionary<(string subjectCode, string number, string title), DatabaseCourse>
dbCachedCourses =
new Dictionary<(string subjectCode, string number, string title), DatabaseCourse>();

private Dictionary<(Guid campusId, string buildingCode), DatabaseBuilding> dbCachedBuildings
= new Dictionary<(Guid campusId, string buildingCode), DatabaseBuilding>();
Expand Down Expand Up @@ -374,6 +375,7 @@ private void InternalSynchronizeClass(DatabaseTerm term, DatabaseSubject subject
.ToList();
if (dbSections.Count == 0)
{
// The sections don't exist, so the class doesn't either.
var newClass = new DatabaseClass()
{
Id = Guid.NewGuid(),
Expand All @@ -386,7 +388,19 @@ private void InternalSynchronizeClass(DatabaseTerm term, DatabaseSubject subject
}
else
{
// At least one of the sections exists in the database.
classId = dbSections.First().ClassId;

// Detect if the section's Class has moved to a different Course
// (handle the rare case where a CRN gets moved to a different Course)
var dbSectionGroupClass = dbContext.Classes.Single(c => c.Id == classId);
if (dbSectionGroupClass.CourseId != course.Id)
{
dbSectionGroupClass.CourseId = course.Id;
dbContext.Entry(dbSectionGroupClass)
.Property(s => s.CourseId).CurrentValue = course.Id;
dbContext.Entry(dbSectionGroupClass).State = EntityState.Modified;
}
}

// Hydrate each section
Expand Down Expand Up @@ -438,7 +452,8 @@ private DatabaseCourse FetchOrAddCourse(DatabaseSubject subject, string courseNu
string courseTitle, double creditHours, string courseDescription)
{
DatabaseCourse course;
var courseKey = (number: courseNumber, title: courseTitle);
var courseKey = (subjectCode: subject.Abbreviation,
number: courseNumber, title: courseTitle);
if (dbCachedCourses.ContainsKey(courseKey))
{
course = dbCachedCourses[courseKey];
Expand Down
Loading