This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
117 lines (106 loc) · 3.2 KB
/
server.js
File metadata and controls
117 lines (106 loc) · 3.2 KB
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
const express = require('express');
const cookieParser = require('cookie-parser');
const path = require('path');
const { UserModel, connect } = require('./database');
const {
getUser,
searchAlbums,
getSpotify,
spotifyLogin,
saveTokens,
getTokens,
getRecommendations
} = require('./spotify');
const app = express();
app.use(express.json());
app.use(cookieParser());
// Serve the static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));
// redirect to Spotify login
app.get('/login', (req, res) => {
console.log('Connecting to Spotify');
res.redirect(spotifyLogin(req));
});
// handle Spotify auth callback
app.get('/spotify', async (req, res) => {
console.log('Spotify authorized');
const { access_token, refresh_token } = await getTokens(req);
const {
body: { id }
} = await getSpotify(access_token).getMe();
res.cookie('accessToken', access_token);
res.redirect('/');
await saveTokens(id, access_token, refresh_token);
});
// get a user's favorite albums
app.get('/api/favorites', async (req, res) => {
const user = await getUser(req, res);
const favorites = user.toObject().favorites.map(album => ({
...album,
favorite: true
}));
res.json(favorites);
});
// change a user's favorite albums
app.post('/api/favorite', async (req, res) => {
const user = await getUser(req, res);
const { newAlbum } = req.body;
// add or remove from favorites
user.favorites = newAlbum.favorite
? [newAlbum, ...user.favorites]
: user.favorites.filter(({ id }) => id !== newAlbum.id);
user.save();
res.end();
});
// search Spotify and add user's favorites
app.post('/api/query', async (req, res) => {
const user = await getUser(req, res);
const favoriteIds = user.favorites.map(({ id }) => id);
const { query } = req.body;
const albums = await searchAlbums(query, user.accessToken, res);
const albumsWithFavorite = albums.map(album => ({
...album,
favorite: favoriteIds.includes(album.id)
}));
res.json(albumsWithFavorite);
});
// recommend albums based on user's favorites
app.get('/api/recommendations', async (req, res) => {
console.log('recommending albums');
const user = await getUser(req, res);
// get recommendations
const recs = await getRecommendations(
user.toObject().favorites,
user.accessToken,
res
);
// filter out favorites
const favoriteIds = user.favorites.map(({ id }) => id);
const recsWithoutFavs = recs.filter(({ id }) => !favoriteIds.includes(id));
// filter out hidden
const hiddenIds = user.hidden.map(({ id }) => id);
const recsWithoutHidden = recsWithoutFavs.filter(
({ id }) => !hiddenIds.includes(id)
);
res.json(recsWithoutHidden);
});
// hide album recommendations for a user
app.post('/api/hide', async (req, res) => {
console.log('hiding album recommendation');
const user = await getUser(req, res);
const { album } = req.body;
// add to hidden list
user.hidden = [album, ...user.hidden];
user.save();
res.end();
});
// Handles any requests that don't match the ones above
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/client/build/index.html'));
});
// connect to DB then start server
connect(() => {
const port = process.env.PORT || 8080;
app.listen(port);
console.log(`listening at http://localhost:${port}`);
});