From 3bec0b8c089c54859882ee8bf1cb16467ec13404 Mon Sep 17 00:00:00 2001 From: Mark Stenglein Date: Fri, 30 Dec 2016 01:11:25 -0500 Subject: [PATCH] Add the majority of the scaffolding to get the class data out to the ical builder - next step is to actually write the ical generator :dancing_penguin: - I'm committing this through WebStorm's UI to try it out so I've no idea what could go wrong....just a heads up --- package.json | 5 +++ routes/api/v1.js | 91 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 293e769..2b1c241 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,11 @@ }, "devDependencies": { "chai": "^3.5.0", + "eslint-config-standard": "^6.2.1", + "eslint-config-standard-jsx": "^3.2.0", + "eslint-plugin-promise": "^3.4.0", + "eslint-plugin-react": "^6.8.0", + "eslint-plugin-standard": "^2.0.1", "husky": "^0.11.9", "lint-staged": "^3.2.1", "mocha": "^3.0.2", diff --git a/routes/api/v1.js b/routes/api/v1.js index 1525829..565a970 100644 --- a/routes/api/v1.js +++ b/routes/api/v1.js @@ -66,21 +66,90 @@ router.get('/json/classes/crn/:CRN', function (req, res, next) { // //////////////////////////////////////////////////////////////////////////// // ICAL API Section -router.get('/ical/:SCHOOL/:SEMSLUG/:CLASSES', function (req, res, next) { -/* +router.get('/ical/:SCHOOL/:SEMSLUG/:SECTIONS', function (req, res, next) { + + // Extract arguments and setup empty objects for them + + // Get School Information var schoolSlug = req.params['SCHOOL'] + var school = db.University + .findOne({ + 'attributes': ['slug', 'name', 'website'], + 'where': { + 'slug': schoolSlug + } + }) + + // Get Semester Information var semSlug = req.params['SEMSLUG'] - var classes = req.params['CLASSES'] -*/ - // Generate blank calendar - var cal = ical({domain: 'schedules.gmu.edu', name: 'SRCT Schedules Generated Calendar'}) - - res.set({ - 'Content-Type': 'text/calendar; charset=utf-8', - 'Content-Disposition': 'attachment; filename="' + ('calendar.ics') + '"' + var semester = db.Semester + .findOne({ + 'attributes': ['slug', 'name', 'university'], + 'where': { + 'slug': semSlug + } + }) + + // Get Section Information + var sectionArgs = req.params['SECTIONS'] + var semesterSlugs = [] + + // Parse Section Args + sectionArgs.split(',').forEach(function (sectionArg) { + semesterSlugs.push(db.sequelize.escape(sectionArg)) }) - res.send(cal.toString()) + var sections = db.Section + .findAll({ + 'where': { + 'semester': semSlug, + 'crn': { + $in: semesterSlugs + } + } + }) + + var makeCalendar = function(school, semester, sections) { + // Generate blank calendar + var cal = ical( + { + domain: 'schedules.gmu.edu', + prodId: '//Student Run Computing and Technology//Schedules//EN', + name: school.get('slug') + ' Class Schedule Fall 2016' + } + ) + + return cal + } + + // Wait for all the queries to finish + Promise + .all( + [school, semester, sections] + ).then(function (queries) { + // Check for invalid responses and throw appropriate errors + if (!queries[0]) { + throw 'Invalid School Given!' + } + if (!queries[1]) { + throw 'Invalid Semester Given' + } + + // Generate the calendar + var semesterCalendar = makeCalendar(queries[0], queries[1], queries[2]) + + res.set({ + 'Content-Type': 'text/calendar; charset=utf-8', + 'Content-Disposition': 'attachment; filename="' + ('calendar.ics') + '"' + }) + + res.send(semesterCalendar.toString()) + }) + // Catch any errors and respond with grace + .catch(function (error) { + console.log(error) + res.send(error) + }) }) module.exports = router -- GitLab