-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (82 loc) · 3.2 KB
/
index.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
const mysql = require('mysql');
const express = require('express');
const bodyparser = require('body-parser');
/*** Configuring express server ***/
const app = express();
app.use(bodyparser.json());
/*Create a connection with MySQL database using the createConnection function
and provide required details => host, user, password, db */
const mysqlConnection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '43B4r.',
multipleStatements: true
});
//Estabilishing connection with the database using provided credentials.
mysqlConnection.connect((err)=> {
if(!err)
console.log('Connection Estabilished Successfully.');
else
console.log('Connection Failed.' + JSON.stringify(err, undefined, 2));
});
//Specifying the port we will be sending our requests to the server.
//PORT ENVIRONMENT VARIABLE
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on port ${port}...`));
/*** Creating GET Router to get learners db ***/
app.get('/learners', (req, res) => {
mysqlConnection.query(
'SELECT * FROM learners.learner_details',
(err, rows, fields) => {
if( !err )
res.send(rows);
else
console.log(err);
})
});
/*** Creating Router to GET learner details from MySQL db ***/
app.get('/learners/:id', (req, res) => {
mysqlConnection.query(
'SELECT * FROM learners.learner_details WHERE learner_id = ?',
[req.params.id],
(err, rows, fields) => {
if( !err )
res.send(rows);
else
console.log(err);
})
});
/*** Creating Router to POST ***/
app.post('/learners', (req, res) => {
let learner = req.body;
var sql = "SET @learner_id=?;SET @learner_name=?;SET @learner_email=?; SET @course_id=?; CALL learners.learner_create_update(@learner_id,@learner_name,@learner_email, @course_id);";
mysqlConnection.query(sql, [learner.learner_id, learner.learner_name, learner.learner_email, learner.course_id], (err, rows, fields) => {
if( !err )
rows.forEach(element => {
if( element.constructor == Array )
res.send('New Learner ID : ' + element[0].learner_id);
});
else
console.log(err);
})
});
/*** Router to UPDATE ***/
app.put('/learners', (req, res) => {
let learner = req.body;
var sql = "SET @learner_id = ?;SET @learner_name = ?;SET @learner_email = ?;SET @course_id = ?; CALL learners.learner_create_update(@learner_id,@learner_name,@learner_email,@course_id);";
mysqlConnection.query(sql, [learner.learner_id, learner.learner_name, learner.learner_email, learner.course_id], (err, rows, fields) => {
if ( !err )
res.send('Learner Details Updated Successfully!');
else
console.log(err);
})
});
/*** Router to DELETE user details ***/
app.delete('/learners/:id', (req,res) => {
mysqlConnection.query('DELETE FROM learners.learner_details WHERE learner_id = ?', [req.params.id], (err, rows, fields) => {
if( !err )
res.send('Learner Record Deleted successfully!');
else
console.log(err);
})
});