-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
114 lines (98 loc) · 3.41 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
'use strict';
const superagent = require('superagent');
require('dotenv').config();
const pg = require('pg');
const express = require('express');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 3000;
const app = express();
const conString = process.env.DATABASE_URL;
const client = new pg.Client(conString);
client.connect();
client.on('error', err => console.error(err));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('./public'));
app.get('/game', (request, response) => response.sendFile('index.html', { root: './public' }));
app.get('/about', (request, response) => response.sendFile('index.html', { root: './public' }));
loadDB();
var sources = [`the-new-york-times`, `the-huffington-post`, `usa-today`, `daily-mail`, `the-wall-street-journal`];
sources.forEach((source) => {
app.get(`/${source}`, (request, response) => {
superagent
.get(`https://newsapi.org/v1/articles?source=${source}&sortBy=top&apiKey=${process.env.API_KEY}`)
.end((err, superagentResponse) => response.send(superagentResponse.text));
});
});
app.post('/articles', (request, response) => {
client.query(`
INSERT INTO
articles(title, description, url, source_id, author, url_to_image, published_at)
VALUES($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT DO NOTHING`,
[request.body.title, request.body.description, request.body.url, request.body.sourceId, request.body.author, request.body.urlToImage, request.body.publishedAt],
function (err) {
if (err) console.error(err);
})
.then(() => {
client.query(`
UPDATE sources SET
count_left = count_left + $1,
count_center_left =count_center_left + $2,
count_center =count_center + $3,
count_center_right =count_center_right + $4,
count_right =count_right + $5
WHERE id = $6`,
[request.body.voteLeft, request.body.voteCenterLeft, request.body.voteCenter, request.body.voteCenterRight, request.body.voteRight, request.body.sourceId],
function (err) {
if (err) console.error(err);
response.send('update complete');
});
});
});
function loadDB() {
client.query(`
CREATE TABLE IF NOT EXISTS
sources (
id int PRIMARY KEY NOT NULL,
name varchar(100) NOT NULL,
count_left int DEFAULT 0,
count_center_left int DEFAULT 0,
count_center int DEFAULT 0,
count_center_right int DEFAULT 0,
count_right int DEFAULT 0);`)
.then(loadSourceData)
.catch(console.error);
client.query(`
CREATE TABLE IF NOT EXISTS
articles (
title varchar(500) PRIMARY KEY NOT NULL,
description varchar(500) NOT NULL,
url varchar(500) NOT NULL,
source_id int references sources(id) NOT NULL,
author varchar(555) NOT NULL,
url_to_image varchar(500) NOT NULL,
published_at timestamp);`)
.catch(console.error);
}
function loadSourceData() {
client.query(`
INSERT INTO sources VALUES
(1, 'the-huffington-post',0,0,0,0,0),
(2, 'the-new-york-times',0,0,0,0,0),
(3, 'usa-today',0,0,0,0,0),
(4, 'daily-mail',0,0,0,0,0),
(5, 'the-wall-street-journal',0,0,0,0,0)
ON CONFLICT DO NOTHING;`);
}
app.get('/sources', function (request, response) {
client.query('SELECT * FROM sources;')
.then(result => {
response.send(result.rows);
})
.catch(err => console.error(err));
});
app.get('*', (request, response) => response.sendFile('index.html', { root: './public' }));
app.listen(PORT, function () {
console.log('Your port is running on:', PORT);
});