Skip to content

Commit

Permalink
SERVER4: Security and Authentication
Browse files Browse the repository at this point in the history
- Allow user to manage  their own favorite dishes
  • Loading branch information
yogykwan committed May 29, 2017
1 parent 4108c9f commit 986419e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 2 additions & 0 deletions conFusion-Express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var users = require('./routes/users');
var dishRouter = require('./routes/dishRouter');
var promoRouter = require('./routes/promoRouter');
var leaderRouter = require('./routes/leaderRouter');
var favoriteRouter = require('./routes/favoriteRouter');

var app = express();

Expand Down Expand Up @@ -55,6 +56,7 @@ app.use('/users', users);
app.use('/dishes', dishRouter);
app.use('/promotions', promoRouter);
app.use('/leadership', leaderRouter);
app.use('/favorites', favoriteRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
Expand Down
19 changes: 19 additions & 0 deletions conFusion-Express/models/favorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var favoriteSchema = new Schema({
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
dishes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Dish'
}]
}, {
timestamps: true
});

var Favorites = mongoose.model('Favorite', favoriteSchema);

module.exports = Favorites;
2 changes: 1 addition & 1 deletion conFusion-Express/routes/dishRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var dishRouter = express.Router();
dishRouter.use(bodyParser.json());

dishRouter.route('/')
.all()
.all(Verify.verifyOrdinaryUser)

.get(function (req, res, next) {
Dishes.find({})
Expand Down
76 changes: 76 additions & 0 deletions conFusion-Express/routes/favoriteRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var Favorites = require('../models/favorites');
var Verify = require('./verify');

var favoriteRouter = express.Router();
favoriteRouter.use(bodyParser.json());

favoriteRouter.route('/')
.all(Verify.verifyOrdinaryUser)

.get(function (req, res, next) {
Favorites.findOne({customer: req.decoded._doc._id})
.populate(['customer', 'dishes'])
.exec(function (err, favorite) {
if (err) throw err;
res.json(favorite);
});
})

.post(function (req, res) {
Favorites.findOne({customer: req.decoded._doc._id}, function (err, favorite) {
if (!favorite) {
favorite = new Favorites({
customer: req.decoded._doc._id
});
}
if (favorite.dishes.indexOf(req.body._id) === -1) {
favorite.dishes.push(req.body);
}
favorite.save(function (err, favorite) {
if (err) throw err;
res.json(favorite);
});
});
})

.delete(function (req, res) {
Favorites.findOne({customer: req.decoded._doc._id}, function (err, favorite) {
if (err) throw err;
if (favorite) {
Favorites.findByIdAndRemove(favorite._id, function (err, resp) {
if (err) throw err;
res.json(resp);
});
}
});
});

favoriteRouter.route('/:favoriteId')
.all(Verify.verifyOrdinaryUser)

.delete(function (req, res) {
Favorites.findOne({customer: req.decoded._doc._id}, function (err, favorite) {
if (err) throw err;
favorite.dishes = favorite.dishes.filter(function (dish) {
return dish != req.params.favoriteId; // typeof(dish)=object, typeof(id)=string
});

if (favorite.dishes.length > 0) {
favorite.save(function (err, resp) {
if (err) throw err;
res.json(resp);
});
} else {
Favorites.findByIdAndRemove(favorite._id, function (err, resp) {
if (err) throw err;
res.json(resp);
});
}
});
});

module.exports = favoriteRouter;

0 comments on commit 986419e

Please sign in to comment.