-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
806 lines (666 loc) · 22.9 KB
/
index.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Import Dependencies.
import express from 'express';
import pg from 'pg';
import jsSHA from 'jssha';
import cookieParser from 'cookie-parser';
import methodOverride from 'method-override';
import moment from 'moment';
import multer from 'multer';
import aws from 'aws-sdk';
import multerS3 from 'multer-s3';
// Init DB connection.
const { Pool } = pg;
// Separate DB connection configs for production vs non-production environments.
let poolConfigs;
// if (process.env.DATABASE_URL) {
// poolConfigs = {
// connectionString: process.env.DATABASE_URL,
// ssl: {
// rejectUnauthorized: false
// }
// };
// } else
if (process.env.ENV === 'production') {
poolConfigs = {
user: 'postgres',
password: process.env.DB_PASSWORD,
host: 'localhost',
database: 'bookrepo',
port: 5432,
};
} else {
// Set up Remote Postgres server.
poolConfigs = {
user: process.env.USER,
host: 'localhost',
database: 'bookrepo',
port: 5432,
}
}
// Create new Pool with above conditions.
const pool = new Pool(poolConfigs);
// Init Express.
const app = express();
const PORT = process.argv[2] || 3004;
const SALT = process.env['SALT'];
// Configure S3.
const s3 = new aws.S3({
accessKeyId: process.env.ACCESSKEYID,
secretAccessKey: process.env.SECRETACCESSKEY,
});
// Configure multerUpload directoryr.
const multerUpload = multer({
storage: multerS3({
s3,
bucket: 'bucky-bookrepo',
acl: 'public-read',
metadata: (request, file, callback) => {
callback(null, { fieldName: file.fieldname });
},
key: (request, file, callback) => {
callback(null, Date.now().toString());
},
}),
});
// Configure Express settings.
app.set('view engine', 'ejs'); // Set view engine for 'ejs' templates.
app.use('/public', express.static('public')); // Retrieve static files from `public`.
app.use('/uploads', express.static('uploads')); // Retrieve static files from `uploads`.
app.use(express.urlencoded({ extended : false })); // Set up our middleware to parse POST data.
app.use(methodOverride('_method')); // Enable PUT/DELETE headers to be sent in requests.
app.use(cookieParser()); // Configure usage of cookie-parser.
// Helper Functions:
// Helper Function 1 - Convert dates to strings.
const momentFromNow = (date) => {
return moment(date).fromNow();
};
// Helper Function 2 - Generate hash from cookie/password and salt.
const getHash = (input) => {
const shaObj = new jsSHA('SHA-512', 'TEXT', { encoding: 'UTF8' });
const unhashedString = `${input}-${SALT}`;
shaObj.update(unhashedString);
return shaObj.getHash('HEX');
}
// Helper Function 3 - Run auth middleware to check for log-in & userId.
const checkAuth = (req, res, next) => {
console.log("Verifying user authentication. ---")
const { loggedInHash, userId } = req.cookies;
req.isUserLoggedIn = false; // Set default value.
// Check to see if relevant cookies exists.
if (loggedInHash && userId) {
const hash = getHash(userId);
if (loggedInHash === hash) {
req.isUserLoggedIn = true;
}
}
next();
};
// /Root - GET Request. [Ad Page to prompt user to sign up (if not logged in)].
app.get('/', (req, res) => {
console.log('/ root GET request came in! ---')
const { userId } = req.cookies;
// If user is logged-in, redirect to collection page.
if (userId) {
res.redirect('/collection');
}
res.render('root', { userId })
});
// Signup - POST Request. [Accept a POST request to create a user].
app.post('/signup', (req, res) => {
console.log('/signup POST request came in!');
const { name, email, password } = req.body;
const shaObj = new jsSHA('SHA-512', 'TEXT', { encoding: 'UTF8'});
shaObj.update(password);
const hashedPassword = shaObj.getHash('HEX');
const insertUserQuery = `
INSERT INTO users (user_name, email, password)
VALUES ($1, $2, $3)
RETURNING *
`
const values = [ name, email, hashedPassword ];
pool
.query(insertUserQuery, values)
.then(result => {
const newUser = result.rows[0];
res.render('welcome', { newUser })
})
.catch(err => console.log('insertUserQuery error: ---', err))
});
// Login - GET Request. [Render form for user to log in].
app.get('/login', (req, res) => {
console.log('/login GET request came in! ---')
res.render('login');
});
// Login - POST Request. [Accept POST request to log user in].
app.post('/login', (req, res) => {
console.log('/login POST request came in! ---')
const { email, password } = req.body;
const selectUserQuery = `
SELECT * FROM users
WHERE email = $1
`;
pool
.query(selectUserQuery, [ email ])
.then(result => {
// When email does not exist in DB.
if (result.rows.length === 0) {
res.status(403).render('error');
return;
}
// Get user record from result.
const user = result.rows[0];
// Generate hashed password from what was keyed in log-in form.
const shaPassword = new jsSHA('SHA-512', 'TEXT', { encoding: 'UTF8' });
shaPassword.update(password);
const hashedPassword = shaPassword.getHash('HEX');
if (user.password !== hashedPassword) {
res.status(403).render('error');
return;
}
// Generate hashed cookie value.
const shaCookie = new jsSHA('SHA-512', 'TEXT', { encoding: 'UTF8' });
const unhashedCookieString = `${user.id}-${SALT}`;
shaCookie.update(unhashedCookieString);
const hashedCookieString = shaCookie.getHash('HEX');
res.cookie('loggedInHash', hashedCookieString);
res.cookie('userId', user.id);
res.redirect('/collection')
})
.catch(err => {
res.status(503).send(result.rows);
return;
})
});
// Logout - POST Request. [Log user out by deleting cookies].
app.delete('/logout', (req, res) => {
console.log('/logout DELETE request came in');
res.clearCookie('userId');
res.clearCookie('loggedInHash');
res.redirect('/')
});
// (Universal) Books - GET Request. [Render a list of entire database of books].
app.get('/books', (req, res) => {
console.log('/books GET request came in!')
const { userId } = req.cookies;
const allBooksQuery = `
WITH bookauthors AS (
SELECT authors.author_name, book_authors.book_id, authors.id
FROM authors
INNER JOIN book_authors ON authors.id = book_authors.author_id
),
bookgenres AS (
SELECT genres.genre_name, book_genres.book_id
FROM genres
INNER JOIN book_genres ON genres.id = book_genres.genre_id
)
SELECT
books.id,
books.book_cover,
books.title,
books.description,
STRING_AGG(DISTINCT bookauthors.author_name, ', ') authors_names,
STRING_AGG(DISTINCT bookgenres.genre_name, ', ') genres_names
FROM books
INNER JOIN bookauthors ON books.id = bookauthors.book_id
INNER JOIN bookgenres ON books.id = bookgenres.book_id
GROUP BY 1,2,3,4
`
pool
.query(allBooksQuery)
.then(result => {
console.log('allBooksQuery results: --- ', result.rows);
const allBooksArr = result.rows;
res.render('books', { allBooksArr, userId })
})
.catch(err => console.log('/books GET request Query Error', err));
});
// Books/:id - GET Request. [Render a single book from entire database of books].
app.get('/books/:id', (req, res) => {
console.log('/books/:id GET request came in!');
const { userId } = req.cookies;
const { id } = req.params;
const singleBookQuery = `
WITH bookauthors AS (
SELECT authors.author_name, book_authors.book_id, authors.id
FROM authors
INNER JOIN book_authors ON authors.id = book_authors.author_id
),
bookgenres AS (
SELECT genres.genre_name, book_genres.book_id
FROM genres
INNER JOIN book_genres ON genres.id = book_genres.genre_id
)
SELECT
books.id,
books.book_cover,
books.title,
books.description,
STRING_AGG(DISTINCT bookauthors.author_name, ', ') authors_names,
STRING_AGG(DISTINCT bookgenres.genre_name, ', ') genres_names
FROM books
INNER JOIN bookauthors ON books.id = bookauthors.book_id
INNER JOIN bookgenres ON books.id = bookgenres.book_id
GROUP BY 1,2,3,4
HAVING books.id = $1
`
pool.query(singleBookQuery, [id], (err , result) => {
if (err) {
console.log('/books/:id GET request singleBookQuery Error', err);
res.status(503);
return;
}
console.log('singleBookQuery results: ---', result.rows);
const singleBook = result.rows[0];
const checkBookExistsQuery = `
SELECT book_id
FROM collection
WHERE user_id = $1 AND book_id IN ($2)
`;
pool.query(checkBookExistsQuery, [userId, id], (checkBookExistsQueryErr, checkBookExistsQueryResult) => {
if (checkBookExistsQueryErr) {
console.log('/books/:id GET request checkBookExistsQuery error', checkBookExistsQueryErr);
res.status(503);
return;
}
const bookExistsArr = checkBookExistsQueryResult.rows;
const notesQuery = `
SELECT
description,
private
FROM notes
WHERE private = false AND book_id = $1
`;
pool.query(notesQuery, [id], (notesQueryErr, notesQueryResult) => {
if (notesQueryErr) {
console.log('/books/:id GET request notesQueryErr error', notesQueryErr);
res.status(503);
return;
}
const publicNotes = notesQueryResult.rows;
res.render('books-id', { singleBook, bookExistsArr, publicNotes })
})
});
});
});
// Books/:id - POST Request. [Push book metadata into user collection].
app.post('/books/:id', (req, res) => {
console.log('/books/:id POST request came in!');
const { userId } = req.cookies;
const bookId = req.params.id;
const dateNow = moment().format('YYYY-MM-DD')
const bookCoverQuery = `
SELECT book_cover
FROM books
WHERE id = $1
`
const insertCollectionQuery = `
INSERT INTO collection (user_id, book_id, user_cover, pages_completed, date_added, date_completed)
VALUES ($1, $2, $3, $4, $5, $6)
`;
pool
.query(bookCoverQuery, [ bookId ])
.then(result => {
let bookCover = result.rows[0].book_cover;
const values = [userId, bookId, bookCover, 0, dateNow, 'infinity']
return pool.query(insertCollectionQuery, values);
})
.then(result => {
console.log("Added to colletion table SUCCESS.")
res.redirect(`/books/${bookId}`);
})
.catch(err => console.log('/books/:id query errror: ---', err));
});
// Collection - GET Request. [Render list of user's collected books].
app.get('/collection', checkAuth, (req, res) => {
console.log('/collection/ GET request came in! ---')
// Redirect user if not logged in.
if (req.isUserLoggedIn === false) {
res.status(403).send('sorry');
return;
}
const { view } = req.query;
const { userId } = req.cookies;
let collectionArr, booksAuthors, noteCounts;
const collectionQuery = `
SELECT *, RANK() OVER (ORDER BY id ASC) AS book_rank
FROM collection
WHERE user_id = ${userId}
`
pool.query(collectionQuery)
.then(collectionResult => {
collectionArr = collectionResult.rows;
if (collectionArr.length === 0) {
throw new Error('Empty collection');
}
// Create an array of book_ids which will go in the next query.
const bookIdArr = collectionArr.map(book => book.book_id)
const bookQuery = `
SELECT
books.id AS book_id,
books.book_cover,
books.title,
books.description,
book_authors.author_id
FROM books
INNER JOIN book_authors ON books.id = book_authors.book_id
WHERE books.id IN (${[...bookIdArr]})
`;
console.log('test', bookQuery);
return pool.query(bookQuery)
})
.then(booksResult => {
const collectionAuthors = booksResult.rows;
// Create an array of author_ids from the bookQuery in previous query.
const authorIds = collectionAuthors.map(author => author.author_id);
const bookAuthorsQuery = `
SELECT
book_authors.book_id,
authors.author_name
FROM book_authors
INNER JOIN authors ON book_authors.author_id = authors.id
WHERE book_authors.author_id IN (${[...authorIds]})
`;
return pool.query(bookAuthorsQuery)
})
.then(bookAuthorsResult => {
booksAuthors = bookAuthorsResult.rows;
const authorNames = {};
for (let i = 0; i < booksAuthors.length; i += 1) {
const booksAuthor = booksAuthors[i];
if (!authorNames[booksAuthor.book_id]) {
authorNames[booksAuthor.book_id] = [];
}
authorNames[booksAuthor.book_id].push(booksAuthor.author_name);
}
// Return newly formed authorNames
for (let i = 0; i < collectionArr.length ; i ++) {
// Create new key (author_names), with values (array of authors) from authorNames object.
collectionArr[i].author_names = authorNames[collectionArr[i].book_id];
}
const noteCountsQuery = `
SELECT
DISTINCT book_id,
user_id,
COUNT(description) note_counts
FROM notes
GROUP BY 1, 2
HAVING user_id = $1
ORDER BY 1`;
return pool.query(noteCountsQuery, [userId]);
})
.then(noteCountsResult => {
noteCounts = noteCountsResult.rows;
const bookIds = collectionArr.map(book => book.book_id);
const bookInfoQuery = `
SELECT *
FROM books
WHERE id IN (${bookIds})
`
return pool.query(bookInfoQuery)
})
.then(bookInfoResult => {
const bookInfo = bookInfoResult.rows;
// Add respective book info to the user's collection.
for (let i = 0; i < collectionArr.length; i++) {
for (let j = 0; j < bookInfo.length; j++) {
if (bookInfo[j].id === collectionArr[i].book_id) {
collectionArr[i].book_cover = bookInfo[j].book_cover
collectionArr[i].title = bookInfo[j].title
collectionArr[i].description = bookInfo[j].description
}
}
}
// Add note counts to respective book_ids.
for (let i = 0 ; i < collectionArr.length; i++) {
collectionArr[i].note_counts = 0;
for (let j = 0; j < noteCounts.length; j++) {
if (collectionArr[i].book_id === noteCounts[j].book_id) {
collectionArr[i].note_counts = Number(noteCounts[j].note_counts);
}
}
}
// Convert datetime to Moment string.
collectionArr.forEach(bookListing => {
bookListing.date_added = momentFromNow(bookListing.date_added);
});
// 1st view: Book in Cover format
if (view === 'covers') {
res.render('collection-cover', { collectionArr });
// 2nd view: Book in Table List format
} else {
// res.send(collectionArr);
res.render('collection-table', { collectionArr });
}
})
// This catch is for anything bad that can happen in the .then()s.
.catch(err => {
if (err.message === 'Empty collection') {
// This is only for when the collection is empty.
res.render('empty-collection-table');
} else {
res.render('error')
}
})
});
// Collection - DELETE Request. [Delete book from collection list].
app.delete('/collection/:id', (req, res) => {
console.log('/collection/:id DELETE request came in!');
const { userId } = req.cookies;
const bookId = req.params.id;
const deleteBookQuery = `
DELETE FROM collection
WHERE user_id = $1
AND book_id = $2
RETURNING *
`
const values = [userId, bookId]
pool.query(deleteBookQuery, values)
.then(deletedBookResult => {
res.redirect(`/collection`)
})
.catch(err => console.log('/collection/:id DELETE request err', err));
});
// Collection/:id - GET Request. [Render one of user's books along with it's user cover & user notes].
app.get('/collection/:id', checkAuth, (req, res) => {
console.log('/collection/:id GET request came in! ---')
// Redirect user if not logged in.
if (req.isUserLoggedIn === false) {
res.status(403).send('sorry');
return;
}
let { userId } = req.cookies;
const { id } = req.params;
const singleBookQuery = `
WITH bookauthors AS (
SELECT authors.author_name, book_authors.book_id, authors.id
FROM authors
INNER JOIN book_authors ON authors.id = book_authors.author_id
),
bookgenres AS (
SELECT genres.genre_name, book_genres.book_id
FROM genres
INNER JOIN book_genres ON genres.id = book_genres.genre_id
),
userscollection AS (
SELECT
users.user_name,
users.id user_id,
RANK() OVER (ORDER BY collection.id ASC) AS book_rank,
collection.book_id,
collection.user_cover,
collection.num_pages,
collection.pages_completed,
collection.date_added,
collection.date_completed
FROM users
INNER JOIN collection ON users.id = collection.user_id
WHERE users.id = $1
)
SELECT
books.id AS book_id,
books.book_cover,
books.title,
books.description,
userscollection.user_cover,
userscollection.book_rank,
userscollection.num_pages,
userscollection.pages_completed,
STRING_AGG(DISTINCT bookauthors.author_name, ', ') authors_names,
STRING_AGG(DISTINCT bookgenres.genre_name, ', ') genres_names
FROM books
INNER JOIN bookauthors ON books.id = bookauthors.book_id
INNER JOIN bookgenres ON books.id = bookgenres.book_id
INNER JOIN userscollection ON books.id = userscollection.book_id
GROUP BY 1,2,3,4,5,6,7,8
HAVING userscollection.book_rank = $2
ORDER BY userscollection.book_rank`;
pool.query(singleBookQuery, [userId, id], (singleBookQueryErr, singleBookQueryResult) => {
if (singleBookQueryErr) {
console.log('/collection/:id GET request singleBookQueryErr:', singleBookQueryErr.stack);
res.status(503);
return;
}
const singleBook = singleBookQueryResult.rows[0];
const notesQuery = `
SELECT *
FROM notes
WHERE user_id = $1 AND book_id = $2
ORDER BY id DESC;
`;
pool.query(notesQuery, [userId, singleBook.book_id], (notesQueryErr, notesQueryResult) => {
if (notesQueryErr) {
console.log('/collection/:id GET request notesQueryErr', notesQueryErr.stack);
res.status(503);
return;
}
const userNotesArr = notesQueryResult.rows;
// Pass in the pages completed perecentage.
singleBook.pct_complete = Math.round((singleBook.pages_completed / singleBook.num_pages) * 100);
singleBook.pct_complete = (isNaN(Number(singleBook.pct_complete)) === true) ? 0 : singleBook.pct_complete;
res.render('collection-id', { singleBook, userNotesArr })
});
});
});
// Collection/:id - PUT Request. [Update user book cover].
app.put('/collection/:id/:bookrank_id', multerUpload.single('usercover'), (req, res) => {
console.log('/collection/:id/:bookrank_id PUT request came in! ---')
const { id, bookrank_id } = req.params;
const { filename } = req.file;
const { userId } = req.cookies;
const insertCoverQuery = `
WITH userbooksordered AS (
SELECT
user_id,
book_id,
RANK() OVER (ORDER BY book_id) AS collection_book_id
FROM collection
WHERE user_id = $1
)
UPDATE collection
SET user_cover = $2
WHERE user_id = $1 AND book_id = $3
RETURNING *`;
pool
.query(insertCoverQuery, [userId, filename, id])
.then(result => {
const insertCoverResults = result.rows;
console.log('insertCoverQuery results: ---', insertCoverResults)
res.redirect(`/collection/${bookrank_id}`);
})
.catch(err => {
res.status(503).render('error');
console.log('/collection/:id POST Request query error: ---', err)
});
});
// Collection/:book_id/pages - PUT Request. [Update page info of a book in user collection].
app.put('/collection/:id/:bookrank_id/pages', (req, res) => {
console.log('/collection/:id/:bookrank_id/pages PUT request came in! ---')
const { id, bookrank_id } = req.params;
const { userId } = req.cookies;
const { pagesCompleted, totalPages } = req.body;
const updatePagesQuery = `
UPDATE collection
SET
num_pages = $1,
pages_completed = $2
WHERE user_id = $3 AND book_id = $4
RETURNING *
`
const values = [totalPages, pagesCompleted, userId, id]
pool
.query(updatePagesQuery, values)
.then(pagesResult => {
console.log('updatePagesQuery result: ---', pagesResult.rows)
res.redirect(`/collection/${bookrank_id}`);
})
.catch(err => console.log('/collection/:id/pages PUT request error: --', err.stack))
});
// Notes/:book_id/:bookrank_id - POST Request. [Insert new note for user].
app.post('/notes/:book_id/:bookrank_id', (req, res) => {
console.log('notes/:book_id/:bookrank_id POST request came in!')
let { userId } = req.cookies;
const { description } = req.body;
const { book_id, bookrank_id} = req.params;
const insertNotesQuery = `
INSERT INTO notes (user_id, description, book_id, private, date)
VALUES ($1, $2, $3, $4, $5)
`
const values = [userId, description, book_id, 'false', '2019-11-14']
pool.query(insertNotesQuery, values, (err, result) => {
if (err) {
console.log('insertNotesQuery error: ---', err)
}
res.redirect(`/collection/${bookrank_id}#my-notes`)
});
});
// Notes/:note_id/:bookrank_id - DELETE Request. [Delete note for user].
app.delete('/notes/:note_id/:bookrank_id', (req, res) => {
console.log('notes/:note_id/:bookrank_id DELETE request came in!')
const { note_id, bookrank_id } = req.params;
const deleteNoteQuery =
`
DELETE FROM notes
WHERE id = ${note_id}
RETURNING *
`
pool
.query(deleteNoteQuery)
.then(result => {
res.redirect(`/collection/${bookrank_id}#my-notes`)
})
.catch(err => console.log('notes/:note_id DELETE error: ---', err));
});
// Notes/:note_id/:bookrank_id - PUT Request. [Edit note for user].
app.put('/notes/:note_id/:bookrank_id', (req, res) => {
console.log('notes/:note_id/:bookrank_id PUT request came in!')
const { note_id, bookrank_id } = req.params;
let { editNoteDescription, makePublic } = req.body;
// If makePublic was an array, means user chose to make the note public.
(Array.isArray(makePublic) === false) ? makePublic = true : makePublic = false
const editNoteQuery = `
UPDATE notes
SET
description = $1,
private = $2
WHERE id = $3
RETURNING *
`
const values = [ editNoteDescription, makePublic, note_id ];
pool
.query(editNoteQuery, values)
.then(result => {
res.redirect(`/collection/${bookrank_id}#my-notes`);
})
.catch(err => console.log('editNoteQuery error: ---', err));
});
app.get('/invite', (req, res) => {
console.log('/invite GET request came in!')
res.render('invite-friend');
});
// Start socket and listen on given port.
app.listen(PORT, (err) => {
if (err) {
console.log('Timeout - error listening to port.');
}
console.log('Success! Connected to port', PORT);
});