public void SyncStudents(District district, School school, IList<SISStudent> sisStudents) { var filter = Builders<Student>.Filter; var update = Builders<Student>.Update; // First mark all students in the school as volatile _collection.UpdateMany( filter.Eq(x => x.School.Id, school.Id), update.Set(x => x.Volatile, true)); // Now update all students accordingly var models = new List<UpdateOneModel<Student>>(); foreach (var sisStudent in sisStudents) { var model = new UpdateOneModel<Student>( filter.Eq(student => student.ExternalId, sisStudent.Data.Id), update .Set(student => student.Active, true) .Set(student => student.District, district) .Set(student => student.ExternalId, sisStudent.Data.Id) .Set(student => student.School, school) .Set(student => student.SISData, sisStudent) .Set(student => student.Volatile, false)); model.IsUpsert = true; models.Add(model); } _collection.BulkWrite(models); // Lastly inactivate any old schools _collection.UpdateMany( filter.Eq(x => x.School.Id, school.Id) & filter.Eq(x => x.Volatile, true), update .Set(x => x.Active, false) .Set(x => x.Volatile, false)); }
public void Run() { // Call Clever to get list of all districts that share var sisDistrictsResp = JsonConvert.DeserializeObject<GetDistrictsResponse>(File.ReadAllText(@"C:\dev\kulepool\Server\SISSyncConsole\districts.json")); // Query the DB to get list of existing districts var dbDistricts = _districtsRepo.List(); // Loop through the other way foreach (var sisDistrict in sisDistrictsResp.Data) { // Match the corresponding db district var district = dbDistricts.FirstOrDefault(x => x.SISData.Data.Id.Equals(sisDistrict.Data.Id)); if (district == null) { district = new District { ExternalId = sisDistrict.Data.Id, SISData = sisDistrict, SyncState = new SyncState { FullSync = true } }; _districtsRepo.Save(district); } if (district.SyncState.FullSync || string.IsNullOrWhiteSpace(district.SyncState.BookmarkId)) { var sisSchools = JsonConvert.DeserializeObject<GetSchoolsResponse>(File.ReadAllText(@"C:\dev\kulepool\Server\SISSyncConsole\schools.json")).Data; _schoolsRepo.SyncSchools(district, sisSchools); var dbSchools = _schoolsRepo.List($"{{'district._id':ObjectId('{district.Id}')}}"); foreach (var school in dbSchools) { var sisStudents = JsonConvert.DeserializeObject<GetStudentsResponse>(File.ReadAllText(@"C:\dev\kulepool\Server\SISSyncConsole\students.json")) .Data.Where(x => x.Data.School.Equals(school.SISData.Data.Id)).ToList(); // simulate getting students from each school _studentsRepo.SyncStudents(district, school, sisStudents); } // Now mark the district as syncing via events // todo } else { // Sync Events } } // Update the list of existing districts /*dbDistricts = _districtsRepo.List(); foreach (var district in dbDistricts) { // Match the corresponding clever district var sisDistrict = sisDistrictsResp.Data.FirstOrDefault(x => x.Data.Id.ToLower().Equals(district.Id.ToLower())); if (sisDistrict == null) { // District stopped sharing..? // todo: mark this district as inactive? continue; } }*/ }
public void Save(District district) { if (district.Id.Equals(ObjectId.Empty)) { _collection.InsertOne(district); } else { _collection.ReplaceOne(x => x.Id == district.Id, district, new UpdateOptions { IsUpsert = true }); } }