-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_app.js
247 lines (208 loc) · 8.07 KB
/
node_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
/*
In order to run, first install Node, NPM and PostgreSQL
Next install the required node packages:
$ npm install pg
$ npm install pg-format
$ npm install express --save
Once dependencies are installed, use the following command to run:
$ node node_app.js <db_username> <db_password> <company_name>
You may want to use the database/seeds.sql file to insert some test data.
*/
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8000');
// Headers you wish to allow in requests
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
// Handles post requests
app.use(bodyParser.json());
// Handles put requests
// app.use(express.methodOverride());
var pg = require('pg')
var format = require('pg-format')
// Database configuration
var PGDATABASE = process.env.CSR_LOOKUP_POSTGRES_DATABASE || 'csr_lookup'
var PGUSER = process.env.CSR_LOOKUP_POSTGRES_USER || 'postgres'
var PGPASSWORD = process.env.CSR_LOOKUP_POSTGRES_PASSWORD || 123456
if (PGDATABASE == null) {
console.log("Error: database must be configured")
} else {
var config = {
user: PGUSER, // name of the user account
password: PGPASSWORD, // password of the user account
database: PGDATABASE, // name of the database
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
}
var pool = new pg.Pool(config)
var myClient
pool.connect(function (err, client, done) {
if (err) console.log(err)
// Define our main route, HTTP "GET /" which will print "hello"
app.get('/', function (req, res) {
res.send('Hello world!');
});
// Company route handler
// * Return a subset of companies
app.get('/companies/users/:userId/search/:companyName', function (req, res) {
var companyName = req.params.companyName;
var userId = req.params.userId;
var query = "SELECT * FROM vw_companies_information WHERE name like '%" + companyName + "%'"
console.log("\n" + query);
console.log("\nuser:" + userId);
myClient.query(query, function (err, result) {
if (err) console.log(err)
var result_rows = result.rows;
if(!result_rows || result_rows.length == 0)
{
res.send(null);
return;
}
var companiesIds = result_rows.map(comp => comp.id);
var ratingQuery = "SELECT * FROM rating_records where fk_company_id in (" + companiesIds.join(',') + ")";
myClient.query(ratingQuery, function (errFromRatingQuery, ratingResult) {
var rating_rows = ratingResult.rows;
for (var company of result_rows) {
var rating_record = rating_rows.find(ra => ra.fk_company_id == company.id && ra.fk_created_by == userId);
if(rating_record)
company.user_rated = true;
}
if (result_rows.length == 0) {
response = `${query} returned no results!`;
} else {
response = JSON.stringify(result.rows);
}
res.send(response);
console.log("=> " + response);
})
});
});
// * Return a single company
app.get('/companies/:id', function (req, res) {
var id = req.params.id;
var query = format('SELECT * FROM vw_companies_information WHERE id = %L', id)
queryDatabase(query, res, true);
});
// * Return a single rating record
app.get('/rating_records/:userId/:companyId', function (req, res) {
var userId = req.params.userId;
var companyId = req.params.companyId;
var query = `SELECT * FROM rating_records WHERE fk_created_by = ${userId} AND fk_company_id = ${companyId}`
queryDatabase(query, res, true);
});
// * Add a company
app.post('/companies', function (req, res) {
var name = req.body.name;
var wikipedia_name = req.body.wikipedia_name;
var industry = req.body.industry;
var query = format('INSERT INTO companies (name, wikipedia_name, industry) VALUES (\'%s\', \'%s\', \'%s\') RETURNING id', name, wikipedia_name, industry)
queryDatabase(query, res, true);
});
// * Add an evidence record for a company
app.post('/companies/:id/evidence_records', function (req, res) {
var companyId = req.params.id;
var query = `
INSERT INTO evidence_records (
fk_company_id,
title,
reference_url,
reference_title,
description,
score
) VALUES (
${companyId},
'${req.body.title}',
'${req.body.reference_url}',
'${req.body.reference_title}',
'${req.body.description}',
${req.body.score}
) RETURNING id
`;
queryDatabase(query, res, true);
});
// * Rate company
app.post('/companies/:id/rate', function (req, res) {
var companyId = req.params.id;
var userId = req.body.userId;
var rated = req.body.rated;
var rating = req.body.rating;
//if user has rated the company before just update his rating
if(rated){
var query = format('UPDATE rating_records SET rating = \'%s\' where fk_created_by = \'%s\' AND fk_company_id = \'%s\'', rating, userId, companyId )
console.log("\n" + query);
myClient.query(query, function (err, result) {
getCompanyInformation(companyId, function(companyInfo){
res.send(companyInfo.rating);
})
});
}
else {
//if user hasn't rated the company before, create a new record with his rating in the rating_records table
var query = format('INSERT INTO rating_records (fk_created_by, fk_company_id, rating) VALUES (\'%s\', \'%s\', \'%s\')', userId, companyId, rating)
console.log("\n" + query);
myClient.query(query, function (err, result) {
getCompanyInformation(companyId, function(companyInfo){
res.send(companyInfo.rating);
})
});
}
});
function getCompanyInformation(id, callback) {
var query = format("SELECT * FROM vw_companies_information WHERE id = " + id);
myClient.query(query, function (err, result) {
if(result && result.rows && result.rows.length > 0)
callback(result.rows[0]);
});
}
// * Return all evidence_records for a given company
app.get('/companies/:id/evidence_records', function (req, res) {
var id = req.params.id;
var query = format('SELECT * FROM evidence_records WHERE fk_company_id = %L', id)
queryDatabase(query, res);
});
function queryDatabase(query, res, firstRowOnly = false) {
console.log("\n" + query);
myClient.query(query, function (err, result) {
if (err) console.log(err)
var result_rows = result.rows
var response;
if (result_rows.length == 0) {
response = null;
} else {
response = JSON.stringify(firstRowOnly ? result.rows[0] : result.rows);
}
res.send(response);
if (response == null) {
console.log(`${query} returned no results!`);
} else {
console.log("=> " + response);
}
})
};
// Start listening
app.listen(3000, function () {
console.log('Listening on 3000...')
})
// Command line inputs
// var companyName = process.argv[4]
// Direct inputs
var companyName = 'ESRI'
// Small test query
myClient = client
var companyQuery = format("SELECT * FROM vw_companies_information WHERE name like '%" + companyName + "%'")
myClient.query(companyQuery, function (err, result) {
if (err) console.log(err)
var result_rows = result.rows
if (result_rows.length == 0) {
console.log(`No companies in our database have the name "${companyName}".`)
} else {
var companyIndustry = result.rows[0]['industry']
console.log(`${companyName} is a company in the ${companyIndustry} industry.`)
}
})
})
}