Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

get year semester to db #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions controllers/scheduleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,40 @@ exports.getYearSchedule = async (req, res) => {

exports.getYearSemesterSchedule = async (req, res) => {
try {
const { yearNumber, semesterNumber } = req.params
if (semesterNumber === '1' || semesterNumber === '2') {
let pathToSchedule = './data/schedule.json'

if (semesterNumber === '2') pathToSchedule = './data/schedule2.json'

const schedule = await getSchedule(pathToSchedule)

if (!schedule[yearNumber]) {
const yearNumber = parseInt(req.params.yearNumber)
const semesterNumber = parseInt(req.params.semesterNumber)
if (semesterNumber === 1 || semesterNumber === 2) {
const schedule = await req.db.Schedule.findOne({ semester: semesterNumber })
if (!schedule.years[yearNumber - 1]) {
return res.status(HttpStatus.NOT_FOUND).json({
success: false,
message: 'Invalid year number'
})
}

const sem = {}

for (const year of schedule.years) {
if (yearNumber === year.year) {
sem[year.year] = {}
for (const day of year.days) {
sem[year.year][day.name] = []
for (const course of day.courses) {
sem[year.year][day.name].push(course)
}
if (sem[year.year][day.name].length === 0) {
delete sem[year.year][day.name]
}
}
if (Object.keys(sem[year.year]).length === 0) {
delete sem[year.year]
}
}
}
return res.status(HttpStatus.OK).json({
success: true,
schedule: schedule[yearNumber]
schedule: sem
})
}

return res.status(HttpStatus.NOT_FOUND).json({
success: false,
message: 'Invalid semester number'
Expand Down
Loading