Created
June 5, 2022 09:13
-
-
Save nicovray/d71e31ce04a70ec7b4b3c6f2c8798aa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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