Skip to content

Instantly share code, notes, and snippets.

@jon-bell
Created February 10, 2022 15:37
Show Gist options
  • Save jon-bell/2b12da184c20daf9538a1427c8e0b2dd to your computer and use it in GitHub Desktop.
Save jon-bell/2b12da184c20daf9538a1427c8e0b2dd to your computer and use it in GitHub Desktop.
//Solution for way 1. This approach will enforce that students and their grades are created in order of appearance in the grades array:
export async function importGrades(gradesToImport: ImportTranscript[]): Promise<Transcript[]> {
const insertedIDs = [];
for (const student of gradesToImport) {
const newStudent = await client.addStudent(student.studentName);
insertedIDs.push(newStudent.studentID);
for (const courseGrade of student.grades) {
await client.addGrade(newStudent.studentID, courseGrade.course, courseGrade.grade);
}
}
const transcripts = [];
for (const studentID of insertedIDs) {
transcripts.push(await client.getTranscript(studentID));
}
return transcripts;
}
//Solution for way 2: This enforces that each students’ grades appear in order of appearance in their grades array, but students might be created out of order
async function importGradesWay2(grades: ImportTranscript[]) {
const insertedIDs = [];
const promises = grades.map(async (student) => {
const newStudent = await client.addStudent(student.studentName);
insertedIDs.push(newStudent.studentID);
for (const courseGrade of student.grades) {
await client.addGrade(newStudent.studentID, courseGrade.course, courseGrade.grade);
}
});
await Promise.all(promises);
const transcripts = [];
for(const studentID of insertedIDs){
transcripts.push(await client.getTranscript(studentID));
}
return transcripts;
}
//Solution for way 3: Does not enforce ordering of classes or grades. However, it’s the fastest.
async function importGradesWay3(grades: ImportTranscript[]) {
const insertedIDs = [];
const promises = grades.map(async (student) => {
const newStudent = await client.addStudent(student.studentName);
insertedIDs.push(newStudent.studentID);
await Promise.all(student.grades.map(courseGrade => client.addGrade(newStudent.studentID, courseGrade.course, courseGrade.grade)));
});
await Promise.all(promises);
const transcripts = [];
for(const studentID of insertedIDs){
transcripts.push(await client.getTranscript(studentID));
}
return transcripts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment