-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
157 lines (133 loc) · 5.42 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
require('dotenv').config();
const fetch = require('node-fetch');
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const app = express();
const superagent = require('superagent');
const PORT = process.env.PORT || 3001;
const apiKey = process.env.API_KEY;
const client = require('./lib/client');
const ensureAuth = require('./lib/auth/ensure-auth');
const createAuthRoutes = require('./lib/auth/create-auth-routes');
const authRoutes = createAuthRoutes({
selectUser(email) {
return client.query(`
SELECT *
FROM users
WHERE email = $1;
`,
[email]
).then(result => result.rows[0]);
},
insertUser(user, hash) {
return client.query(`
INSERT into users (email, display_name, hash)
VALUES ($1, $2, $3)
RETURNING id, email, display_name as "displayName";
`,
[user.email, user.displayName, hash]
).then(result => result.rows[0]);
}
});
app.use(morgan('dev')); // http logging
app.use(cors()); // enable CORS request
app.use(express.static('public')); // server files from /public folder
app.use(express.json()); // enable reading incoming json data
app.use('/api/auth', authRoutes); // setup authentication routes
app.use('/api', ensureAuth); // everything that starts with "/api" below here requires an auth token!
app.post('/api/test', (req, res) => {
res.json({
message: `the user's id is ${req.userId}`
});
});
app.post('/api/candidates', async(request, response) => {
const year = request.body.year;
const yearInt = parseInt(year);
const candidateNamesURL = `https://api.open.fec.gov/v1/elections/?sort_null_only=true&page=1&election_full=true&sort_nulls_last=true&sort=-total_receipts&cycle=${yearInt}&sort_hide_null=true&office=president&api_key=${apiKey}&per_page=20`;
const candidateNames = await superagent.get(candidateNamesURL);
const actualCandidateNames = JSON.parse(candidateNames.text);
response.json(actualCandidateNames);
});
const nameData = async url => {
try {
const response = await fetch(url);
const json = await response.json();
return json;
}
catch (error) {
console.log(error);
}
};
app.post('/api/candidate-cash', async(request, response) => {
const year = request.body.year;
const yearInt = parseInt(year);
const candidateNamesURL = `https://api.open.fec.gov/v1/elections/?sort_null_only=true&page=1&election_full=true&sort_nulls_last=true&sort=-total_receipts&cycle=${yearInt}&sort_hide_null=true&office=president&api_key=${apiKey}&per_page=20`;
const candidateNames = await nameData(candidateNamesURL);
const candidateIdArray = candidateNames.results.map(({ candidate_id }) => candidate_id);
const candidateCashURL = `https://api.open.fec.gov/v1/schedules/schedule_a/by_size/by_candidate/?per_page=100&sort_hide_null=false&sort=size&sort_nulls_last=false&election_full=true&page=1&candidate_id=${candidateIdArray[0]}&candidate_id=${candidateIdArray[1]}&candidate_id=${candidateIdArray[2]}&candidate_id=${candidateIdArray[3]}&candidate_id=${candidateIdArray[4]}&candidate_id=${candidateIdArray[5]}&candidate_id=${candidateIdArray[6]}&candidate_id=${candidateIdArray[7]}&candidate_id=${candidateIdArray[8]}&candidate_id=${candidateIdArray[9]}&candidate_id=${candidateIdArray[10]}&candidate_id=${candidateIdArray[11]}&candidate_id=${candidateIdArray[12]}&candidate_id=${candidateIdArray[13]}&candidate_id=${candidateIdArray[14]}&candidate_id=${candidateIdArray[15]}&candidate_id=${candidateIdArray[16]}&candidate_id=${candidateIdArray[17]}&candidate_id=${candidateIdArray[18]}&candidate_id=${candidateIdArray[19]}&api_key=${apiKey}&sort_null_only=false&cycle=${yearInt}`;
const candidateCashData = await fetch(candidateCashURL);
const actualCandidateCashData = await candidateCashData.json();
response.json(actualCandidateCashData);
});
app.get('/api/favorites', async(req, res) => {
const userId = req.userId;
try {
const result = await client.query(`
SELECT * from favorites
WHERE user_id = $1;
`,
[userId]
);
res.json(result.rows);
}
catch (err) {
console.log(err);
res.status(500).json({
error: err.message || err
});
}
});
app.post('/api/favorites', async(req, res) => {
const candidateId = req.body;
const userId = req.userId;
try {
const result = await client.query(`
INSERT into favorites (candidate_id, user_id)
VALUES ($1, $2)
RETURNING *;
`,
[candidateId.candidate_id, userId]
);
res.json(result.rows[0]);
}
catch (err) {
console.log(err);
res.status(500).json({
error: err.message || err
});
}
});
app.delete('/api/favorites', async(req, res) => {
const candidateId = req.body;
const userId = req.userId;
try {
console.log(req.body, userId);
await client.query(`
DELETE from favorites
WHERE candidate_id = $1 AND user_id = $2;
`,
[candidateId.candidate_id, userId]
);
res.json({ success: true });
}
catch (err) {
console.log(err);
res.status(500).json({
error: err.message || err
});
}
});
app.listen(PORT, () => {
console.log('server running on PORT', PORT);
});