Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Added data support for courses in 'gymsar' level.
Browse files Browse the repository at this point in the history
  • Loading branch information
themaxsandelin committed Oct 15, 2018
1 parent c7b5a2d commit 80742c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
8 changes: 0 additions & 8 deletions src/controllers/gym/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ router.get('/:code', (req, res) => {
parseString(fs.readFileSync(file), (err, result) => {
result.subject.courses.forEach(courseData => {
if (courseData.code[0].toLowerCase() === code) {
// const knowledgeRequirements = []
// courseData.knowledgeRequirements.forEach(knowReq => {
// knowledgeRequirements.push({
// grade: knowReq.gradeStep[0],
// requirement: knowReq.text[0]
// })
// })

res.json(formatter.formatCourseData(courseData))
}
})
Expand Down
70 changes: 70 additions & 0 deletions src/controllers/gymsar/courses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Route manager for: /gymsar/courses
// 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/gym/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) => {
const courses = []

result.SubjectsAndCourses.subject.forEach(subject => {
if (subject.courses) {
subject.courses.forEach(course => {
courses.push({
name: course.name[0],
code: course.code[0]
})
})
}
})

res.json(courses)
})
})

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.courses) {
subject.courses.forEach(course => {
if (course.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) => {
result.subject.courses.forEach(courseData => {
if (courseData.code[0].toLowerCase() === code) {
res.json(formatter.formatCourseData(courseData))
}
})
})
}
})
}
})

if (!validCode) res.status(404).send('Not found.')
})
})

module.exports = router
3 changes: 2 additions & 1 deletion src/controllers/gymsar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const router = require('express').Router()
// Sub routes
router.use('/programmes', require('./programmes'))
router.use('/subjects', require('./subjects'))
router.use('/courses', require('./courses'))

router.get('/', (req, res) => {
res.json({
level: 'Gymnasiesärskolan och särvux gymnasial',
routes: ['/programmes', '/programmes/[code]']
routes: ['/programmes', '/programmes/[code]', '/subjects', '/subjects/[code]', '/courses', '/courses/[code]']
})
})

Expand Down

0 comments on commit 80742c9

Please sign in to comment.