diff --git a/src/controllers/gym/subjects.js b/src/controllers/gym/subjects.js index a59c31f..4a8cce8 100644 --- a/src/controllers/gym/subjects.js +++ b/src/controllers/gym/subjects.js @@ -18,9 +18,7 @@ router.get('/', (req, res) => { const file = rootPath + '/subjectsAndCourses/amnen_och_kurser.xml' parseString(fs.readFileSync(file).toString(), (err, result) => { - const subjects = formatter.formatSubjects(result.SubjectsAndCourses.subject) - - res.json(subjects) + res.json(formatter.formatSubjects(result.SubjectsAndCourses.subject)) }) }) @@ -40,21 +38,7 @@ router.get('/:code', (req, res) => { } parseString(fs.readFileSync(file), (err, result) => { - const turndownService = new Turndown() - const data = result.subject - const subject = { - name: data.name[0], - code: data.code[0], - skolfs: data.skolfsId[0], - applianceDate: data.applianceDate[0], - typeOfSchooling: data.typeOfSchooling[0], - category: data.category[0], - description: turndownService.turndown(data.description[0]), - purpose: turndownService.turndown(data.purpose[0]), - courses: formatter.formatCourses(data.courses) - } - - res.json(subject) + res.json(formatter.formatSubjectData(result.subject)) }) } }) diff --git a/src/controllers/gymsar/index.js b/src/controllers/gymsar/index.js index 4ac2058..5d87f29 100644 --- a/src/controllers/gymsar/index.js +++ b/src/controllers/gymsar/index.js @@ -6,6 +6,7 @@ const router = require('express').Router() // Sub routes router.use('/programmes', require('./programmes')) +router.use('/subjects', require('./subjects')) router.get('/', (req, res) => { res.json({ diff --git a/src/controllers/gymsar/programmes.js b/src/controllers/gymsar/programmes.js index 182ef67..2798657 100644 --- a/src/controllers/gymsar/programmes.js +++ b/src/controllers/gymsar/programmes.js @@ -1,4 +1,5 @@ // Route manager for /gymsar/programmes +// Data folder: gys // Dependencies const router = require('express').Router() @@ -33,22 +34,24 @@ router.get('/:code', (req, res) => { const code = req.params.code.toLowerCase() parseString(fs.readFileSync(programmeFile).toString(), (err, result) => { - let programme + let validCode = false + result.ProgramsAndOrientations.program.forEach(data => { if (data.code[0].toLowerCase() === code) { - const fileName = latinize(data.name[0]) + '.xml' - const filePath = rootPath + '/program/' + fileName - parseString(fs.readFileSync(filePath).toString(), (err, result) => { - programme = formatter.formatProgramme(result.program) + validCode = true + + let file = rootPath + '/program/' + latinize(data.name[0]) + '.xml' + while (file.indexOf('–') > -1) { + file = file.replace('–', '-') + } + + parseString(fs.readFileSync(file).toString(), (err, result) => { + res.json(formatter.formatProgramme(result.program)) }) } }) - if (programme) { - res.json(programme) - } else { - res.status(404).send('Not found') - } + if (!validCode) res.status(404).send('Not found') }) }) diff --git a/src/controllers/gymsar/subjects.js b/src/controllers/gymsar/subjects.js new file mode 100644 index 0000000..cc64929 --- /dev/null +++ b/src/controllers/gymsar/subjects.js @@ -0,0 +1,50 @@ +// Route manager for /gymsar/subjects +// Data folder: gys + +// Dependencies +const router = require('express').Router() +const path = require('path') +const fs = require('fs') +const { parseString } = require('xml2js') + +// Helpers +const formatter = require('../../helpers/gymsar/formatter') +const latinize = require('../../helpers/latinize') + +const rootPath = path.resolve(__dirname + '/../../../files/gys') + +router.get('/', (req, res) => { + const file = rootPath + '/subjectsAndCourses/amnen_och_kurser.xml' + + parseString(fs.readFileSync(file).toString(), (err, result) => { + res.json(formatter.formatSubjects(result.SubjectsAndCourses.subject)) + }) +}) + +router.get('/:code', (req, res) => { + const code = req.params.code.toLowerCase() + const file = rootPath + '/subjectsAndCourses/amnen_och_kurser.xml' + + parseString(fs.readFileSync(file).toString(), (err, result) => { + let validCode = false + + result.SubjectsAndCourses.subject.forEach(subject => { + if (subject.code[0].toLowerCase() === code) { + validCode = true + + let file = rootPath + '/subject/' + latinize(subject.name[0]) + '.xml' + while (file.indexOf('–') > -1) { + file = file.replace('–', '-') + } + + parseString(fs.readFileSync(file), (err, result) => { + res.json(formatter.formatSubjectData(result.subject)) + }) + } + }) + + if (!validCode) res.status(404).send('Not found.') + }) +}) + +module.exports = router diff --git a/src/helpers/gym/formatter.js b/src/helpers/gym/formatter.js index fb82d86..7439eeb 100644 --- a/src/helpers/gym/formatter.js +++ b/src/helpers/gym/formatter.js @@ -170,6 +170,20 @@ function Formatter() { return subjects } + function formatSubjectData(data) { + return { + name: data.name[0], + code: data.code[0], + skolfs: data.skolfsId[0], + applianceDate: data.applianceDate[0], + typeOfSchooling: data.typeOfSchooling[0], + category: data.category[0], + description: turndownService.turndown(data.description[0]), + purpose: turndownService.turndown(data.purpose[0]), + courses: formatCourses(data.courses) + } + } + function formatCourses(courseList) { const courses = [] courseList.forEach(courseData => { @@ -209,7 +223,8 @@ function Formatter() { formatProfiles, formatCourses, formatCourseData, - formatSubjects + formatSubjects, + formatSubjectData } } diff --git a/src/helpers/gymsar/formatter.js b/src/helpers/gymsar/formatter.js index 5430bd0..69dab30 100644 --- a/src/helpers/gymsar/formatter.js +++ b/src/helpers/gymsar/formatter.js @@ -170,6 +170,20 @@ function Formatter() { return subjects } + function formatSubjectData(data) { + return { + name: data.name[0], + code: data.code[0], + skolfs: data.skolfsId[0], + applianceDate: data.applianceDate[0], + typeOfSchooling: data.typeOfSchooling[0], + category: data.category[0], + description: turndownService.turndown(data.description[0]), + purpose: turndownService.turndown(data.purpose[0]), + courses: formatCourses(data.courses) + } + } + function formatCourses(courseList) { const courses = [] courseList.forEach(courseData => { @@ -204,7 +218,9 @@ function Formatter() { } return { - formatProgramme + formatProgramme, + formatSubjects, + formatSubjectData } }