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 subjects in 'gymsar' level
Browse files Browse the repository at this point in the history
  • Loading branch information
themaxsandelin committed Oct 15, 2018
1 parent c22f27a commit c7b5a2d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 30 deletions.
20 changes: 2 additions & 18 deletions src/controllers/gym/subjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

Expand All @@ -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))
})
}
})
Expand Down
1 change: 1 addition & 0 deletions src/controllers/gymsar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
23 changes: 13 additions & 10 deletions src/controllers/gymsar/programmes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Route manager for /gymsar/programmes
// Data folder: gys

// Dependencies
const router = require('express').Router()
Expand Down Expand Up @@ -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')
})
})

Expand Down
50 changes: 50 additions & 0 deletions src/controllers/gymsar/subjects.js
Original file line number Diff line number Diff line change
@@ -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
17 changes: 16 additions & 1 deletion src/helpers/gym/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -209,7 +223,8 @@ function Formatter() {
formatProfiles,
formatCourses,
formatCourseData,
formatSubjects
formatSubjects,
formatSubjectData
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/helpers/gymsar/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -204,7 +218,9 @@ function Formatter() {
}

return {
formatProgramme
formatProgramme,
formatSubjects,
formatSubjectData
}
}

Expand Down

0 comments on commit c7b5a2d

Please sign in to comment.