-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
300 lines (277 loc) · 8.66 KB
/
app.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const express = require('express')
const mysql = require('mysql')
const cors = require('cors')
const fs = require('fs');
const test_data =require('./test_data.json')
const app = express()
app.use(cors())
app.use(express.json())
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`)
})
// Test API is working
// app.get('/', (req, res) => {
// console.log(`Api States GET ALL request ${new Date()}`)
// const states=[];
// test_data.missions.forEach(mission => {
// states.push(mission)
// });
// console.log(states);
// res.status(200).json(states)
// })
app.get("/", (req,res)=>{
res.status(200).send("OceanHackersAPI")
})
// CALL TO ADD TEST DATA
app.post("/test", (req, res)=>{
const images=test_data.images;
const missions=test_data.missions;
const challenges=test_data.challenges;
const challenges_images=test_data.challenges_images;
const users=test_data.users;
const resources=test_data.resources
var it=0
var object={}
images.forEach(image => {
const sql=`INSERT INTO images SET ?`
console.log(image)
connection.query(sql, image, error=>{
if(error) throw error;
console.log(it+" - upload image complete!")
it++
})
});
const sql=`SELECT * FROM images`
connection.query(sql,(error, results)=>{
if(error) throw error;
if(results.length>0){
res.status(200).json(results)
}else{
res.status(400).json(null);
}
})
it=0
missions.forEach(mission => {
const sql=`INSERT INTO missions SET ?`
console.log(mission)
connection.query(sql, mission, error=>{
if(error) throw error;
console.log(it+" - upload mission complete!")
})
it++
});
it=0
challenges.forEach(challenge => {
const sql=`INSERT INTO challenges SET ?`
console.log(challenge)
connection.query(sql, challenge, error=>{
if(error) throw error;
console.log(it+" - upload challenge complete!")
})
it++
});
it=0
challenges_images.forEach(challenge_image => {
const sql=`INSERT INTO challenges_images SET ?`
console.log(challenge_image)
connection.query(sql, challenge_image, error=>{
if(error) throw error;
console.log(it+" - upload challenge_image complete!")
})
it++
});
it=0
users.forEach(user => {
const sql=`INSERT INTO users SET ?`
console.log(user)
connection.query(sql, user, error=>{
if(error) throw error;
console.log(it+" - upload user complete!")
})
it++
});
it=0
resources.forEach(resource => {
const sql=`INSERT INTO resources SET ?`
console.log(resource)
connection.query(sql, resource, error=>{
if(error) throw error;
console.log(it+" - upload resource complete!")
})
it++
});
})
app.get("/missions", (req,res)=>{
const sql="SELECT * FROM missions"
connection.query(sql,(error, results)=>{
if(error) throw error;
if(results.length>0){
res.status(200).json(results)
}else{
res.status(400).json(null);
}
})
})
app.get("/users", (req,res)=>{
const sql="SELECT * FROM users"
connection.query(sql,(error, results)=>{
if(error) throw error;
if(results.length>0){
res.status(200).json(results)
}else{
res.status(400).json(null);
}
})
})
app.get("/missions/:id", (req,res)=>{
const {id} = req.params;
var object_result;
const sql=`SELECT * FROM missions WHERE id_mission = ${id} `
connection.query(sql,(error, result)=>{
if(error) throw error;
if(result.length>0){
object_result = Object.values(JSON.parse(JSON.stringify(result)));
console.log(object_result[0])
const sql=`SELECT * FROM images WHERE id_image = ${object_result[0].id_image} `
console.log("Searching image with id "+object_result[0].id_image+"...")
connection.query(sql,(error, image_result)=>{
if(error){
console.log("ERRORRRRRRRRRRRRRR: ")
throw error;
}
console.log(image_result, image_result.length)
if(image_result.length>0){
console.log("IMAGEN:",image_result)
object_result[0].image=Object.values(JSON.parse(JSON.stringify(image_result)))[0];
res.status(200).json(object_result[0])
}
})
// searchImage(object_result[0].id_image).then((image_result)=>{
// })
}else{
res.status(400).json(null);
}
})
})
function searchImage(id){
const sql=`SELECT * FROM images WHERE id_image = ${id} `
console.log("Searching image with id "+id+"...")
connection.query(sql,(error, result)=>{
if(error){
console.log("ERRORRRRRRRRRRRRRR: ")
throw error;
}
console.log(result, result.length)
if(result.length>0){
return result[0]
}else{
return {}
}
})
}
app.get("/challenges/:id", (req,res)=>{
const {id} = req.params;
const sql=`SELECT * FROM challenges WHERE id_challenge = ${id} `
connection.query(sql,(error, result)=>{
if(error) throw error;
if(result.length>0){
// sql=`SELECT * FROM images WHERE id = 1 `
res.status(200).json(result[0])
}else{
res.status(400).json(null);
}
})
})
app.get("/resources/:id", (req,res)=>{
const {id} = req.params;
const sql=`SELECT * FROM resources WHERE id_resource = ${id} `
connection.query(sql,(error, result)=>{
if(error) throw error;
if(result.length>0){
res.status(200).json(result[0])
}else{
res.status(400).json(null);
}
})
})
app.post("/login/", (req,res)=>{
// console.log(req.body)
var object_result;
var email = req.body.email;
email=email.replace("@","")
var password = req.body.password;
const sql=`SELECT * FROM users HAVING email = '${email}' `
connection.query(sql,(error, result)=>{
if(error) throw error;
if(result.length>0){
object_result = Object.values(JSON.parse(JSON.stringify(result)));
// console.log(object_result[0].PASSWORD, password)
if(object_result[0].PASSWORD!=password){
res.status(400).json(false);
}else{
console.log(true)
res.status(200).json(true)
}
}else{
res.status(400).json(null);
}
})
})
app.post("/image/add", (req,res)=>{
// console.log(req.body)
var image={
url: req.body.url,
description: req.body.description
};
const sql=`INSERT INTO images SET ?`
// console.log(image)
connection.query(sql, image, function(error, result){
// console.log("ERRORRRRRR:", error,"\nRESULLLLLLT", result)
if(error){
throw error;
}
res.status(200).json(result.insertId)
// console.log("._. IMAGE "+result.insertId+" UPLOAD")
})
})
/*
app.post("/missions/add", (req,res)=>{
const {id} = req.params;
const sql=`INSERT INTO missions SET ?`
const tag={
name: req.body.name,
value: req.body.value,
}
const imgObject={
url: req.body.url,
description: req.body.description,
}
const missionObject={
coord_x: req.body.coord_x,
coord_y: req.body.coord_y,
tilte: req.body.title,
}
connection.query(sql,(error, result)=>{
if(error) throw error;
if(result.length>0){
res.status(200).json(result)
}else{
res.status(400).json(null);
}
})
}) */
var connection=mysql.createConnection(
{
host:"oceanhackers.mysql.database.azure.com",
user:"oceanhackerAEADA",
password:"Exception_Elongated_Native_Slashed_Barbell_Capsize",
database:"webpage_1",
port:3306,
ssl: true
}
);
connection.connect(error=>{
if(error) console.log("Database NOT WORK :(!")
console.log("Database server running!")
})