Skip to content

Commit

Permalink
Added controller for Product CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarkus committed Dec 5, 2013
1 parent db2b8fe commit f8e91c2
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions controllers/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* A very simple product editor
*/
'use strict';
var Product = require('../models/productModel');

module.exports = function (server) {

/**
* Retrieve a list of all products for editing.
*/
server.get('/products', function (req, res) {

Product.find(function (err, prods) {
if (err) {
console.log(err);
}

var model =
{
products: prods
};
res.render('products', model);
});

});


/**
* Add a new product to the database.
* **** PLEASE READ THE COMMENT BELOW! ****
*/
server.post('/products', function (req, res) {
var name = req.body.name && req.body.name.trim();

//***** PLEASE READ THIS COMMENT ******\\\
/*
Using floating point numbers to represent currency is a *BAD* idea \\
You should be using arbitrary precision libraries like:
https://github.com/justmoon/node-bignum instead.
So why am I not using it here? At the time of this writing, bignum is tricky to install
on Windows-based systems. I opted to make this example accessible to more people, instead
of making it mathematically correct.
I would strongly advise against using this code in production.
You've been warned!
*/
var price = parseFloat(req.body.price, 10);

//Some very lightweight input checking
if (name === '' || isNaN(price)) {
res.redirect('/products#BadInput');
return;
}

var newProduct = new Product({name: name, price: price});

//Show it in console for educational purposes...
newProduct.whatAmI();

newProduct.save();

res.redirect('/products');
});

/**
* Delete a product.
* @paaram: req.body.item_id Is the unique id of the product to remove.
*/
server.delete('/products', function (req, res) {
Product.remove({_id: req.body.item_id}, function (err) {
if (err) {
console.log('Remove error: ', err);
}
res.redirect('/products');
});
});


/**
* Edit a product.
* Not implemented here
*/
server.put('/products', function (req, res) {
console.log('PUT received. Ignoring.');
res.redirect('/products');
});

};

0 comments on commit f8e91c2

Please sign in to comment.