-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_prep.js
105 lines (93 loc) · 2.58 KB
/
data_prep.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
const Sequelize = require('sequelize');
var sequelize = new Sequelize('lslravwy', 'lslravwy', '8NpyADIOzGo_-K3AAcCL6Fu1ZJ2YiWjd', {
host: 'peanut.db.elephantsql.com',
dialect: 'postgres',
port: 5432,
dialectOptions: {
ssl: true
}, query:{raw: true} // update here, you. Need this
});
sequelize.authenticate().then(()=> console.log('Connection success.'))
.catch((err)=>console.log("Unable to connect to DB.", err));
var Student = sequelize.define('Student', {
StudId: {
type:Sequelize.INTEGER,
primaryKey:true,
autoIncrement:true
},
name:Sequelize.STRING,
program:Sequelize.STRING,
gpa:Sequelize.FLOAT
});
exports.prep = ()=>{
return new Promise((resolve,reject) => {
sequelize.sync()
.then(resolve())
.catch(reject('unable to sync the database'));
})
};
exports.bsd = ()=>{
return new Promise((resolve, reject)=>{
let results = students.filter(student => student.program == "BSD");
(results.length == 0)? reject("No BSD students."):resolve(results);
});
}
exports.cpa = ()=>{
return new Promise((resolve, reject)=>{
Student.findAll({
where: {
program: "CPA"
}
})
.then(data => resolve(data))
.catch('no results returned')
});
}
exports.highGPA = ()=>{
return new Promise((resolve, reject)=>{
Student.findAll({
where: {
gpa: 4
}
})
.then(data => resolve(data))
.catch(err => reject(err))
})
};
exports.lowGPA = ()=>{
return new Promise((resolve, reject)=>{
let low = 4.0;
let lowStudent;
for (let i=0; i<students.length; i++)
{
if (students[i].gpa < low)
{
low = students[i].gpa;
lowStudent = students[i];
}
}
resolve(lowStudent);
});
};
exports.allStudents =()=>{
return new Promise((resolve, reject)=>{
Student.findAll()
.then(data => { resolve(data); })
.catch(err => { reject(err); })
})
};
exports.addStudent= (student)=>{
return new Promise((resolve, reject)=>{
Student.create(student).then(resolve(Student.findAll()))
.catch(reject('unable to add the student'))
})
};
exports.getStudent = (studId)=>{
return new Promise((resolve, reject)=>{
students.forEach(function(student){
if (student.studId == studId)
resolve(student);
});
reject("No result found!");
})
}