Skip to content

Commit f6b16b5

Browse files
add search functionality
1 parent cf5820a commit f6b16b5

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

DB-query-planning.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
# Joe and Dom thinking of queries:
1+
# Joe and Dom (and Eve now!) thinking of queries:
2+
Misc notes
23

34
## People searching:
45

56
* User searches for item
67
* `SELECT * FROM items WHERE lower(name) LIKE '%{user-input.toLowerCase()}%' OR lower(description) LIKE '%{user-input.toLowerCase()}%';
78
* The above is passed in using JS template literal. Do we need to escape the percentages?
89

10+
* Updated query example:
11+
`SELECT users.name, items.name, items.description FROM items
12+
INNER JOIN users ON items.lender_id = users.id
13+
WHERE lower(items.name) LIKE '%blade%' OR lower(items.description) LIKE '%blade%';
14+
`
15+
16+
`SELECT users.name, users.fav_colour, items.name, items.description
17+
FROM items
18+
INNER JOIN users
19+
ON items.lender_id = users.id
20+
WHERE lower(items.name) LIKE '%$1%' OR lower(items.description) LIKE '%$1%';`
21+
922
* User searches for item, with box checked to only show items that are available (ie not out on loan)
1023
* `SELECT * FROM loans WHERE item_id=${item_id}`
1124

src/handlers.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,19 @@ const handlers = {
5050
});
5151
},
5252

53-
search(req, res) {
54-
// add some innards
53+
search(req, res, endpoint) {
54+
const qry = decodeURIComponent(endpoint.split("?q=")[1]).replace(/[^A-Za-z0-9 ]/,"").toLowerCase();
55+
getData(qry, (err, result) => {
56+
if(err) {
57+
res.writeHead(500, { "Content-Type": "text/html" });
58+
res.end("<h1>Server Error</h1>");
59+
console.log("search error");
60+
} else {
61+
res.writeHead(200, "Content-type: application/json");
62+
res.end(JSON.stringify(result));
63+
}
64+
})
65+
5566
},
5667
requestItem(req, res) {
5768
if (req.method === "POST") {

src/queries/getData.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const databaseConnection = require('../database/db_connection.js');
22

33
// qry is the db query we want to make
4-
const getData = (cb) => {
5-
databaseConnection.query('SELECT * FROM users', (err, res) => {
4+
const getData = (qry, cb) => {
5+
databaseConnection.query(`SELECT users.name, users.fav_colour, items.name, items.description
6+
FROM items
7+
INNER JOIN users
8+
ON items.lender_id = users.id
9+
WHERE lower(items.name) LIKE '%${qry}%' OR lower(items.description) LIKE '%${qry}%'`, (err, res) => {
610
if (err) {
7-
cb(err);
8-
} else {
9-
cb(null, res.rows)
10-
}
11+
return cb(err);
12+
}
13+
return cb(null, res.rows);
14+
1115
});
1216
};
1317

src/router.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const router = (req, res) => {
55

66
if (endpoint === "") {
77
handlers.home(req, res);
8-
} else if (endpoint === "search") {
9-
handlers.search(req, res); // some arguments maybe
8+
} else if (endpoint.includes("search?q=")) {
9+
handlers.search(req, res, endpoint); // some arguments maybe
1010
} else if (endpoint === "request-item") {
1111
handlers.requestItem(req, res);
1212
} else if (endpoint === "add-item") {

0 commit comments

Comments
 (0)