-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibmodifying.js
146 lines (127 loc) · 4.88 KB
/
libmodifying.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
const router = require('express').Router();
let User = require('../models/user.model');
let Movie = require('../models/movie.model');
const jwt = require('jsonwebtoken');
const protect = require('./protect');
router.route('/:section/checkstate').post(protect, (req, res, next) => {
const userID = req.user._id;
const movieID = req.body.movieID;
const section = req.params.section;
let query;
if (section === "towatch") {
query = { _id: userID, 'library.toWatch.movieID': movieID };
} else if (section === 'favourites') {
query = { _id: userID, 'library.favourites.movieID': movieID };
} else if (section === 'seen') {
query = { _id: userID, 'library.seen.movieID': movieID };
}
User.countDocuments(query)
.then((count) => {
if (count) res.json({ found: true });
else res.json({ found: false });
})
.catch(err => console.log(err))
})
router.route('/:section/add').post(protect, (req, res, next) => {
const userID = req.user._id;
const movieID = req.body.movieID;
const title = req.body.title;
const section = req.params.section;
let query, toPush;
if (section === "towatch") {
query = { _id: userID, 'library.toWatch.movieID': { $nin: movieID } };
toPush = { "library.toWatch": { "movieID": movieID, "title": title } };
} else if (section === 'favourites') {
query = { _id: userID, 'library.favourites.movieID': { $nin: movieID } };
toPush = { "library.favourites": { "movieID": movieID, "title": title } };
} else if (section === 'seen') {
query = { _id: userID, 'library.seen.movieID': { $nin: movieID } };
toPush = { "library.seen": { "movieID": movieID, "title": title } };
}
Movie.countDocuments({ id: movieID }, (err, count) => {
if (err || count <= 0) {
res.status(401).json('Error no movie with that id')
return;
}
User.findOneAndUpdate(query, { "$push": toPush }, (err, doc) => {
if (doc != null) {
res.json("Added")
}
else {
res.json("Error: Already in lib")
}
})
})
})
router.route('/:section/remove').post(protect, (req, res, next) => {
const userID = req.user._id
const movieID = req.body.movieID
const title = req.body.title
const section = req.params.section;
let query, toPull;
if (section === "towatch") {
query = { _id: userID, 'library.toWatch.movieID': { $in: movieID } };
toPull = { "library.toWatch": { "movieID": movieID, "title": title } };
} else if (section === 'favourites') {
query = { _id: userID, 'library.favourites.movieID': { $in: movieID } };
toPull = { "library.favourites": { "movieID": movieID, "title": title } };
} else if (section === 'seen') {
query = { _id: userID, 'library.seen.movieID': { $in: movieID } };
toPull = { "library.seen": { "movieID": movieID, "title": title } };
}
Movie.countDocuments({ id: movieID }, (err, count) => {
if (err || count <= 0) {
res.status(401).json('Error no movie with that id')
return;
}
User.findOneAndUpdate(query, { "$pull": toPull }, (err, doc) => {
if (doc != null) {
res.json("Removed")
}
else {
res.json("Nothing to remove")
}
})
})
})
router.route('/seen/rate/add').post(protect, (req, res, next) => {
const userID = req.user._id
const movieID = req.body.movieID
const rate = req.body.rate
Movie.countDocuments({ id: movieID }, (err, count) => {
if (err || count <= 0) {
res.status(401).json('Error no movie with that id')
return;
}
User.findOneAndUpdate({ _id: userID, "library.seen.movieID": { $in: movieID } }, { $set: { "library.seen.$.rate": rate } }, { runValidators: true }, (err, doc) => {
if (doc != null) {
res.json("Rate updated")
}
else {
res.json("Nothing to update")
}
})
})
})
router.route('/seen/rate/value').post(protect, (req, res, next) => {
const userID = req.user._id;
const movieID = req.body.movieID;
Movie.countDocuments({ id: movieID }, (err, count) => {
if (err || count <= 0) {
res.status(401).json('Error no movie with that id')
return;
}
User.findOne({ _id: userID, "library.seen.movieID": movieID }, { "library.seen": 1 }, (err, doc) => {
if (doc != null) {
for (let m of doc.library.seen) {
if (m.movieID === movieID) {
res.json({ rate: m.rate });
return;
}
}
}
res.json({ rate: 0 });
})
})
})
module.exports = router;