-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
116 lines (95 loc) · 3.2 KB
/
app.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
// I'm grading off the dev branch here. Is that right? Is dev the most updated code? The master branch didn't seem like it
const express = require('express');
const cors = require('cors');
const client = require('./client.js');
const app = express();
const request = require('superagent');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const ensureAuth = require('./auth/ensure-auth');
const createAuthRoutes = require('./auth/create-auth-routes');
const authRoutes = createAuthRoutes({
selectUser(email) {
return client.query(`
SELECT id, email, hash
FROM users
WHERE email = $1;
`,
[email]
).then(result => result.rows[0]);
},
insertUser(user, hash) {
return client.query(`
INSERT into users (email, hash)
VALUES ($1, $2)
RETURNING id, email;
`,
[user.email, hash]
).then(result => result.rows[0]);
}
});
app.use('/auth', authRoutes);
app.use('/api', ensureAuth);
app.get('/color', async(req, res) => {
// nice work getting this endpoint together so quickly!
const data = await request.get('http://thecolorapi.com/scheme?hex=0047AB&rgb=0,71,171&hsl=215,100%,34%&cmyk=100,58,0,33&format=json&mode=analogic&count=6');
res.json(data.body);
});
app.get('/locations', async(req, res) => {
const data = await client.query('SELECT * from locations');
res.json(data.rows);
});
app.get('/events/:eventId', async(req, res) => {
const data = await client.query(`
select * from events where events.planet_id = $1
`, [req.params.eventId]);
res.json(data.rows);
});
app.get('/shipchoices', async(req, res) => {
const data = await client.query('SELECT * from shipchoices');
res.json(data.rows);
});
app.get('/usership/:id', async(req, res) => {
const data = await client.query(`
SELECT shipchoices.id, shipchoices.ship_name, shipchoices.ship_fuel, shipchoices.ship_hull, shipchoices.base_combat, shipchoices.base_diplomacy, shipchoices.base_science, shipchoices.used_item_slots, shipchoices.max_item_slots
FROM shipchoices
WHERE shipchoices.id = $1
`, [req.params.id]);
res.json(data.rows);
});
app.get('/randomPlanet', async(req, res) => {
const data = await client.query(`
SELECT locations.id, locations.location_name, locations.location_type, locations.location_image, locations.location_description, events
FROM locations
JOIN events
on locations.event_id = events.planet_id
`,);
res.json(data.rows);
});
app.post('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
INSERT INTO loggedinuser (name, score, owner_id)
values ($1, $2, $3)
returning *
`, [req.body.name, req.body.score, req.userId]);
res.json(data.rows);
});
app.get('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
SELECT * FROM loggedinuser
WHERE owner_id = $1
`, [req.userId]);
res.json(data.rows);
});
// seems like you could have managed this stuff with req.userId
app.put('/api/loggedinuser', async(req, res) => {
const data = await client.query(`
UPDATE loggedinuser
SET score = score + 1
WHERE owner_id=$1
`, [req.userId]);
res.json(data.rows);
});
app.use(require('./middleware/error'));
module.exports = app;