-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
371 lines (354 loc) · 12.7 KB
/
server.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
'use strict';
//================================Dependencies==========================
const express = require('express');
const pg = require('pg');
const superAgent = require('superagent');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT;
//==========================Set EJS==================================
app.set('view engine', 'ejs');
//=====================FOR URL========================================
app.use(express.urlencoded({extended: true,}));
app.use(express.static(__dirname + '/public'));
app.use(express.static('./public'));
//=============================PG setup===============================
const client = new pg.Client(process.env.DATABASE_URL);
client.connect();
client.on('error', err => console.error(err));
//========================REQUEST CALLS==============================
app.get('/', goHome);
app.post('/available-dogs', goDogs);
app.post('/user', makeUser);
app.get('/about-the-team', aboutTeam);
app.post('/woof-list', woofList);
app.get('/dog-detail', dogDetail);
app.post('/remove-dog', removeDog);
app.post('/likedog', likeDog);
app.post('/dogviewed', viewDog);
//================================HOME=======================================
function goHome(req, res){
res.render('pages/index.ejs');
}
//=============================ABOUT US ===================================
function aboutTeam(request, response){
response.render('pages/about');
}
// ===========================WOOF LIST==================================
function woofList(request, response){
let likedDogs = [];
let id = request.body.username;
let SQL=`SELECT likes FROM users WHERE id = $1`;
client.query(SQL,[id])
.then(data=>{
let likes = JSON.parse(data.rows[0].likes);
let string = '';
for(let i=1 ; i<=likes.length; i++) {
string += '$' + i + ', ';
}
let nstring = string.substring(0, string.length-2);
let SQL2 = `SELECT * FROM dogs WHERE dog_id IN (${nstring})`;
client.query(SQL2, likes)
.then(result => {
result.rows.forEach(row => {
likedDogs.push(new DBDog(row));
})
response.render('pages/wooflist/listShow.ejs',{likedDogs});
}).catch((err)=>{console.log(err)});
}).catch((err)=>{console.log(err)});
}
// =============================DOG DETAIL ================================
function dogDetail(req,res){
let SQL = `SELECT * FROM dogs WHERE dog_id=$1`;
let dogId = req.url.substring(req.url.length-8, req.url.length);
client.query(SQL, [dogId])
.then(data => {
let dog = new DBDog(data.rows[0]);
let SQL = `SELECT * FROM shelters WHERE shelters_id=$1`;
client.query(SQL, [dog.loactionID])
.then(data => {
if(data.rows[0]){
let shelterDeet = new DBShelter(data.rows[0]);
let dogDeets = [dog, shelterDeet];
res.render('pages/wooflist/dogDetail', {dogDeets});
}else{
superAgent.get(`http://api.petfinder.com/shelter.get?key=${process.env.PET_KEY}&format=json&id=${dog.locationID}`)
.then(data => {
let shelterDeet = new IndiShelter(data.body.petfinder.shelter);
let SQL = `INSERT INTO shelters
(shelters_id, name, city, state, zip, phone, email)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id`;
let values = [shelterDeet.shelters_id, shelterDeet.name, shelterDeet.city, shelterDeet.state, shelterDeet.zip, shelterDeet.phone, shelterDeet.email];
client.query(SQL, values).catch(err=>{
console.log(err);
});
let dogDeets = [dog, shelterDeet];
res.render('pages/wooflist/dogDetail', {dogDeets});
})
.catch(err => {
console.log(err);
});
}
})
.catch(err => {
console.log(err);
});
})
.catch(err => {
console.log(err);
});
}
//==================REMOVE DOGS==========================================
//required:dog id and user id
//when this path is called, we will query the users table for all liked dogs of the user that pressed the button,
//then it will parse the array, remove the index of the ID of the dog we want to remove, and append the data in the database
//at the users row.
function removeDog(req, res){
console.log(req,'attempting to a remove a dog from a users liked dogs')
let SQL = `SELECT likes FROM users WHERE id=$1`;
return client.query(SQL, [req.body.username])
.then(data => {
console.log(data.rows[0].likes);
let newData = JSON.parse(data.rows[0].likes);
console.log(newData);
let newerData = newData.filter(ele => {
return ele !== req.body.dogId;
});
console.log(newerData);
let SQL1123 = `UPDATE users
SET likes = $1
WHERE id = $2`;
let newestData = JSON.stringify(newerData);
let values3211 = [newestData, req.body.username];
client.query(SQL1123, values3211);
woofList(req,res);
})
.catch(err => {
console.log(err);
});
}
//==================CHECK USER===========================================
function makeUser(req, res){
let SQL = `INSERT INTO users
(likes, views)
VALUES ($1, $2)
RETURNING id`;
let likes = [];
let views = [];
let values = [JSON.stringify(likes), JSON.stringify(views)];
return client.query(SQL, values)
.then(data =>{
console.log(data.rows[0].id);
res.render('pages/index2.ejs', {userId: data.rows[0].id});
})
.catch(err =>{
console.log(err);
});
}
//==============================SEARCH=====================================
function goDogs(req, res){
//define variables
let dataArray = [];
let views=[];
let dataarr=[]
//get all shelters in the searched area
searchApiForShelters(req.body.search);
client.query(`SELECT views FROM users WHERE id=${req.body.username}`)
.then(data=>{
views=JSON.parse(data.rows[0].views);
superAgent.get(`http://api.petfinder.com/pet.find?key=${process.env.PET_KEY}&format=json&animal=dog&location=${req.body.search}`)
.then(data=>{
data.body.petfinder.pets.pet.forEach(ele => {
dataArray.push(new Dog(ele));
});
let SQL = `INSERT INTO dogs
(dog_id, name, age, gender, size, availability, breed, mix, photos, description, shelter_id, housetrained, fixed, kids, cats, vaccinated)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
ON CONFLICT DO NOTHING`;
dataArray.forEach(ele=>{
let values =[ele.ID, ele.name, ele.age, ele.gender, ele.size, ele.isAdopted, ele.breed, ele.mix, ele.picture, ele.description, ele.locationID, ele.housetrained, ele.fixed, ele.kidFriendly, ele.catFriendly, ele.vaccinated];
client.query(SQL, values).catch(er => console.log(er));
})
dataarr.push(views);
dataarr.push(dataArray);
console.log(dataArray[0].locationID);
res.render('pages/choices/dogShow.ejs', {dataarr});
}).catch(err => {
console.log(err);
})
}).catch(err => {
console.log(err);
})
}
function searchApiForShelters(zip){
return superAgent.get(`http://api.petfinder.com/shelter.find?key=${process.env.PET_KEY}&format=json&location=${zip}`)
.then(data => {
let SQL = `INSERT INTO shelters
(shelters_id, name, city, state, zip, phone, email)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id`;
let dataArray = [];
data.body.petfinder.shelters.shelter.forEach(ele => {
dataArray.push(new Shelter(ele));
});
dataArray.forEach(ele => {
let values = [ele.shelters_id, ele.name, ele.city, ele.state, ele.zip, ele.phone, ele.email];
return client.query(SQL, values);
});
}).catch(err => {
console.log(err);
});
}
//==================likedogs=======================
function likeDog(req, res){
let userid=req.body.userId;
let dogid=req.body.dogId;
let SQL=`SELECT * FROM users WHERE id = $1`;
client.query(SQL,[userid])
.then (data=>{
console.log(data.rows);
let likes = JSON.parse(data.rows[0].likes);
console.log(likes);
let views = JSON.parse(data.rows[0].views);
likes.push(dogid);
views.push(dogid);
let SQL2 = `UPDATE users SET likes=$1, views=$2 WHERE id=$3`;
let value2 = [JSON.stringify(likes), JSON.stringify(views), userid];
client.query(SQL2, value2);
console.log(`dog with id ${dogid} was liked and added to the table @ user id ${userid}`);
}).catch(err => {
console.log(err);
});
}
//====================viewdog==================================================
function viewDog(req,res){
let userid=req.body.userId;
let dogid=req.body.dogId;
let SQL=`SELECT * FROM users WHERE id = $1`;
client.query(SQL,[userid])
.then (data=>{
console.log(data.rows, 'line 137')
let views = JSON.parse(data.rows[0].views);
views.push(dogid);
let SQL2 = `UPDATE users SET views=$1 WHERE id=$2`;
let value2 = [JSON.stringify(views), userid];
client.query(SQL2, value2);
console.log(`dog with id ${dogid} was viewed and added to the table @ user id ${userid}`);
}).catch(err => {
console.log(err);
});
}
//==================CONSTRUCTORS=================================
function Dog(pet){
this.ID = pet.id.$t;
this.locationID = pet.shelterId.$t;
this.name = pet.name.$t;
this.age = pet.age.$t;
this.gender = pet.sex.$t;
this.housetrained = false;
this.size = pet.size.$t;
this.fixed = false;
this.catFriendly = true;
this.kidFriendly = true;
this.vaccinated = false;
this.isAdopted = pet.status.$t;
this.breed = pet.breeds.breed.$t||'Unknown';
this.mix = pet.mix.$t;
this.picture = pet.media.photos.photo[3].$t || 'images/connor-dog.png';
this.description = pet.description.$t;
this.options(pet);
this.opt=pet.options.option;
}
Dog.prototype.options = function(pet){
if (this.gender==='M'){
this.gender='Male';
}else{
this.gender='Female';
}
if (this.size==='S'){
this.size='Small';
}else if (this.size ==='M'){
this.size='Medium';
}else if(this.size==='L'){
this.size='Large';
}else{
this.size='Extra Large';
}
if (Array.isArray(pet.options.option)){
pet.options.option.forEach(ele => {
if(ele.$t === 'noCats'){
this.catFriendly = false;
}else if(ele.$t === 'altered'){
this.fixed = true;
}else if(ele.$t === 'hasShots'){
this.vaccinated = true;
}else if(ele.$t === 'housetrained'){
this.housetrained = true;
}else if(ele.$t === 'noKids'){
this.kidFriendly = false;
}
});
}else if (pet.options.option){
let derp = pet.options.option;
if(derp.$t === 'noCats'){
this.catFriendly = false;
}else if(derp.$t === 'altered'){
this.fixed = true;
}else if(derp.$t === 'hasShots'){
this.vaccinated = true;
}else if(derp.$t === 'housetrained'){
this.housetrained = true;
}else if(derp.$t === 'noKids'){
this.kidFriendly = false;
}
}
}
function DBDog(pet){
this.ID = pet.dog_id;
this.locationID = pet.shelter_id;
this.name = pet.name || 'Not Provided';
this.age = pet.age || 'Not Provided';
this.gender = pet.gender || 'Not Provided';
this.housetrained = pet.housetrained;
this.size = pet.size.$t || 'Not Provided';
this.fixed = pet.fixed;
this.catFriendly = pet.cats;
this.kidFriendly = pet.kids;
this.vaccinated = pet.vaccinated;
this.isAdopted = pet.availability;
this.breed = pet.breed || 'Not Provided';
this.mix = pet.mix || 'Not Provided';
this.picture = pet.photos || 'images/no_img.jpg';
this.description = pet.description || 'Not Provided';
}
function Shelter(shelter){
this.shelters_id = shelter.id.$t;
this.name = shelter.name.$t;
this.city = shelter.city.$t;
this.state = shelter.state.$t;
this.zip = shelter.zip.$t;
this.phone = shelter.phone.$t;
this.email = shelter.email.$t;
}
function DBShelter(shelter){
this.shelters_id = shelter.shelters_id;
this.name = shelter.name;
this.city = shelter.city;
this.state = shelter.state;
this.zip = shelter.zip;
this.phone = shelter.phone;
this.email = shelter.email;
}
function IndiShelter(shelter){
this.shelters_id = shelter.id.$t;
this.name = shelter.name.$t;
this.city = shelter.city.$t;
this.state = shelter.state.$t;
this.zip = shelter.zip.$t;
this.phone = shelter.phone.$t;
this.email = shelter.email.$t;
}
//===========================Listener============================
app.listen(PORT, () => console.log(`APP is up on PORT : ${PORT}`));