From faf9cab87cac91d84a0291c222f325afc929a9ad Mon Sep 17 00:00:00 2001 From: Max Sandelin Date: Mon, 15 Oct 2018 06:25:54 +0200 Subject: [PATCH] =?UTF-8?q?Added=20final=20data=20set,=20subject-area,=20t?= =?UTF-8?q?o=20'gymsar'=20level,=20completing=20it's=20implementation!=20?= =?UTF-8?q?=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/gymsar/courses.js | 2 +- src/controllers/gymsar/index.js | 3 +- src/controllers/gymsar/subjectAreas.js | 60 ++++++++++++++++++++++++++ src/controllers/index.js | 8 ++-- src/helpers/gym/formatter.js | 10 +++-- src/helpers/gymsar/formatter.js | 37 ++++++++++++---- 6 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 src/controllers/gymsar/subjectAreas.js diff --git a/src/controllers/gymsar/courses.js b/src/controllers/gymsar/courses.js index 812813d..05021b4 100644 --- a/src/controllers/gymsar/courses.js +++ b/src/controllers/gymsar/courses.js @@ -8,7 +8,7 @@ const fs = require('fs') const { parseString } = require('xml2js') // Helpers -const formatter = require('../../helpers/gym/formatter') +const formatter = require('../../helpers/gymsar/formatter') const latinize = require('../../helpers/latinize') const rootPath = path.resolve(__dirname + '/../../../files/gys') diff --git a/src/controllers/gymsar/index.js b/src/controllers/gymsar/index.js index 30570f7..5a66b9e 100644 --- a/src/controllers/gymsar/index.js +++ b/src/controllers/gymsar/index.js @@ -8,11 +8,12 @@ const router = require('express').Router() router.use('/programmes', require('./programmes')) router.use('/subjects', require('./subjects')) router.use('/courses', require('./courses')) +router.use('/subject-areas', require('./subjectAreas')) router.get('/', (req, res) => { res.json({ level: 'Gymnasiesärskolan och särvux gymnasial', - routes: ['/programmes', '/programmes/[code]', '/subjects', '/subjects/[code]', '/courses', '/courses/[code]'] + routes: ['/programmes', '/programmes/[code]', '/subjects', '/subjects/[code]', '/courses', '/courses/[code]', '/subject-areas', '/subject-areas/[code]'] }) }) diff --git a/src/controllers/gymsar/subjectAreas.js b/src/controllers/gymsar/subjectAreas.js new file mode 100644 index 0000000..c8846cf --- /dev/null +++ b/src/controllers/gymsar/subjectAreas.js @@ -0,0 +1,60 @@ +// Route manager for: /gymsar/subject-areas +// Data folder: gys + +// Dependencies +const router = require('express').Router() +const path = require('path') +const fs = require('fs') +const { parseString } = require('xml2js') +const async = require('async') + +// Helpers +const formatter = require('../../helpers/gymsar/formatter') +const latinize = require('../../helpers/latinize') + +const rootPath = path.resolve(__dirname + '/../../../files/gys') + +router.get('/', (req, res) => { + const folder = rootPath + '/subjectArea' + const files = fs.readdirSync(folder) + + let areas = [] + async.each(files, (file, callback) => { + parseString(fs.readFileSync(folder + '/' + file), (err, result) => { + areas.push({ + name: result.subject.name[0], + code: result.subject.code[0] + }) + + callback() + }) + }, (error) => { + res.json(areas) + }) +}) + +router.get('/:code', (req, res) => { + const code = req.params.code.toLowerCase() + const folder = rootPath + '/subjectArea' + const files = fs.readdirSync(folder) + + let area + async.each(files, (file, callback) => { + parseString(fs.readFileSync(folder + '/' + file), (err, result) => { + if (result.subject.code[0].toLowerCase() === code) { + const data = result.subject + area = formatter.formatSubjectData(result.subject) + } + + callback() + }) + }, (error) => { + if (area) { + res.json(area) + } else { + res.status(404).send('Not found') + } + }) +}) + +module.exports = router diff --git a/src/controllers/index.js b/src/controllers/index.js index 30416e6..de96ca8 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -22,12 +22,12 @@ router.get('/', (req, res) => { '/gym

' + 'Obligatoriska skolan
' + - '/gru


' + + '/gru

' + - 'Work-in-progress routes:

' + + 'Gymnasiesärskolan och särvux gymnasial
' + + '/gymsar


' + - 'Gymnasiesärskolan och särvux gymnasial (WIP)
' + - '/gymsar

' + + 'Work-in-progress routes:

' + 'Kommunal vuxenutbildning grundläggande (WIP)
' + '/vuxgru

' + diff --git a/src/helpers/gym/formatter.js b/src/helpers/gym/formatter.js index 7439eeb..34469e4 100644 --- a/src/helpers/gym/formatter.js +++ b/src/helpers/gym/formatter.js @@ -208,10 +208,12 @@ function Formatter() { function formatKnowledgeRequirements(requirementsList) { const requirements = [] requirementsList.forEach(reqData => { - requirements.push({ - grade: reqData.gradeStep[0], - description: turndownService.turndown(reqData.text[0]) - }) + const req = { description: turndownService.turndown(reqData.text[0]) } + + if (reqData.gradeStep) req.grade = reqData.gradeStep[0] + if (reqData.year && typeof reqData.year[0] === 'string') req.year = reqData.year[0] + + requirements.push(req) }) return requirements diff --git a/src/helpers/gymsar/formatter.js b/src/helpers/gymsar/formatter.js index 69dab30..a89c86b 100644 --- a/src/helpers/gymsar/formatter.js +++ b/src/helpers/gymsar/formatter.js @@ -171,7 +171,7 @@ function Formatter() { } function formatSubjectData(data) { - return { + const subject = { name: data.name[0], code: data.code[0], skolfs: data.skolfsId[0], @@ -179,9 +179,13 @@ function Formatter() { typeOfSchooling: data.typeOfSchooling[0], category: data.category[0], description: turndownService.turndown(data.description[0]), - purpose: turndownService.turndown(data.purpose[0]), - courses: formatCourses(data.courses) + purpose: turndownService.turndown(data.purpose[0]) } + if (data.courses) subject.courses = formatCourses(data.courses) + if (data.centralContents) subject.centralContent = formatCentralContent(data.centralContents) + if (data.knowledgeRequirements) subject.knowledgeRequirements = formatKnowledgeRequirements(data.knowledgeRequirements) + + return subject } function formatCourses(courseList) { @@ -205,13 +209,29 @@ function Formatter() { return course } + function formatCentralContent(blocks) { + const content = [] + blocks.forEach(block => { + const row = { + text: turndownService.turndown(block.text[0]) + } + if (block.year && typeof block.year[0] === 'string') row.year = block.year[0] + + content.push(row) + }) + + return content.length > 1 ? content : content[0] + } + function formatKnowledgeRequirements(requirementsList) { const requirements = [] requirementsList.forEach(reqData => { - requirements.push({ - grade: reqData.gradeStep[0], - description: turndownService.turndown(reqData.text[0]) - }) + const req = { description: turndownService.turndown(reqData.text[0]) } + + if (reqData.gradeStep) req.grade = reqData.gradeStep[0] + if (reqData.year && typeof reqData.year[0] === 'string') req.year = reqData.year[0] + + requirements.push(req) }) return requirements @@ -220,7 +240,8 @@ function Formatter() { return { formatProgramme, formatSubjects, - formatSubjectData + formatSubjectData, + formatCourseData } }