-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (133 loc) · 3.71 KB
/
app.js
File metadata and controls
153 lines (133 loc) · 3.71 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
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
const express = require("express");
const app = express();
const axios = require("axios");
const sqlite3 = require("sqlite3");
let count = 1;
const HOST = "localhost";
const PORT = process.env.PORT || 8888;
const KEY = "rbz67hBwSB6KkkGKhzVOT6QKNhG965Ml";
const currentTime = new Date();
const db = new sqlite3.Database("./db.sqlite3", (err) => {
if (err) {
throw new Error("Error opening database " + err.message);
}
});
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
let term = null;
app.get("/", (req, res) => {
let limit = 50;
let offset = 0;
console.log("DDDD", req.query);
if (req.query.search != null) {
term = req.query.search;
}
if (req.query.limit != 50) {
limit = req.query.limit;
}
if (req.query.offset != 0) {
offset = parseInt(req.query.offset);
}
const gURL =
"https://api.giphy.com/v1/gifs/search?api_key=" +
KEY +
"&q=" +
term +
"&limit=" +
limit +
"&offset=" +
offset +
"&lang=en";
console.log("GIPHY URL: ", gURL);
const getData = async (gURL) => {
try {
const response = await axios.get(gURL).then((res) => res.data);
const pagination = {
totalCount: response.pagination.total_count,
count: response.pagination.count,
page: offset / limit,
};
const gifUrls = response.data.map((gifObject) => gifObject.url);
const formattedResponse = {
data: gifUrls,
pagination,
};
res.send(formattedResponse);
insertSearchTerm(term, gifUrls, response.pagination.total_count);
} catch (error) {
throw new Error(err);
}
};
getData(gURL);
});
app.get("/data", (req, res) => {
db.all(`SELECT * FROM SearchTerms`, [], (err, rows) => {
if (err) {
throw new Error(err);
}
res.status(200).json(rows);
});
});
function insertSearchTerm(term, gifUrls, totalCount) {
/* Check here if the searchTerm is already in the database, and
if it is, do not insert in DB, but make the api call to get the results
and update search term data:the count(how many times it was searched),totalCount and updatedAt.
*/
db.get(
"SELECT term,count FROM SearchTerms WHERE term =?",
term,
(err, row) => {
if (err) {
throw new Error(err);
}
if (!row) {
//here the database is empty, no need for checking if search term is there
const insertQuerySearches =
"INSERT INTO SearchTerms (term,count,createdAt,updatedAt,totalCount) VALUES(?,?,?,?,?)";
db.run(insertQuerySearches, [
term,
count,
currentTime,
currentTime,
totalCount,
]);
} else {
//will check if search term already exists
if (row.term === term) {
//item already exists
const updateQuery =
"UPDATE SearchTerms SET count=?, updatedAt=?, totalCount=? WHERE term =? ";
let updatedTime = new Date();
db.run(updateQuery, [row.count + 1, updatedTime, totalCount, term]);
} else {
//unique item
const insertQuerySearches =
"INSERT INTO SearchTerms (term,count,createdAt,updatedAt,totalCount) VALUES(?,?,?,?,?)";
db.run(insertQuerySearches, [
term,
count,
currentTime,
currentTime,
totalCount,
]);
}
}
}
);
}
function closeDB() {
db.close((err) => {
if (err) console.log(err.message);
else console.log("Closed the database connection.");
});
}
app.listen(PORT, () => console.log(`API Running at ${HOST}:${PORT}!`));
process.on("SIGINT", () => {
console.log("\nClosing the database connection");
closeDB();
process.exit();
});