-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductHandler.js
238 lines (223 loc) · 8.77 KB
/
productHandler.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
/**
* a module for handling interactions related to the Product table of the
* database.
* @module productHandler
*/
/* eslint-disable no-undef */
var db = require("./databaseHandler")
var fs = require("fs")
var crypto = require("crypto")
/* eslint-enable no-undef */
/**
* a callback that does not return data
* @callback emptyCallback
* @param {Error|null} err
*/
/**
* a callback that returns the ID of the product affected
* @callback updateCallback
* @param {Error|null} err
* @param {string} ID
*/
/**
* a callback that returns product data
* @callback getCallback
* @param {Error|null} err
* @param {Object} JSONToReturn - The return object containing the data
* @param {Object[]} JSONToReturn.content - The list of product objects to return
* @param {string} JSONToReturn.content.productID - The ID of the product
* @param {string} JSONToReturn.content.title - The name of the product
* @param {string} JSONToReturn.content.author - The author of the product
* @param {string} JSONToReturn.content.location - The location of the product
* @param {string} JSONToReturn.content.description - The description of the product
* @param {string} JSONToReturn.content.price - The price of the product
* @param {Object[]} JSONToReturn.content.links - A list of links that the recipient can access
* @param {string} JSONToReturn.content.links.rel - The relation of the link to the product (always self)
* @param {string} JSONToReturn.content.links.href - The image of the product if available
*/
/**
* create a new product in the Product table
* @param {Object} conData - The connection data for the database
* @param {string} conData.host - The name of the host we're connecting to
* @param {string} conData.user - The username of the administrator of the database
* @param {string} conData.password - The password of the administrator of the database
* @param {string} conData.database - The name of the database to connect to
* @param {Object} req - The request object sent from ExpressJS
* @param {string} req.body.title - The title of the new product
* @param {string} req.body.description - The description of the new product
* @param {string} req.body.location - The location of the new product
* @param {string|float} req.body.price - The price of the new product. Must be parseable to float
* @param {string} username - The username of the user adding the product
* @param {updateCallback}
*/
/* eslint-disable-next-line no-undef */
exports.addProduct = function(conData, req, username, callback){
db.connect(conData, function(err, data){
if (err) {
callback(err)
return
}
var ID = crypto.randomBytes(16).toString("hex")
var product = {
productID: ID,
title: req.body["title"],
description: req.body["description"],
img: null,
location: req.body["location"],
price: parseFloat(req.body["price"]),
author: username,
}
data.query("INSERT INTO Products SET ?", product, function(err){
if (err) {
data.end()
callback(err)
return
} else {
data.end()
callback(err, ID)
return
}
})
})
}
/**
* add an image to a product in the Product table
* @param {Object} conData - The connection data for the database
* @param {string} conData.host - The name of the host we're connecting to
* @param {string} conData.user - The username of the administrator of the database
* @param {string} conData.password - The password of the administrator of the database
* @param {string} conData.database - The name of the database to connect to
* @param {Object} fileData - The image data information object sent from ExpressJS
* @param {string} fileData.ID - The ID of the product being updated
* @param {string} fileData.currentUrl - The current internal URL (path) of the image
* @param {updateCallback}
*/
/* eslint-disable-next-line no-undef */
exports.addImage = function(conData, fileData, callback){
var newUrl = `public/products/images/${fileData.ID}.jpg`
var routeUrl = `http://localhost:8080/products/images/${fileData.ID}.jpg`
fs.rename(fileData.currentUrl, newUrl, function(err){
if(err){
callback(err)
return
} else {
db.connect(conData, function(err, data){
if (err) {
callback(err)
return
}
var sql = "UPDATE Products SET img = ? WHERE productID = ?;"
data.query(sql, [routeUrl, fileData.ID], function(err){
if (err) {
data.end()
callback(err)
return
} else {
data.end()
callback(null, fileData.ID)
return
}
})
})
}
})
}
/**
* get products from the Product table
* @param {Object} conData - The connection data for the database
* @param {string} conData.host - The name of the host we're connecting to
* @param {string} conData.user - The username of the administrator of the database
* @param {string} conData.password - The password of the administrator of the database
* @param {string} conData.database - The name of the database to connect to
* @param {Object} req - The request object sent from ExpressJS
* @param {string} [req.query.title] - The title of product to be searched for
* @param {getCallback}
*/
/* eslint-disable-next-line no-undef */
exports.getAllProducts = function(conData, req, callback){
db.connect(conData, function(err, conn){
if (err) {
callback(err)
return
}
var sql = "SELECT * FROM Products"
if(req.query.title){
sql = sql + " WHERE LOCATE(?,title) > 0"
}
conn.query(sql, [req.query.title], function(err, result){
if (err) {
conn.end()
callback(err)
return
}
conn.end()
var JSONToReturn = { "content": [] }
result.forEach(function(item){
var product = {
"productID": item.productID,
"title": item.title,
"location": item.location,
"price": item.price,
"description": item.description,
"author": item.author,
"links": [ {
"rel": "self",
"href": item.img
} ],
}
JSONToReturn.content.push(product)
})
callback(err, JSONToReturn)
return
})
})
}
/**
* deletes a product in the Product table
* @param {Object} conData - The connection data for the database
* @param {string} conData.host - The name of the host we're connecting to
* @param {string} conData.user - The username of the administrator of the database
* @param {string} conData.password - The password of the administrator of the database
* @param {string} conData.database - The name of the database to connect to
* @param {string} id - The ID of the product to be deleted
* @param {Object} decoded - The decoded data from a JWT
* @param {string} decoded.username - The username of the product author
* @param {bool|undefined} decoded.isAdmin - Whether or not the user is an admin. Is admin if present
* @param {updateCallback}
*/
/* eslint-disable-next-line no-undef */
exports.deleteProduct = function(conData, id, decoded, callback){
db.connect(conData, function(err, conn){
if (err) {
callback(err)
return
}
var sql = ""
// admins can delete any product
if(decoded.hasOwnProperty("isAdmin")){
sql = "DELETE FROM Products WHERE productID = ?"
} else {
sql = "DELETE FROM Products WHERE productID = ? AND author = ?"
}
conn.query(sql, [id, decoded.username], function(err, result){
conn.end()
if (err) {
callback(err)
return
} else if (result.affectedRows === 0){
var emptyErr = new Error("Didn't delete anything, is productID and author correct?")
callback(emptyErr)
return
} else {
fs.unlink(`public/products/images/${id}.jpg`, function(){
/*
* we don't care about the error from fs.unlink, it's
* just a cleanup function.
*/
callback(null, id)
return
})
}
})
})
}