Last active
August 18, 2021 00:55
-
-
Save phpmaps/0ef4dfcda54235d41406f736b82119b4 to your computer and use it in GitHub Desktop.
favorites data management - web service
This file contains hidden or 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
| const express = require('express'); | |
| const favoriteRouter = express.Router(); | |
| const authenticate = require('../authenticate'); | |
| const Favorite = require('../models/favorite'); | |
| const cors = require('./cors'); | |
| favoriteRouter.route('/') | |
| .options(cors.corsWithOptions, (req, res) => res.sendStatus(200)) | |
| .get(cors.cors, authenticate.verifyUser, (req, res, next) => { | |
| Favorite.find({ user: req.user._id }) | |
| .populate('user') //Adds fully hydrated user object | |
| .populate('campsites') //Adds fully hydrated campsite object | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }) | |
| .catch(err => next(err)); | |
| }) | |
| .post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => { | |
| // Is there a favorite document for this user in the collection | |
| Favorite.findOne({ user: req.user._id }) | |
| .then(favorite => { | |
| if (favorite) { //Yes the favorite document is in the collection... | |
| req.body.forEach(fav => { | |
| // Does the campsite not exist in the users favorites | |
| if (!favorite.campsites.includes(fav._id)) { | |
| favorite.campsites.push(fav._id); // no... so add the campsite to his/her favorites | |
| } | |
| }); | |
| favorite.save() | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }) | |
| .catch(err => next(err)); | |
| } else { //The favorite document is not in the campsites array | |
| Favorite.create({ user: req.user._id }) | |
| .then(favorite => { | |
| req.body.forEach(fav => { | |
| if (!favorite.campsites.includes(fav._id)) { | |
| favorite.campsites.push(fav._id); | |
| } | |
| }); | |
| favorite.save() | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }) | |
| .catch(err => next(err)); | |
| }) | |
| .catch(err => next(err)); | |
| } | |
| }).catch(err => next(err)); | |
| }) | |
| .put(cors.corsWithOptions, authenticate.verifyUser, (req, res) => { | |
| res.statusCode = 403; | |
| res.end('PUT operation not supported on /favorites'); | |
| }) | |
| .delete(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => { | |
| Favorite.findOneAndDelete({ user: req.user._id }) | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| if (favorite) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| } else { | |
| res.setHeader('Content-Type', 'text/plain'); | |
| res.end('You do not have any favorites to delete.'); | |
| } | |
| }) | |
| .catch(err => next(err)); | |
| }); | |
| favoriteRouter.route('/:campsiteId') | |
| .options(cors.corsWithOptions, (req, res) => res.sendStatus(200)) | |
| .get(cors.cors, authenticate.verifyUser, (req, res) => { | |
| res.statusCode = 403; | |
| res.end(`GET operation not supported on /favorites/${req.params.campsiteId}`); | |
| }) | |
| .post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => { | |
| Favorite.findOne({user: req.user._id}) | |
| .then(favorite => { | |
| if (favorite) { | |
| if (!favorite.campsites.includes(req.params.campsiteId)) { | |
| favorite.campsites.push(req.params.campsiteId); | |
| favorite.save() | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }) | |
| .catch(err => next(err)); | |
| } else { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'text/plain'); | |
| res.end('That campsite is already a favorite!'); | |
| } | |
| } else { | |
| Favorite.create({ user: req.user._id, campsites: [req.params.campsiteId] }) | |
| .then(favorite => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }) | |
| .catch(err => next(err)); | |
| } | |
| }) | |
| .catch(err => next(err)); | |
| }) | |
| .put(cors.corsWithOptions, authenticate.verifyUser, (req, res) => { | |
| res.statusCode = 403; | |
| res.end(`PUT operation not supported on /favorites/${req.params.campsiteId}`); | |
| }) | |
| .delete(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => { | |
| Favorite.findOne({user: req.user._id }) | |
| .then(favorite => { | |
| if (favorite) { | |
| const index = favorite.campsites.indexOf(req.params.campsiteId); | |
| if (index >= 0) { | |
| //To delete favorite, splice(start, deleteCount) | |
| favorite.campsites.splice(index, 1); | |
| } | |
| /* can alternatively use filter as below, instead of using indexOf/splice, | |
| but make sure to use loose inequality OR do fav.toString() before comparing: */ | |
| //favorite.campsites = favorite.campsites.filter(fav => fav.toString() !== req.params.campsiteId); | |
| favorite.save() | |
| .then(favorite => { | |
| console.log('Favorite Campsite Deleted!', favorite); | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.json(favorite); | |
| }).catch(err => next(err)); | |
| } else { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'text/plain'); | |
| res.end('You do not have any favorites to delete.'); | |
| } | |
| }).catch(err => next(err)) | |
| }); | |
| module.exports = favoriteRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment