-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·89 lines (74 loc) · 3.03 KB
/
Copy pathserver.js
File metadata and controls
executable file
·89 lines (74 loc) · 3.03 KB
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var oracledb = require('oracledb');
oracledb.autoCommit = true;
var connectionProperties = {
user: process.env.DBAAS_USER_NAME || "loyalty",
password: process.env.DBAAS_USER_PASSWORD || "Welcome_1",
connectString: process.env.DBAAS_DEFAULT_CONNECT_DESCRIPTOR || "localhost:1521/orcl.oracle.com"
};
function doRelease(connection) {
connection.release(function (err) {
if (err) {
console.error(err.message);
}
});
}
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: '*/*' }));
var PORT = process.env.PORT || 8080;
var router = express.Router();
router.use(function (request, response, next) {
console.log("REQUEST:" + request.method + " " + request.url);
console.log("BODY:" + JSON.stringify(request.body));
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
response.setHeader('Access-Control-Allow-Credentials', true);
next();
});
// To-do: Let's get a quick preview of all offers
// router.route('/ptmgt/v1/offers').get(function (request, response) {
// console.log("GET ALL OFFERS");
//
// })
/**
* GET /ptmgt/v1/offers/:id
* Returns the detail of an offer
*/
router.route('/ptmgt/v1/offers/:id').get(function (request, response) {
console.log("GET OFFER BY ID:" + request.params.id);
oracledb.getConnection(connectionProperties, function (err, connection) {
if (err) {
console.error(err.message);
response.status(500).send("Error connecting to DB");
return;
}
var id = request.params.id;
connection.execute("SELECT offers.id, offername, points, message, productname, productprice, productimage, productdesc, productid FROM offers, product WHERE offers.id = :id AND product.id = offers.productid",
[id],
{ outFormat: oracledb.OBJECT },
function (err, result) {
if (err) {
console.error(err.message);
response.status(500).send("Error getting data from DB");
doRelease(connection);
return;
}
console.log("RESULTSET:" + JSON.stringify(result));
if (result.rows.length === 1) {
var offer = { id: result.rows[0].ID, name: result.rows[0].OFFERNAME, points: result.rows[0].POINTS, message: result.rows[0].MESSAGE, productid: result.rows[0].PRODUCTID ,productname: result.rows[0].PRODUCTNAME, productprice: result.rows[0].PRODUCTPRICE, productimage: result.rows[0].PRODUCTIMAGE, productdesc: result.rows[0].PRODUCTDESC };
response.json(offer);
doRelease(connection);
} else {
response.end();
}
});
});
});
app.use('/', router);
app.listen(PORT);
console.log("Server started in port:" + PORT + ", using connection: " + JSON.stringify(connectionProperties));