Skip to content

Commit

Permalink
make some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadAkthamObeidat committed Feb 2, 2020
1 parent 6f54a52 commit 02b66e6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 137 deletions.
5 changes: 3 additions & 2 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const logout = (req, res, next) => {

// Protect the routes that only available for authenticated users.
const protectRoutes = catchAsync(async (req, res, next) => {
console.log('REQ.HEADERS.AUTHORIZATION :', req.headers);
// 1) Get the token and check if it's there.
let token;
if (
Expand All @@ -97,7 +98,7 @@ const protectRoutes = catchAsync(async (req, res, next) => {
) {
token = req.headers.authorization.split(' ')[1];
}

console.log('TOKEN', token);
if (!token) {
return next(
new AppError('Your not logged in!! please login to get access'),
Expand All @@ -106,7 +107,7 @@ const protectRoutes = catchAsync(async (req, res, next) => {
}
// 2) Token verification if token payload has not manipulated by malicious 3rd party.
const decoded = await jwt.verify(token, process.env.JWT_SECRET);

console.log('decoded :', decoded);
// 3) Check if user still exists.
const currentUser = await user.getCurrentUser(decoded.id);
if (!currentUser) {
Expand Down
7 changes: 5 additions & 2 deletions controllers/UsersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ app.use(express.json());
// Add new movie to watch list.
const addMovieToWatchList = (req, res, next) => {
const { userID } = req.params;
console.log('req.body', req.body)
user.addMovieToWatchList(userID, req.body, result => {
res.status(200).json({
status: 'Success Adding Movie To Watchlist.',
Expand All @@ -22,8 +23,10 @@ const addMovieToWatchList = (req, res, next) => {
// Delete movie from watch list.
const deleteMovieFromWatchList = (req, res, next) => {
const { userID } = req.params;
console.log('REQ.BODY', req.body);
user.deleteMovieFromWatchList(userID, req.body, result => {
const { movieID } = req.params;
console.log('userID :', userID);
console.log('movieID :', movieID);
user.deleteMovieFromWatchList(userID, movieID, result => {
res.status(200).json({
status: 'Success Delete Movie From Watch List.',
// eslint-disable-next-line no-undef
Expand Down
58 changes: 33 additions & 25 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ const userSchema = new mongoose.Schema({
type: String,
required: false
},
movies_list: {
watch_list: [],
watched_list: []
movies_watch_list: {
type: Array,
required: false
},
movies_watched_list: {
type: Array,
required: false
},
shows_list: {
watch_list: [],
watched_list: []
shows_watch_list: {
type: Array,
required: false
},
shows_watched_list: {
type: Array,
required: false
}
});

Expand Down Expand Up @@ -122,13 +130,14 @@ const addUser = (newUser, callback) => {

// Add new movie to watch list.
const addMovieToWatchList = (userID, newMovie, callback) => {
console.log('newMovie :', newMovie);
User.findOneAndUpdate(
{
_id: userID
},
{
$push: {
'movies_list.watch_list': newMovie
movies_watch_list: newMovie
}
},
(error, data) => {
Expand All @@ -142,14 +151,13 @@ const addMovieToWatchList = (userID, newMovie, callback) => {
};

// Delete movie from watch list.
const deleteMovieFromWatchList = (userID, newWatchlist, callback) => {
console.log('NEW WATCHLIST :', newWatchlist);
User.update(
const deleteMovieFromWatchList = (userID, movieID, callback) => {
console.log('USER ID :', userID);
console.log('MOVIE ID:', movieID);
User.updateOne(
{ _id: userID },
{
$pull: {
'movies_list.watch_list': newWatchlist
}
$pull: { movies_watch_list: { id: movieID } }
},
(error, data) => {
if (error) {
Expand All @@ -169,7 +177,7 @@ const addMovieToWatchedList = (userID, newMovie, callback) => {
},
{
$push: {
'movies_list.watched_list': newMovie
movies_watched_list: newMovie
}
},
(error, data) => {
Expand All @@ -184,13 +192,13 @@ const addMovieToWatchedList = (userID, newMovie, callback) => {

// Delete movie from watched list.
const deleteMovieFromWatchedList = (userID, newWatchedlist, callback) => {
User.update(
User.findOneAndUpdate(
{
_id: userID
},
{
$set: {
'movies_list.watched_list': newWatchedlist
$pull: {
movies_watched_list: { id: newWatchedlist.id }
}
},
(error,
Expand All @@ -214,7 +222,7 @@ const addShowToWatchList = (userID, newShow, callback) => {
},
{
$push: {
'shows_list.watch_list': newShow
shows_watch_list: newShow
}
},
(error, data) => {
Expand All @@ -229,13 +237,13 @@ const addShowToWatchList = (userID, newShow, callback) => {

// Delete show from watch list.
const deleteShowFromWatchList = (userID, newWatchlist, callback) => {
User.update(
User.findOneAndUpdate(
{
_id: userID
},
{
$set: {
'shows_list.watch_list': newWatchlist
$pull: {
shows_watch_list: { id: newWatchlist.id }
}
},
(error,
Expand All @@ -257,7 +265,7 @@ const addShowToWatchedList = (userID, newShow, callback) => {
},
{
$push: {
'shows_list.watched_list': newShow
shows_watched_list: newShow
}
},
(error, data) => {
Expand All @@ -272,13 +280,13 @@ const addShowToWatchedList = (userID, newShow, callback) => {

// Delete show from watched list.
const deleteShowFromWatchedList = (userID, newWatchedlist, callback) => {
User.update(
User.findOneAndUpdate(
{
_id: userID
},
{
$set: {
'shows_list.watched_list': newWatchedlist
$pull: {
shows_watched_list: { id: newWatchedlist.id }
}
},
(error,
Expand Down
18 changes: 9 additions & 9 deletions routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ const authController = require('../controllers/AuthController');

// MOVIES ROUTES. **************************************************************

// @PUT
// @PATCH
// Add movie to watch list.
router
.route('/movie/add/watchlist/:userID')
.patch(authController.protectRoutes, userController.addMovieToWatchList);
// @DELETE
// @PATCH
// Remove movie from watch list.
router
.route('/movie/delete/watchlist/:userID')
.route('/movie/delete/watchlist/:userID/:movieID')
.patch(
authController.protectRoutes,
userController.deleteMovieFromWatchList
);
// @PUT
// @PATCH
// Add movie to watched list.
router
.route('/movie/add/watchedlist/:userID')
.patch(authController.protectRoutes, userController.addMovieToWatchedList);
// @DELETE
// @PATCH
// Remove movie from watched list.
router
.route('/movie/delete/watchedlist/:userID')
Expand All @@ -34,25 +34,25 @@ router
);

// TV SHOW ROUTES. **************************************************************
// @PUT
// @PATCH
// Add TvShow to watch list.
router
.route('/show/add/watchlist/:userID')
.patch(authController.protectRoutes, userController.addShowToWatchList);
// @DELETE
// @PATCH
// Remove TvShow from watch list.
router
.route('/show/delete/watchlist/:userID')
.patch(
authController.protectRoutes,
userController.deleteShowFromWatchList
);
// @PUT
// @PATCH
// Add TvShow to watched list.
router
.route('/show/add/watchedlist/:userID')
.patch(authController.protectRoutes, userController.addShowToWatchedList);
// @DELETE
// @PATCH
// Remove TvShow from watched list.
router
.route('/show/add/watchedlist/:userID')
Expand Down
61 changes: 35 additions & 26 deletions view/src/Layouts/DiscoverMovies.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ class DiscoverMovies extends Component {
const loadedMovie = await axios.get(`/movies/details/${id}`);
const { movieDetails } = loadedMovie.data.data;

this.state.user.movies_list.watch_list.map(movie => {
if (movie.id === id) {
return this.setState({
isMovieExistInWatchlist: true
});
} else {
return this.setState({
isMovieExistInWatchlist: false
});
}
});
let isExist;
if (this.state.user.movies_watch_list.length === 0) {
isExist = false;
} else {
await this.state.user.movies_watch_list.forEach(movie => {
if (movie.id === id) {
isExist = true;
} else {
isExist = false;
}
});
}

console.log('isExist :', isExist);
console.log('isExist :', !isExist);

// Add Movie To Authenticated User Watchlist.
if (movieDetails && this.state.isMovieExistInWatchlist === false) {
if (movieDetails && isExist === false) {
const addedMovie = await axios.patch(
`/user/movie/add/watchlist/${this.state.user._id}`,
movieDetails,
Expand All @@ -60,9 +64,10 @@ class DiscoverMovies extends Component {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const token = Auth.getToken();
const { watch_list } = this.state.user.movies_list;
const newWatchlist = watch_list.filter(element => element.id !== id);
console.log('WATCH_LIST :', watch_list);
const { movies_watch_list } = this.state.user;
const newWatchlist = movies_watch_list.filter(
element => element.id !== id
);

axios.patch(
`/user/movie/delete/watchlist/${this.state.user._id}`,
Expand All @@ -86,16 +91,21 @@ class DiscoverMovies extends Component {
const loadedMovie = await axios.get(`/movies/details/${id}`);
const { movieDetails } = loadedMovie.data.data;

this.state.user.movies_list.watched_list.map(movie => {
if (movie.id === id) {
return this.setState({ isMovieExistInWatchedlist: true });
} else {
return this.setState({ isMovieExistInWatchedlist: false });
}
});
let isExist;
if (this.state.user.movies_watched_list.length === 0) {
isExist = false;
} else {
await this.state.user.movies_watched_list.forEach(movie => {
if (movie.id === id) {
isExist = true;
} else {
isExist = false;
}
});
}

// Add Movie To Authenticated User Watchlist.
if (movieDetails && this.state.isMovieExistInWatchedlist === false) {
if (movieDetails && isExist === false) {
const addedMovie = await axios.patch(
`/user/movie/add/watchedlist/${this.state.user._id}`,
movieDetails,
Expand All @@ -117,11 +127,10 @@ class DiscoverMovies extends Component {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const token = Auth.getToken();
const { watched_list } = this.state.user.movies_list;
const newWatchedList = watched_list.filter(
const { movies_watched_list } = this.state.user;
const newWatchedList = movies_watched_list.filter(
element => element.id !== id
);
console.log('WATCHED_LIST :', watched_list);

axios.patch(
`/movie/delete/watchedlist/${this.state.user._id}`,
Expand Down
Loading

0 comments on commit 02b66e6

Please sign in to comment.