-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
340 lines (305 loc) · 9.09 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//CEAB Helper app to assist with making the documents required to be an accredited for engineering
//Group 3
//3/4/20
const mongoose = require('mongoose');
const express = require('express');
var cors = require('cors');
const bodyParser = require('body-parser');
const Course = require('./Models/Course');
const Improvement = require('./Models/Improvement');
const Instructor = require('./Models/Instructor');
const json2csv = require('json2csv');
const fs = require('fs');
const multer = require('multer');
const API_PORT = process.env.PORT || 3001;
const app = express();
app.use(cors());
app.use(bodyParser());
const router = express.Router();
const path = require("path");
//connects mongo db
mongoose
.connect('mongodb://ceab-admin:[email protected]:63757/heroku_gj4ph953', {
useNewUrlParser: true
})
.catch(e => {
console.error('Connection error', e.message)
})
let db = mongoose.connection;
module.exports = db
db.once('open', () => console.log('connected to the database'));
// checks if connection with the database is successful
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
//for uploading csv
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null,'.')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname)
}
}) // create the multer instance that will be used to upload/save the file
var upload = multer({
storage: storage
}).single('file')
//get method that calls db to get syllabus of the given course code
router.get('/loadSyllabus', (req, res) => {
code = req.query.courseCode;
Course.findOne({
courseCode: code
}, (err, data) => {
if (data != null) {
return res.json({
success: true,
data: data
});
}
});
});
//get method that calls db to get improvements of the given course id
router.get('/loadImprovement', (req, res) => {
code = req.query.courseID;
console.log(code);
Improvement.findOne({
courseID: code
}, (err, data) => {
if (data != null) {
return res.json({
success: true,
data: data
});
}
});
});
//get method that calls db to get instructor with the given name
router.get('/loadInstructor', (req, res) => {
first = req.query.instructorFirstName;
family = req.query.instructorFamilyName;
Instructor.findOne({
firstName: first,
familyName: family
}, (err, data) => {
if (data != null) {
return res.json({
success: true,
data: data
});
}
});
});
//post method that delets any data with the given name, and submits a new instructor entry
router.post('/submitInstructor', (req, res) => {
let instructor = req.body.info;
let submit = new Instructor();
for (const value of Object.keys(instructor)) {
console.log(value);
submit[value] = instructor[value];
}
Instructor.deleteMany({
firstName: first,
familyName: family
}, (err, data) => {
console.log(data);
});
submit.save((err) => {
if (err) return res.json({
success: false,
error: err
});
return res.json({
success: true
});
});
});
//post method that delets any data with the given course code, and submits a new syllabus entry
router.post('/submitSyllabus', (req, res) => {
let syllabus = req.body.courses;
let submit = new Course();
for (const value of Object.keys(syllabus)) {
submit[value] = syllabus[value];
}
console.log(submit)
Course.deleteMany({
courseCode: submit.courseCode
}, (err, data) => {
console.log(data);
});
submit.save((err) => {
if (err) return res.json({
success: false,
error: err
});
return res.json({
success: true
});
});
});
//post method that delets any data with the given course id, and submits a new improvement entry
router.post('/submitImprovement', (req, res) => {
let improve = req.body.info;
let submit = new Improvement();
for (const value of Object.keys(improve)) {
submit[value] = improve[value];
}
Improvement.deleteMany({
courseID: submit.courseID
}, (err, data) => {
console.log(data);
});
submit.save((err) => {
if (err) return res.json({
success: false,
error: err
});
return res.json({
success: true
});
});
});
//get method that calls the db for courses and constructs a csv file
router.get('/downloadCourses', (req, res) => {
const filePath = path.join(__dirname, "csv-" + "today" + ".csv");
const fields = [
'courseName', 'courseYear', 'courseCode', 'instructorName', 'lecHours', 'tutHours', 'labHours', 'courseDescription',
'knowledgeGA', 'knowledgeInfo', 'problemGA', 'problemInfo', 'investigationGA', 'investigationInfo', 'designGA', 'designInfo',
'engineeringToolsGA', 'engineeringToolsInfo', 'individualGA', 'individualInfo', 'communicationGA', 'communicationInfo',
'professionalismGA', 'professionalismInfo', 'environmentGA', 'environmentInfo', 'ethicsGA', 'ethicsInfo', 'economicsGA',
'economicsInfo', 'learningGA', 'learningInfo', 'mathPerct', 'basicSciPerct', 'studiesPerct', 'engSciPerct', 'engDesignPerct'
];
let csv;
console.log("getting courses")
Course.find({}, (err, data) => {
if (err) return res.json({
success: false,
error: err
});
try {
csv = json2csv.parse(data, {
fields
});
} catch (error) {
console.error(error);
}
fs.writeFile(filePath, csv, function (err) {
if (err) {
return res.json(err).status(500);
} else {
setTimeout(function () {
fs.unlink(filePath, function (err) { // delete this file after 30 seconds
if (err) {
console.error(err);
}
console.log('File has been Deleted');
});
}, 30000);
res.download(filePath);
}
})
})
});
//get method that calls the db for the course info and constructs the csv for table 3.1.2
router.get('/downloadTable', (req, res) => {
const filePath = path.join(__dirname, "csv-" + "table" + ".csv");
const fields = [
'level','code','indicator'
];
let csv;
console.log("getting table 3.1.2")
Course.find({}, (err, data) => {
if (err) return res.json({
success: false,
error: err
});
let test = {
knowledge:[],
problem : [],
investigation : [],
design : [],
tools : [],
individual : [],
communication : [],
professionalism : [],
environment : [],
ethics : [],
economics : []
};
data.forEach((course)=>{
test.knowledge.push({indicator: course.knowledgeInfo, level : course.knowledgeGA, code: course.courseCode});
test.problem.push({indicator: course.problemInfo, level : course.problemGA, code: course.courseCode});
test.investigation.push({indicator: course.investigationInfo, level : course.investigationGA, code: course.courseCode});
test.design.push({indicator: course.designInfo, level : course.designGA, code: course.courseCode});
test.tools.push({indicator: course.engineeringToolsInfo, level : course.engineeringToolsGA, code: course.courseCode});
test.individual.push({indicator: course.individualInfo, level : course.individualGA, code: course.courseCode});
test.communication.push({indicator: course.communicationInfo, level : course.communicationGA, code: course.courseCode});
test.professionalism.push({indicator: course.professionalismInfo, level : course.professionalismGA, code: course.courseCode});
test.environment.push({indicator: course.environmentInfo, level : course.environmentGA, code: course.courseCode});
test.ethics.push({indicator: course.ethicsInfo, level : course.ethicsGA, code: course.courseCode});
test.economics.push({indicator: course.economicsInfo, level : course.economicsGA, code: course.courseCode});
})
console.log(test)
try {
csv = json2csv.parse(test);
} catch (error) {
console.error(error);
}
fs.writeFile(filePath, csv, function (err) {
if (err) {
return res.json(err).status(500);
} else {
setTimeout(function () {
fs.unlink(filePath, function (err) { // delete this file after 30 seconds
if (err) {
console.error(err);
}
console.log('File has been Deleted');
});
}, 30000);
res.download(filePath);
}
})
})
});
//post method that deconstructs the given csv and updates courses
router.post('/upload', function (req, res) {
console.log("uploading...")
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
return res.status(500).json(err)
} else if (err) {
return res.status(500).json(err)
}
const csvFilePath=req.file.path;
const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
jsonObj.forEach((elem)=>{
let submit = new Course();
for (const value of Object.keys(elem)) {
submit[value] = elem[value];
}
Course.deleteMany({
courseCode: submit.courseCode
}, (err, data) => {
console.log(data);
});
submit.save((err) => {
if (err) return res.json({
success: false,
error: err
});
});
})
return res.status(200).send(req.file)
})
})
});
// append /api for our http requests
app.use('/api', router);
//have the server connect the client
app.use(express.static(path.join(__dirname, "./frontend/build")));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './frontend/build', 'index.html'));
});
// launch our backend into a port
app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));