-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
221 lines (198 loc) · 6.84 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// ... other imports
const path = require("path")
// ... other app.use middleware
app.use(express.static(path.join(__dirname, "client", "build")))
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt');
// const request = require('request')
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/music_player', {useNewUrlParser: true});
//mongodb:// localhost:27017/hashAppDb"
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("successfully conencted with mongodb!");
});
var UserSchema = new mongoose.Schema({
username: { type: String, unique: true, required: true, trim: true },
email: { type: String, unique: true, required: true, trim: true },
password: { type: String, required: true },
video_list: [
{
video_id: String,
liked: { type: Boolean, default:false }
//likes: { type: Int8Array, default: 0 }
}
],
})
// var VideoSchema = new mongoose.Schema({
// id: { type:mongoose.Schema.Types.ObjectId, index: true, unique:true, required: true, auto:true },
// username: { type: String, unique: true, required: true, trim: true },
// video_list: { type: Array, unique: true},
// })
// schema -> model
var User = mongoose.model('User', UserSchema);
// var Videos = mongoose.model('Videos', VideoSchema);
module.exports = User;
// module.exports = Videos;
app.get('/v1/hello', (req, res) => {
let password = 'Cd18712312';
res.send({
express: 'Hello From Express',
raw_password: password,
});
});
// {
// email: ''
// }
app.get('/v1/users/:username', (req, res) => {
var username = req.params.username;
// console.log(username + ' hey got cha!');
var query = { username: username };
db.collection('users').find(query).toArray((err, result) => {
// console.log('user: ', result);
if (err) return console.error(err);
if (result.length === 1) {
var email = result[0].email;
res.send({ email: email }); // currently on email. playlists in the future
console.log("send email " + email + " to the front-end");
}
})
});
// {
// 'username':
// 'video_list': [{},{}...]
// }
app.get('/v1/users/:username/playlist', (req, res) => {
var username = req.params.username;
var query = { username: username };
db.collection('users').find(query).toArray((err, result) => {
if (err) return console.error(err);
if (result.length === 1) {
// console.log("result_0: ", result[0]);
// console.log("result_1: ", result[0].video_list);
res.send({
username: username,
video_list:result[0].video_list
});
}
})
})
app.get('/v1/users/:username/playlist/:video_id', (req, res) => {
var {username, video_id} = req.params;
if (username && video_id) {
db.collection('users').find({username:username}).toArray((err, result) => {
if (err) return console.error(err);
if (result.length === 1){
//console.log("send to front-end result: ", result[0]);
result[0].video_list.forEach((video) => {
if (video.video_id === video_id)
res.send({liked:video.liked});
console.log(video);
})
}});
}
})
app.post('/v1/users/:username/playlist/add', (req, res) => {
var username = req.params.username;
var added_video_id = req.body.add_video_id;
if (added_video_id) {
db.collection('users').findOneAndUpdate({username:username},
{$push: {video_list:{video_id: added_video_id, liked:false}}}, (err, result) => {
if (err) return console.error(err);
console.log("new video_id: ", added_video_id);
});
}
})
// talk with "ListItem.handleVideoLike()"
app.post('/v1/users/:username/playlist/:video_id/like', (req, res) => {
var username = req.params.username;
var like_video_id = req.params.video_id;
var index = req.body.index;
console.log("like video_id: ", like_video_id);
var key = `video_list.${index}.liked`;
if (username && like_video_id) {
db.users.update(
{ username:username },
{ $set: {"video_list.0.liked":true}}
)
console.log("yout just liked me!!!");
// db.collection('users').findOneAndUpdate({username:username},
// {$push: {favorite_list:{video_id: like_video_id}}}, (err, result) => {
// if (err) return console.error(err);
// console.log("like video_id: ", like_video_id);
// })
}
})
app.delete('/v1/users/:username/playlist/delete', (req, res) => {
var username = req.params.username;
var delete_video_id = req.body.delete_video_id;
if (delete_video_id) {
db.collection('users').findOneAndUpdate({username:username},
{$pull: {video_list:{video_id: delete_video_id}}}, (err, result) => {
if (err) return console.error(err);
console.log("deleted video_id: ", delete_video_id);
});
res.send({
username: username,
delete_video_id: delete_video_id,
})
}
})
app.post('/v1/login', (req, res) => {
// console.log(req.body);
var {username, password} = req.body;
if (username && password) {
var query = { username: username };
var db_password;
db.collection('users').find(query).toArray(function(err, result) {
if (err) return console.error(err);
if (result.length == 1)
db_password = result[0].password;
// compare hashed password
bcrypt.compare(password, db_password, function(err, result) {
if (result) {
// console.log("password matches!");
res.send({ match: true });
} else {
// console.log("please reenter again");
res.send({ match: false });
}
});
// error handling
// res.redirect('./hello');
})
}
})
app.post('/v1/register', (req, res) => {
console.log(req.body);
var {username, email, password} = req.body;
if (username && email && password) {
console.log("outer successfuly created!");
// hash password
bcrypt
.hash(password, 10)
.then(hash => {
var userData = new User({username:username, email:email, password:hash, video_list:[]});
console.log(`hashed password:${hash}`);
userData.save(function(err, user) {
if (err)
return console.error(err);
res.send(`Hello! you succeessfully registered with username:${username}, email:${email}, password:${password}`)
console.log(user.username, user.email, user.password);
})
})
.catch(err => console.log(err.message));
}
});
// ...
// Right before your app.listen(), add this:
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(port, () => console.log(`Listening on port ${port}`));