-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-original.js
173 lines (153 loc) · 6.95 KB
/
server-original.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
'use strict';
// ============== Packages ==============================
const express = require('express');
const superagent = require('superagent');
const pg = require('pg');
const methodOverride = require('method-override');
require('dotenv').config();
// ============== App ===================================
// database setup
const DATABASE_URL = process.env.DATABASE_URL;
const client = new pg.Client(DATABASE_URL);
client.on('error', error => console.log('There was an error like dudh', error));
// Application Setup
const app = express();
const PORT = process.env.PORT || 3232;
// Application Middleware
app.use(express.urlencoded({extended: true})); // tells express to peel off form data and put it into request.body
app.use(express.static(__dirname + '/public'));
app.use(methodOverride('_method'));
// Set the view engine for server-side templating
app.set('view engine', 'ejs');
// ============== Routes ================================
app.get('/test', handleTest);
app.get('/', getBooksSql);
app.get('/searches/new', (req, res) => {
res.render('pages/searches/new.ejs');
});
app.post('/searches', googleBookRequest);
app.post('/books', saveSingleBook);
app.get('/books/:id', getSingleBook);
app.put('/update/:id', updateBookInfo);
app.delete('/books/:id', deleteBook);
app.get('/books/detail-view/:id', redirectToUpdateBook);
// =========== functions ============
function redirectToUpdateBook(req, res) {
// res.render('pages/books/detail-new.ejs');
const sqlString = 'SELECT * FROM book_table WHERE id = $1;';
const sqlArray = [req.params.id]; //params gives you the parameter of what was in the url/
client.query(sqlString, sqlArray)
.then (result => {
const ejsObject = { bookSearchArray: result.rows[0]};
// console.log('', result.rows[0]);
res.render('./pages/books/detail-new.ejs', ejsObject);
})
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books GETTING FROM DB');
});
}
function updateBookInfo(req, res) {
// console.log('==================================================================================', req.body.pub_date);
let sqlString4 = `UPDATE book_table SET authors=$1, title=$2, isbn=$3, image_url=$4, book_description=$5, pub_date=$6 WHERE id=$7;`;
let sqlArray4 = [req.body.authors, req.body.title, req.body.isbn, req.body.image_url, req.body.book_description, req.body.pub_date, req.params.id];
// console.log(sqlArray4);
client.query(sqlString4, sqlArray4)
.then(results => {
res.redirect(`/books/${req.params.id}`);
})
// .then(res.redirect('/books'))
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books UPDATE ');
});
}
function deleteBook (req, res) {
let sqlString3 = `DELETE FROM book_table WHERE id=$1;`;
let sqlArray3 = [req.params.id];
client.query(sqlString3, sqlArray3)
.then(results => {
res.redirect('/');
})
// .then(res.redirect('/books'))
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books DELETE ');
});
}
function saveSingleBook (req, res) {
// future to do - check if book already in db - if not save - if yes - redirect to detail page - message already in your collection
const sqlString2 = `INSERT INTO book_table (authors, title, isbn, image_url, book_description, pub_date) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id;`;
// console.log(sqlString2);
const sqlArray2 = [req.body.authors, req.body.title, req.body.isbn, req.body.image_url, req.body.book_description, req.body.pub_date];
client.query(sqlString2, sqlArray2)
.then(results => {
// const ejsObject3 = {bookSearchArray: req.body};
res.redirect('/books/' + results.rows[0].id);
// res.render('pages/books/detail.ejs', ejsObject3);
})
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books SAVING to DB ');
});
}
function getSingleBook (req, res) {
const sqlString = 'SELECT * FROM book_table WHERE id = $1;';
const sqlArray = [req.params.id]; //params gives you the parameter of what was in the url/
client.query(sqlString, sqlArray)
.then (result => {
const ejsObject = { bookSearchArray: result.rows[0]};
// console.log('', result.rows[0]);
res.render('./pages/books/detail.ejs', ejsObject);
})
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books GETTING FROM DB');
});
}
function getBooksSql (req, res) {
const sqlString = 'SELECT * FROM book_table;';
client.query(sqlString)
.then (result => {
const sqlObject = { bookSearchArray: result.rows};
res.render('./pages/index.ejs', sqlObject);
})
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with books GETTING ALL BOOKS FROM DB');
});
}
function handleTest(req, res) {
// res.render('./pages/index.ejs');
res.send('The test is working - you are awesome - keep going');
}
function googleBookRequest(req, res) {
let bookSearch = req.body.bookSearch; //because it is a POST method data shows up in req.body
let search = req.body.search;
let url = `https://www.googleapis.com/books/v1/volumes?q=in${search}:${bookSearch}`;
superagent.get(url)
.then(bookDataFromGoogle => {
const bookSearchArray = bookDataFromGoogle.body.items.map(bookResults => new Books(bookResults));
res.render('pages/searches/show.ejs', {bookSearchArray:bookSearchArray});
// res.redirect('pages/searches/show.ejs');
})
.catch(errorThatComesBack => {
console.log(errorThatComesBack);
res.status(500).send('Sorry something went wrong with looking up books at google');
});
}
function Books(bookObj) {
this.authors = bookObj.volumeInfo.authors ? bookObj.volumeInfo.authors : 'sorry no authors for this item';
this.title = bookObj.volumeInfo.title ? bookObj.volumeInfo.title : 'sorry no title for this item';
this.isbn = bookObj.volumeInfo.industryIdentifiers[1] ? bookObj.volumeInfo.industryIdentifiers[1].type + ': ' + bookObj.volumeInfo.industryIdentifiers[1].identifier : 'no ISBN number';
this.image_url = bookObj.volumeInfo.imageLinks ? bookObj.volumeInfo.imageLinks.smallThumbnail.replace(/^http:\/\//i, 'https://') : 'https://i.imgur.com/J5LVHEL.jpg';
this.book_description = bookObj.volumeInfo.description ? bookObj.volumeInfo.description : 'sorry no description for this item';
this.pub_date = bookObj.volumeInfo.publishedDate ? bookObj.volumeInfo.publishedDate : 'sorry no publishing date for this item';
}
// ============== Initialization ========================
client.connect()
.then(() => {
app.listen(PORT, () => console.log(`up on http://localhost:${PORT}`));
}).catch(errorThatComesBack => {
console.log(errorThatComesBack);
});