-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
237 lines (209 loc) · 6.81 KB
/
Copy pathserver.js
File metadata and controls
237 lines (209 loc) · 6.81 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
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
'use strict'
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json())
const axios = require('axios');
require('dotenv').config();
const pg = require('pg');
const client = new pg.Client(process.env.DBURL);
const PORT = process.env.PORT || 3005
const APIKEY = process.env.APIKEY
app.get(`/`, homeHandler)
app.get('/db-data', handleDbData)
app.get(`/category`, categoryHandler)
app.get(`/exercises`, exercisesHandler)
app.get(`/schedule`, handleScheduleData)
app.get(`/get-categories-db`, handleGetDbCategory)
app.put(`/contact/:id`, updateContactHandler)
app.put(`/update-category/:id`, updateCategoryHandler)
app.put(`/update-schedule/:id`, updateScheduleHandler)
app.post(`/contact`, handleAddUserForm)
app.post(`/add-category`, handleAddCategory)
app.post(`/schedule`, handleAddSchedule)
app.delete(`/delete/:id`, handleDeleteForm)
app.delete(`/deleteCategory/:id`, handleDeleteCategory)
app.delete(`/deleteSchedule/:id`, handleDeleteSchedule)
app.use(errorHandler)
//Functions
async function homeHandler(req, res) {
const options = {
method: 'GET',
url: 'https://exercisedb.p.rapidapi.com/exercises/bodyPartList',
headers: {
'X-RapidAPI-Key': APIKEY,
'X-RapidAPI-Host': 'exercisedb.p.rapidapi.com'
}
};
try {
const response = await axios.request(options);
console.log(response.data);
res.status(202).json({
results: response.data
})
} catch (error) {
console.error(error);
}
}
async function categoryHandler(req, res) {
const options = {
method: 'GET',
url: 'https://exercisedb.p.rapidapi.com/exercises/bodyPartList',
headers: {
'X-RapidAPI-Key': APIKEY,
'X-RapidAPI-Host': 'exercisedb.p.rapidapi.com'
}
};
try {
const response = await axios.request(options);
console.log(response.data);
res.status(202).json({
results: response.data
})
} catch (error) {
console.error(error);
}
}
function handleScheduleData(req, res) {
const sql = `select * from user_schedule`;
client.query(sql).then(data => {
res.json({
count: data.rowCount,
data: data.rows
})
}).catch(err => {
errorHandler(err, req, res);
})
}
async function exercisesHandler(req, res) {
const options = {
method: 'GET',
url: 'https://exercisedb.p.rapidapi.com/exercises',
headers: {
'X-RapidAPI-Key': APIKEY,
'X-RapidAPI-Host': 'exercisedb.p.rapidapi.com'
}
};
try {
const response = await axios.request(options);
console.log(response.data);
res.status(202).json({
results: response.data
})
} catch (error) {
console.error(error);
}
}
async function updateContactHandler(req, res) {
const id = req.params.id;
const newData = req.body;
const sql = `update user_contact_form set username = $1, email = $2, usermessage = $3 where id = $4 returning *`;
const updatedValue = [newData.username, newData.email, newData.usermessage, id];
client.query(sql, updatedValue).then(data =>
res.status(202).json(data.rows)
)
}
async function updateCategoryHandler(req, res) {
const id = req.params.id;
const newData = req.body;
const sql = `update exercise_category set category = $1, category_url = $2, description = $3 where id = $4 returning *`;
const updatedValue = [newData.category, newData.category_url, newData.description, id];
client.query(sql, updatedValue).then(data =>
res.status(202).json(data.rows)
)
}
async function updateScheduleHandler(req, res) {
const id = req.params.id;
const newData = req.body;
const sql = `update user_schedule set week_day = $1 where id = $2 returning *`;
const updatedValue = [newData.week_day, id];
client.query(sql, updatedValue).then(data =>
res.status(202).json(data.rows)
)
}
async function handleAddUserForm(req, res) {
const userInput = req.body;
const sql = `insert into user_contact_form(username, email, usermessage) values($1, $2, $3) returning *`;
const handleValueFromUser = [userInput.username, userInput.email, userInput.usermessage];
client.query(sql, handleValueFromUser).then(data => {
res.status(201).json(data.rows)
}).catch(err => errorHandler(err, req, res))
}
async function handleAddCategory(req, res) {
const userInput = req.body;
const sql = `insert into exercise_category(category, category_url,description) values($1, $2,$3) returning *`;
const handleValueFromUser = [userInput.category, userInput.category_url, userInput.description];
client.query(sql, handleValueFromUser).then(data => {
res.status(201).json(data.rows)
}).catch(err => errorHandler(err, req, res))
}
async function handleAddSchedule(req, res) {
const userInput = req.body;
const sql = `insert into user_schedule(bodypart, equipment, gifUrl, exercise_name, exercise_target, week_day) values($1, $2, $3, $4, $5, $6) returning *`;
const handleValueFromUser = [userInput.bodypart, userInput.equipment, userInput.gifUrl, userInput.exercise_name, userInput.exercise_target, userInput.week_day];
client.query(sql, handleValueFromUser).then(data => {
res.status(201).json(data.rows)
}).catch(err => errorHandler(err, req, res))
}
function handleDbData(req, res) {
const sql = `select * from user_contact_form`;
client.query(sql).then(data => {
res.json({
count: data.rowCount,
data: data.rows
})
}).catch(err => {
errorHandler(err, req, res);
})
}
function handleDeleteForm(req, res) {
const id = req.params.id;
const sql = `delete from user_contact_form where id = ${id}`;
client.query(sql).then(() => {
return res.status(204).json({
code: 204,
message: `Row deleted successfuly with id: ${id}`
})
}).catch(err => errorHandler(err, req, res))
}
function handleGetDbCategory(req, res) {
const sql = `select * from exercise_category`;
client.query(sql).then(data => {
res.json({
count: data.rowCount,
data: data.rows
})
}).catch(err => {
errorHandler(err, req, res);
})
}
function handleDeleteCategory(req, res) {
const id = req.params.id;
const sql = `delete from exercise_category where id = ${id}`;
client.query(sql).then(() => {
return res.status(204).json({
code: 204,
message: `Row deleted successfuly with id: ${id}`
})
}).catch(err => errorHandler(err, req, res))
}
function handleDeleteSchedule(req, res) {
const id = req.params.id;
const sql = `delete from user_schedule where id = ${id}`;
client.query(sql).then(() => {
return res.status(204).json({
code: 204,
message: `Row deleted successfuly with id: ${id}`
})
}).catch(err => errorHandler(err, req, res))
}
function errorHandler(error, req, res, next) {
res.status(500).json({ code: 500, message: error.message || error })
}
client.connect().
then(con => {
console.log(con)
app.listen(PORT, () => console.log(`Up and running on port ${PORT}`));
}
)