Skip to content

Instantly share code, notes, and snippets.

@nicovray
Created June 5, 2022 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicovray/d71e31ce04a70ec7b4b3c6f2c8798aa0 to your computer and use it in GitHub Desktop.
Save nicovray/d71e31ce04a70ec7b4b3c6f2c8798aa0 to your computer and use it in GitHub Desktop.
moviesRouter.get('/', (req, res) => {
const { user_token } = req.cookies;
User.findByToken(user_token).then((user) => {
User.movies(user.id).then((movies) => {
res.send(movies)
}).catch(() => res.status(500).send('Error'))
}).catch(()=>res.status(401).send('Unauthorized access'))
});
moviesRouter.post('/', (req, res) => {
User.findByToken(req.cookies['user_token'])
.then((user) => {
const error = Movie.validate(req.body);
if (error) {
res.status(422).json({ validationErrors: error.details });
}
else {
Movie.create({ ...req.body, user_id: user.id })
.then((createdMovie) => {
res.status(201).json(createdMovie);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error saving the movie');
});
}
}
)
.catch(() => { res.status(401).send('Unauthorized user'); })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment