-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
385 lines (330 loc) · 13.2 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'use strict';
let nameArray = [];
let idArray = [];
const express = require('express');
const pg = require('pg');
const superagent = require('superagent');
require('dotenv').config();
require('ejs');
const methodOverride = require('method-override');
const { render } = require('ejs');
const app = express();
const PORT = process.env.PORT || 3001;
const client = new pg.Client(process.env.DATABASE_URL);
client.on('client error', error => {
console.log('client error: ', error);
});
app.set('view engine', 'ejs');
// middleware
app.use(express.static('./public'));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
// paths
app.post('/pages', renderResults);
app.get('/', renderHome);
app.get('/index/:id', renderIndex);
app.post('/addplant', addToGreenhouse);
app.get('/greenhouse/:id', renderGreenhouse);
app.delete('/greenhouse/:id', deletePlant);
app.delete('/notes/:id', deleteNote);
app.get('/details/:id', renderDetails);
app.get('/aboutUs/:id', renderAboutUs);
app.put('/addNote/:id', addNotes);
app.put('/updateNotes/:id', updateNotes);
app.post('/addUser', addUser);
app.post('/signIn', signIn);
app.get('/error/:id', renderError);
app.post('/createplant/:id', createPlant);
app.post('/newplant', newPlant);
app.use('*', (request, response) => response.status(404).send('Page not Found'));
// ----- Functions -------
function renderHome(request, response)
{
response.render('login');
}
//---------------------------
function renderIndex(request, response)
{
response.status(200).render('index', {user: request.params.id, not_found: '', newplant: '', add_button: ''})
}
//-----------------------------
function renderResults(request, response)
{
let found = false;
let user_key = request.body.user_key; //grab user key
let userSearch = request.body.search; // grab user search
if(userSearch === '') // make sure user entered something
{
response.status(200).render('index', {user: user_key, not_found: '', newplant: '', add_button: ''});
}else
{
let searchName = userSearch.toLowerCase().replace(/\s+/g, '').replace(/s$/, '');
const url = 'http://harvesthelper.herokuapp.com/api/v1/plants'; //api url
let queryParams = {
api_key: process.env.PLANTS_API_KEY
}
const imageHash = 'https://res-5.cloudinary.com/do6bw42am/image/upload/c_scale,f_auto,h_300/v1/';
let alreadyExists = false; // set flag to false
let sql = 'SELECT * FROM greenhouse WHERE search_name=$1 AND user_key=$2;'; //search database for match
let safeValue = [searchName, user_key]; //pass in searchName and user keys
client.query(sql, safeValue).then (results =>
{
if(results.rowCount > 0) // check if plant was found in database
{
alreadyExists = true; // set flag to true
response.render('pages/results.ejs', { target: results.rows[0], targetImg: '', alreadyExists: alreadyExists, user: user_key, search: searchName}); //render results page: pass in plant from db, change greenhouse button
} else
{
alreadyExists = false;
superagent.get(url).query(queryParams).then(results =>
{
if(nameArray.length < 1) //create nameArray & idArray first time thru
{
nameArray = results.body.map(plant => plant.name.toLowerCase().replace(/\s+/g, '').replace(/s$/, ''));
idArray = results.body.map(plant => plant.id)
}
// search nameArray for match and grab index of match
let index = nameArray.indexOf(searchName)
if(index > -1) //check if match was found
{
results.body.forEach(plant => // go through each plant from api
{
if(plant.id === idArray[index]) // find plant id that matches id at the matching index
{
found = true
response.status(200).render('pages/results.ejs', { target: plant, targetImg: imageHash, alreadyExists: alreadyExists, user : user_key, search: searchName});
}
})
}
if(found === false)
{
let buttonString = `add ${userSearch}`
response.status(200).render('index', {user: user_key, not_found: `Unable to find ${userSearch}`, newplant: userSearch, add_button: buttonString});
}
}).catch((error) => {
console.log('ERROR', error);
response.redirect(`pages/error/${user_key}`);
})
}
})
}
}
//-----------------------
function addToGreenhouse(request, response)
{
let {name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, search_name, user_key} = request.body;
let sql = 'INSERT INTO greenhouse (name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, search_name, user_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) RETURNING id;';
image_url = `https://res-5.cloudinary.com/do6bw42am/image/upload/c_scale,f_auto,h_300/v1/${image_url}`;
let safeValues = [name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, search_name, user_key];
client.query(sql, safeValues)
.then(() =>
{
let imageHash = 'https://res-5.cloudinary.com/do6bw42am/image/upload/c_scale,f_auto,h_300/v1/';
let alreadyExists = true;
response.render('pages/results.ejs', { target: request.body, targetImg: imageHash, alreadyExists: alreadyExists, user: user_key})
})
}
//------------------------------
function renderGreenhouse(request, response)
{
let id = request.params.id;
let sql2 = 'SELECT zipcode FROM user_table WHERE id=$1'
let safeValue = [id]
client.query(sql2, safeValue)
.then(zippy => {
if(zippy.rowCount > 0)
{
let zip = zippy.rows[0].zipcode;
let url = 'http://api.weatherbit.io/v2.0/current';
let queryParams =
{
key: process.env.WEATHER_API_KEY,
postal_code: zip,
days: 1,
units: 'I'
}
superagent.get(url).query(queryParams).then(day =>
{
let fTemp = day.body.data[0].temp;
let sql = `SELECT * FROM greenhouse WHERE user_key=${id} ;`;
client.query(sql)
.then(plants => {
let plantArray = plants.rows;
response.render('pages/greenhouse', {target: plantArray, user: id, temp: fTemp});
}).catch((error) => {
console.log('ERROR', error);
response.redirect(`pages/error/${id}`);
})
})
}else{
let sql = `SELECT * FROM greenhouse WHERE user_key=${id} ;`;
client.query(sql)
.then(plants => {
let plantArray = plants.rows;
response.render('pages/greenhouse', {target: plantArray, user: id, temp: ''});
}).catch((error) => {
console.log('ERROR', error);
response.redirect(`pages/error/${id}`);
})
}
})
}
//------------------------------------
function deletePlant(request, response)
{
let id = request.params.id;
let sql = 'DELETE FROM greenhouse WHERE id=$1 RETURNING user_key;';
let safeValue = [id];
client.query(sql, safeValue)
.then (theKey => {
response.status(200).redirect(`/greenhouse/${theKey.rows[0].user_key}`);
})
}
//--------------------------------------
function renderDetails(request, response)
{
let id = request.params.id;
let sql = 'SELECT * FROM greenhouse WHERE id=$1;';
let safeValue = [id];
let sql2 = 'SELECT * FROM notes WHERE plant_key=$1;';
client.query(sql, safeValue)
.then(plant => {
client.query(sql2, safeValue)
.then(ourNotes =>
{
response.status(200).render('pages/details',{detailsTarget: plant.rows[0], notesArray: ourNotes.rows, user: plant.rows[0].user_key});
})
}).catch((error) => {
console.log('ERROR', error);
response.redirect(`pages/error/${id}`);
})
}
//-------------------------------------
function deleteNote(request, response)
{
let id = request.params.id;
let sql = 'DELETE FROM notes WHERE id=$1 RETURNING plant_key;';
let safeValue = [id];
client.query(sql, safeValue)
.then (keys => {
let keyID = keys.rows[0].plant_key;
response.status(200).redirect(`/details/${keyID}`)
})
}
//------------------------------------
function addNotes(request, response)
{
let id = request.params.id;
let notes = request.body.notes;
let sql = 'INSERT INTO notes (user_notes, plant_key) VALUES ($1, $2);';
let safeValues = [notes, id];
client.query(sql, safeValues)
.then(() =>
{
response.status(200).redirect(`/details/${id}`);
})
}
//--------------------------------
function addUser(request, response)
{
let {name, email, zipcode} = request.body;
let sql = 'INSERT INTO user_table (name, email, zipcode) VALUES ($1, $2, $3) RETURNING id;';
let safeValues = [name, email, zipcode];
client.query(sql, safeValues)
.then(obj =>
{
response.status(200).redirect(`index/${obj.rows[0].id}`)
})
}
//-------------------------------
function signIn(request, response)
{
let userEmail = request.body.email;
let sql = 'SELECT id FROM user_table WHERE email=$1;';
let safeValues = [userEmail];
client.query(sql, safeValues)
.then(mail => {
if(mail.rowCount > 0)
{
response.status(200).redirect(`index/${mail.rows[0].id}`);
}else{
response.status(200).render('signup', {mail: userEmail});
}
})
}
//--------------------------------
function updateNotes(request, response)
{
let id = request.params.id;
let newNotes = request.body.notes;
let sql = 'UPDATE notes SET user_notes=$1 WHERE id=$2 RETURNING plant_key;';
let safeValues = [newNotes, id];
client.query(sql, safeValues)
.then(hope =>
{
let key = hope.rows[0].plant_key;
response.status(200).redirect(`/details/${key}`);
})
}
//--------------------------------
function createPlant(request, response)
{
response.render('pages/createplant', { user: request.params.id, new_plant: request.body.new_plant});
}
//--------------------
function newPlant(request, response)
{
let {name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, user_key} = request.body;
// make function to put n/a in place of empty strings ??
image_url === '' ? image_url = '/img/veggies.jpg' : '';
description === '' ? description = 'n/a' : '';
optimal_sun === '' ? optimal_sun = 'n/a' : '';
optimal_soil === '' ? optimal_soil = 'n/a' : '';
planting_considerations === '' ? planting_considerations = 'n/a' : '';
when_to_plant === '' ? when_to_plant = 'n/a' : '';
growing_from_seed === '' ? growing_from_seed = 'n/a' : '';
transplanting === '' ? transplanting = 'n/a' : '';
spacing === '' ? spacing = 'n/a' : '';
watering === '' ? watering = 'n/a' : '';
feeding === '' ? feeding = 'n/a' : '';
other_care === '' ? other_care = 'n/a' : '';
diseases === '' ? diseases = 'n/a' : '';
pests === '' ? pests = 'n/a' : '';
harvesting === '' ? harvesting = 'n/a' : '';
storage_use === '' ? storage_use = 'n/a' : '';
let search_name = name.toLowerCase().replace(/\s+/g, '').replace(/s$/, '');
let sql = 'INSERT INTO greenhouse (name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, search_name, user_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) RETURNING id;';
let safeValues = [name, description, image_url, optimal_sun, optimal_soil, planting_considerations, when_to_plant, growing_from_seed, transplanting, spacing, watering, feeding, other_care, diseases, pests, harvesting, storage_use, search_name, user_key];
client.query(sql, safeValues).then(newId =>
{
let id = newId.rows[0].id;
let sql = 'SELECT * FROM greenhouse WHERE id=$1;';
let safeValue = [id];
let sql2 = 'SELECT * FROM notes WHERE plant_key=$1;';
client.query(sql, safeValue).then(plant =>
{
client.query(sql2, safeValue).then(ourNotes =>
{
response.status(200).render('pages/details',{detailsTarget: plant.rows[0], notesArray: ourNotes.rows, user: plant.rows[0].user_key});
})
}).catch((error) => {
console.log('ERROR', error);
response.redirect(`pages/error/${id}`);
})
})
}
//--------------------------------
function renderAboutUs(request, response)
{
response.render('pages/aboutUs', {user : request.params.id});
}
//--------------------------------
function renderError(request, response)
{
response.render('pages/error', {user : request.params.id})
}
//--------------------------------
client.connect()
.then(() => {
app.listen(PORT, () => console.log(`listening on ${PORT}`));
}).catch(err => console.log('error connecting', err));