This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries.js
223 lines (196 loc) · 6.11 KB
/
queries.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
/* Queries corresponding to REST API
How to test?
Debug server.js and let it run until it waits
Run the api.js with some test call func, it will trigger.
note: postgresql does not accept dynamic SQL queries, they must be dynamically pre-made using pg-format
* */
require('dotenv').config();
const Pool = require('pg').Pool;
var format = require('pg-format');
// https://www.npmjs.com/package/pg-format
// should put these into an env
// make sure to squash/commit to remove this credential history
const pool = new Pool({
user: process.env.USER_NAME,
host: process.env.HOST_ADD,
database: process.env.DATABASE_NAME,
password: process.env.SECRET_PASS,
port: process.env.PORT_NUM,
});
// GET user by ID ; This needs tweaking
/**
* function getUsers
* @param request
* @param response - object type. it will have property rows (response.rows) which is an array of the sql results
*/
const getUsers = (request, response) => {
pool.query('SELECT * FROM user_table ORDER BY user_id ASC', (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
};
// currently works
function getBookTable(request,response) {
pool.query('SELECT * FROM book_table ORDER BY book_id ASC', (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
// currently works
function getChatTable(request,response) {
pool.query('SELECT * FROM chat_table ORDER BY chat_id ASC', (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
// currently works
function getUserTable(request,response) {
pool.query('SELECT * FROM user_table ORDER BY user_id ASC', (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
// working
function addRecordBook(request,response) {
console.log('into final add record book method');
//console.log(request);
const rec = request.body;
pool.query('INSERT INTO book_table VALUES ($1, $2, $3, $4, $5, $6, $7, $8)',
[rec.bookid,rec.title,rec.author, rec.isbn, rec.genre, rec.owner_id, rec.borrowed_by, rec.due_date],
(error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(201).send('Row added')
})
}
function addRecordUser(request, response) {
console.log('into final add record user method');
//console.log(request);
const rec = request.body;
//console.log(rec);
pool.query('INSERT INTO user_table VALUES ($1, $2, $3)',
[rec.user_id, rec.user_lon, rec.user_lat],
(error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(201).send('Row added')
})
}
function addRecordChat(request, response) {
//console.log(request);
const rec = request.body;
//console.log(rec);
pool.query('INSERT INTO chat_table VALUES ($1, $2, $3, $4)',
[rec.chatid, rec.firstpname, rec.secondpname, rec.hist],
(error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(201).send('Row added')
})
}
function updateRecord(request, response) {
console.log('into final update record method');
//console.log(request);
const rec = request.body;
//console.log(rec);
let this_sql = format('UPDATE %I SET %I = %L WHERE %I = %L', rec.tablename, rec.cell_d, rec.cell_v, rec.where_d, rec.where_v );
pool.query(
this_sql,
(error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).send('Record updated')
})
}
// currently works
// SELECT <columns> FROM <table> WHERE <condition>
function getARecord(request,response) {
const rec = request.query;
console.log(rec);
let this_sql = format('SELECT * FROM %I WHERE %I = %L', rec.tablename, rec.column_name, rec.value );
pool.query(this_sql, (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
// currently works
// SELECT <columns> FROM <table> WHERE <condition>
/**
*
*
A 202 (Accepted) status code if the action will likely succeed but has not yet been enacted.
A 204 (No Content) status code if the action has been enacted and no further information is to be supplied.
A 200 (OK) status code if the action has been enacted and the response message includes a representation describing the status.
*
*
* @param request
* @param response
*/
function delARecord(request,response) {
console.log(request);
const rec = request.query;
console.log(rec);
let this_sql = format('DELETE FROM %I WHERE %I = %L', rec.tablename, rec.columnname, rec.value );
console.log(this_sql);
pool.query(this_sql, (error, results) => {
if (error) {
response.status(404);
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
/**
* look up a user on the user_table providing the user_id
* @param request
* @param response
*/
function getUserLookUp(request, response) {
const rec = request.query;
// console.log(rec);
let this_sql = format('SELECT first_name, last_name FROM user_table WHERE user_id = %L', rec.user_id_value );
pool.query(this_sql, (error, results) => {
if (error) {
throw error
}
// console.log(results);
response.status(200).json(results.rows)
})
}
// export all the queries to be usable
module.exports = {
getBookTable,
getChatTable,
getUserTable,
addRecordBook,
addRecordUser,
updateRecord,
getARecord,
delARecord,
getUserLookUp,
addRecordChat
};