-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
144 lines (122 loc) · 4.12 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
'use strict'
// libraries
require('dotenv').config();
const pg = require('pg');
const cors = require('cors');
const express = require('express');
// const superagent = require('superagent');
const methodOverride = require('method-override');
// global variables
const app = express();
const PORT = process.env.PORT || 3001;
// middleware
app.use(cors()); // Allows everyone to access our info
app.set('view engine','ejs'); // EJS templating engine looks for views folder
app.use(methodOverride('_method')); // Turns a POST or GET into PUT or DELETE
app.use(express.static('./public')); // Serves our files from public
app.use(express.urlencoded({extended:true})); // Body parser
// setup PG
const client = new pg.Client(process.env.DATABASE_URL);
client.on('error', err => console.error(err));
app.get('/', handleSearch);
app.get('/about', (request,response) => {response.render('./about');});
// app.get('/favorites', (request,response) => {response.render('./favorites');});
app.get('/favorites', renderFavorites);
app.get('/search', handleSearch);
app.post('/favorites', handleFavorites);
app.post('/updateComments', updateComments);
app.get('alex', renderFavorites);
app.put('/alex/:result_id', alexFunction);
app.post('/deleteFavorites', deleteFavorites);
function alexFunction(request, response){
console.log(request.body);
let comments = request.body.comments;
let id = request.body.id;
console.log('Updating this id',id);
let sql = 'UPDATE items SET comments=$1 where id=$2;';
let safeValues = [comments, id];
client.query(sql, safeValues)
.then(() => {
response.redirect('/favorites');
})
}
function deleteFavorites (request, response) {
let id = request.body.id;
let sql = 'DELETE FROM items WHERE id=$1;';
let safeValues = [id];
client.query(sql, safeValues)
.then(() => {
response.redirect('/favorites');
})
}
function renderFavorites (request, response){
let sql = 'Select * FROM items;';
client.query(sql)
.then(res => {
let videos = res.rows;
// console.log('videos',videos);
response.render('./favorites', ({apples : videos}));
})}
function handleFavorites (request, response){
// console.log('favorites request', request.body);
let{name, picture, locations, comments} = request.body;
let sql = 'INSERT INTO items (name, picture, locations, comments) VALUES ($1, $2, $3, $4);';
let safeValues = [name, picture, locations, comments];
client.query(sql, safeValues)
// .then(response => {
// // let id = results.rows.id;
// response.render('./favorites');
.then(() => {
response.redirect('/favorites');
})
}
function updateComments(request, response){
let id = request.body.id;
let sql = 'SELECT * FROM items WHERE id=$1;';
let safeValues = [id];
client.query(sql, safeValues)
.then((results) => {
response.render('./edit', ({myEdit : results.rows}));
})
}
//API Request
const request = require('request');
var videoArray = [];
function handleSearch (req, res){
videoArray = [];
let xyz = req.query.search
const options = {
method: 'GET',
// type: 'JSON',
url: 'https://utelly-tv-shows-and-movies-availability-v1.p.rapidapi.com/lookup',
qs: {term: `${xyz}`, country: 'us'},
headers: {
'x-rapidapi-host': 'utelly-tv-shows-and-movies-availability-v1.p.rapidapi.com',
'x-rapidapi-key': `${process.env.UTELY_API_KEY}`}};
request(options, function (error, response, body) {
if (error) throw new Error(error);
let allResults = JSON.parse(response.body);
allResults.results.forEach(result=>{
new Video(result);});
res.render('./index', {bananas: videoArray})}
)}
function Video(obj){
this.name = obj.name;
this.picture = obj.picture;
this.locations = obj.locations.map((value) => {
return value.display_name;
})
// this.providericon = obj.locations.map((value) => {
// console.log("inside .map", value.icon);
// return value.icon;
// })
this.comments = '';
// console.log('this.providericon', this.providericon);
videoArray.push(this);
}
client.connect()
.then(() => {
app.listen(PORT, () => {
console.log(`listening on ${PORT}`)
})
});