Created
June 15, 2016 09:25
-
-
Save miriame/a0059d2c5614194b1dcfb7717e693848 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
{ | |
"name": "uptown", | |
"dependencies": { | |
"express": "latest", | |
"lodash":"latest", | |
"mongoose": "4.5.0" | |
} | |
} |
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
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema; | |
/** | |
* Product Schema | |
*/ | |
var ProductSchema = new Schema({ | |
name: { | |
type: String, | |
required: true | |
}, | |
description: { | |
type: String, | |
required: true | |
}, | |
confirmed: { | |
type: Boolean, | |
default: false | |
}, | |
tags: [{ | |
type: String | |
}] | |
}); | |
/** | |
* Statics | |
*/ | |
ProductSchema.statics.load = function (id, cb) { | |
this.findOne({ | |
_id: id | |
}).exec(cb); | |
}; | |
mongoose.model('Product', ProductSchema); |
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
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose'), | |
Product = mongoose.model('Product'), | |
_ = require('lodash'); | |
/** | |
* Find product by id | |
*/ | |
exports.product = function (req, res) { | |
Product.load(req.params.id, function (err, product) { | |
if (err) return next(err); | |
if (!product) return next(new Error('Failed to load product ' + id)); | |
req.product = product; | |
next(); | |
}); | |
}; | |
/** | |
* Create an product | |
*/ | |
exports.create = function (req, res) { | |
var product = new Product(req.body); | |
product.save(function (err) { | |
if (err) { | |
return res.status(500).json({ | |
error: 'Cannot save the product' | |
}); | |
} | |
res.json(product); | |
}); | |
}; | |
/** | |
* Update an product | |
*/ | |
exports.update = function (req, res) { | |
var product = req.product; | |
product = _.extend(product, req.body); | |
product.save(function (err) { | |
if (err) { | |
return res.status(500).json({ | |
error: 'Cannot update the product' | |
}); | |
} | |
res.json(product); | |
}); | |
}; | |
/** | |
* Delete an product | |
*/ | |
exports.destroy = function (req, res) { | |
var product = req.product; | |
product.remove(function (err) { | |
if (err) { | |
return res.status(500).json({ | |
error: 'Cannot delete the product' | |
}); | |
} | |
res.json(product); | |
}); | |
}; | |
/** | |
* Show an product | |
*/ | |
exports.show = function (req, res) { | |
res.json(req.product); | |
}; | |
/** | |
* List of Products | |
*/ | |
exports.all = function (req, res) { | |
Product.find({},function (err, products) { | |
if (err) { | |
return res.status(500).json({ | |
error: 'Cannot list the products' | |
}); | |
} | |
res.json(products); | |
}); | |
}; | |
exports.confirm = function (req, res) { | |
var product = req.product; | |
product.confirmed = true; | |
product.save(function (err) { | |
if (err) { | |
return res.status(500).json({ | |
error: 'Cannot confirm the product' | |
}); | |
} | |
res.json(product); | |
}); | |
}; |
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
var express = require('express'); | |
var app = express(); | |
var products = require('./productsController'); | |
app.get('/products', products.all); | |
app.post('/products', products.create); | |
app.get('/products/:productId', products.show); | |
app.put('/products/:productId', products.update); | |
app.delete('/products/:productId', products.destroy); | |
app.post('/products/:productId/confirm', products.confirm); | |
app.param('productId', products.product); | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment