Forked from Sunflower/gist:d63a352119dc4287557b686704129f77
Created
February 6, 2017 05:29
-
-
Save minitech/08867a9dd8f376e6ffb234e46660fc8c to your computer and use it in GitHub Desktop.
addDataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addDataset(id: string, content: string): Promise<InsightResponse> { | |
return new Promise(function (fulfill, reject) { | |
let zip = new JSZip(); | |
let courseStringsPromises: Promise<string>[] = []; | |
if (id === "" || id === null) { | |
reject({"code": 400, "body": {"error": "invalid id"}}); | |
} | |
if (content === "" || content === null) { | |
reject({"code": 400, "body": {"error": "invalid content"}}); | |
} | |
zip.loadAsync(content, {base64: true}) // returns promise with updated zip object | |
.then(function (zip: JSZip) { | |
zip.forEach(function (relativePath: string, file: JSZipObject) { | |
if (relativePath !== "courses/"){ | |
let courseStringsPromise: Promise<string> = file.async("string"); | |
courseStringsPromises.push(courseStringsPromise); | |
} | |
}); | |
let coursesArray: Course[] = []; | |
Promise.all(courseStringsPromises) | |
.then(function (courseStrings: string[]) { | |
for (let courseString of courseStrings) { | |
if (courseString !== "") { | |
let courseOuterObject: any = JSON.parse(courseString); | |
let courseResultArray = courseOuterObject["result"]; | |
if (courseResultArray.length !== 0) { | |
for (let courseObjectFor of courseResultArray) { | |
let courseObject: any = courseObjectFor; // changes type to any for typescript | |
let course:Course; | |
try { | |
course = { | |
courses_dept: courseObject["Subject"], | |
courses_id: courseObject["Course"], | |
courses_avg: courseObject["Avg"], | |
courses_instructor: courseObject["Professor"], | |
courses_title: courseObject["Title"], | |
courses_pass: courseObject["Pass"], | |
courses_fail: courseObject["Fail"], | |
courses_audit: courseObject["Audit"], | |
courses_uuid: courseObject["id"] | |
}; | |
} catch (err) { | |
console.log("incomplete course data"); | |
} | |
coursesArray.push(course); | |
} | |
} | |
} | |
} | |
if (coursesArray.length === 0){ | |
reject({code: 400, body: {"error": "empty data set."}}); | |
}else{ | |
let coursesArrayJSON = JSON.stringify(coursesArray); | |
if (fs.existsSync("./" + id + ".json")) { | |
fs.writeFileSync("./" + id + ".json", coursesArrayJSON); | |
fulfill({code: 201, body: {}}); | |
} else { | |
fs.writeFileSync("./" + id + ".json", coursesArrayJSON); | |
fulfill({code: 204, body: {}}); | |
} | |
} | |
}) | |
.catch(function (err: any) { | |
console.log("caught all!!"); | |
reject({"code": 400, "body": {"error": "promise.all fail"}}); | |
}); | |
}) | |
.catch(function (err: any) { | |
reject({"code": 400, "body": {"error": "loadAsync error"}}); | |
}); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment