From 2c3858d75bec1af6216ec99afe4b806c9a4f392c Mon Sep 17 00:00:00 2001 From: anav238 Date: Mon, 11 May 2020 17:10:36 +0300 Subject: [PATCH 1/2] moved getroomschedule to db --- controllers/scheduleController.js | 87 +++++++++++++------------------ 1 file changed, 36 insertions(+), 51 deletions(-) diff --git a/controllers/scheduleController.js b/controllers/scheduleController.js index ddbd839..1862776 100644 --- a/controllers/scheduleController.js +++ b/controllers/scheduleController.js @@ -89,80 +89,65 @@ exports.getProfessorSchedule = async (req, res) => { exports.getRoomSchedule = async (req, res) => { try { - const schedule = await getSchedule('./data/schedule.json') - const schedule2 = await getSchedule('./data/schedule2.json') - + const schedule1 = await req.db.Schedule.findOne( {semester: 1}); + const schedule2 = await req.db.Schedule.findOne( {semester: 2}); + years = schedule1.years; const { r } = req.query if (!r) { return res.status(HttpStatus.OK).json({ success: true, - schedule + schedule1, + schedule2 }) } - const rooms = r.split(',') + const rooms = r ? r.split(','):undefined const sem1 = {} const sem2 = {} - if (schedule) { - for (const year in schedule) { - for (const day in schedule[year]) { - if (!Object.prototype.hasOwnProperty.call(sem1, year)) { - sem1[year] = {} - } - - sem1[year][day] = Array.prototype.filter.call( - schedule[year][day], - entry => { - for (const room in rooms) { - if (entry.Sala === rooms[room]) { - return true - } - } - return false - } - ) + for (const year of schedule1.years) { + + sem1[year.year] = {} - if (Array.isArray(sem1[year][day]) && sem1[year][day].length === 0) { - delete sem1[year][day] - } + for (const day of year.days) { + sem1[year.year][day.name] = [] + + for (const course of day.courses) { + if (rooms.indexOf(course.room) > -1) + sem1[year.year][day.name].push(course); } - if (Object.keys(sem1[year]).length === 0) { - delete sem1[year] + if (sem1[year.year][day.name].length === 0) { + delete sem1[year.year][day.name] } } - } - if (schedule2) { - for (const year in schedule2) { - for (const day in schedule2[year]) { - if (!Object.prototype.hasOwnProperty.call(sem2, year)) { - sem2[year] = {} - } + if (Object.keys(sem1[year.year]).length === 0) { + delete sem1[year.year] + } + } - sem2[year][day] = Array.prototype.filter.call( - schedule[year][day], - entry => { - for (const room in rooms) { - if (entry.Sala === rooms[room]) { - return true - } - } - return false - } - ) + for (const year of schedule2.years) { + + sem2[year.year] = {} - if (Array.isArray(sem2[year][day]) && sem2[year][day].length === 0) { - delete sem2[year][day] - } + for (const day of year.days) { + sem2[year.year][day.name] = [] + + for (const course of day.courses) { + if (rooms.indexOf(course.room) > -1) + sem2[year.year][day.name].push(course); } - if (Object.keys(sem2[year]).length === 0) { - delete sem2[year] + if (sem2[year.year][day.name].length === 0) { + delete sem2[year.year][day.name] } } + + if (Object.keys(sem2[year.year]).length === 0) { + delete sem2[year.year] + } } return res.status(HttpStatus.OK).json({ From 5c66aaf3d81f5991d55b65a7b0fc05e0ffa6cb9e Mon Sep 17 00:00:00 2001 From: anav238 Date: Mon, 11 May 2020 17:30:02 +0300 Subject: [PATCH 2/2] moved getRoomSchedule to db --- controllers/scheduleController.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/controllers/scheduleController.js b/controllers/scheduleController.js index 1862776..ee89818 100644 --- a/controllers/scheduleController.js +++ b/controllers/scheduleController.js @@ -89,9 +89,8 @@ exports.getProfessorSchedule = async (req, res) => { exports.getRoomSchedule = async (req, res) => { try { - const schedule1 = await req.db.Schedule.findOne( {semester: 1}); - const schedule2 = await req.db.Schedule.findOne( {semester: 2}); - years = schedule1.years; + const schedule1 = await req.db.Schedule.findOne({ semester: 1 }) + const schedule2 = await req.db.Schedule.findOne({ semester: 2 }) const { r } = req.query if (!r) { @@ -102,20 +101,18 @@ exports.getRoomSchedule = async (req, res) => { }) } - const rooms = r ? r.split(','):undefined + const rooms = r ? r.split(',') : undefined const sem1 = {} const sem2 = {} for (const year of schedule1.years) { - sem1[year.year] = {} for (const day of year.days) { sem1[year.year][day.name] = [] - + for (const course of day.courses) { - if (rooms.indexOf(course.room) > -1) - sem1[year.year][day.name].push(course); + if (rooms.indexOf(course.room) > -1) { sem1[year.year][day.name].push(course) } } if (sem1[year.year][day.name].length === 0) { @@ -129,15 +126,13 @@ exports.getRoomSchedule = async (req, res) => { } for (const year of schedule2.years) { - sem2[year.year] = {} for (const day of year.days) { sem2[year.year][day.name] = [] - + for (const course of day.courses) { - if (rooms.indexOf(course.room) > -1) - sem2[year.year][day.name].push(course); + if (rooms.indexOf(course.room) > -1) { sem2[year.year][day.name].push(course) } } if (sem2[year.year][day.name].length === 0) {