Implementing the Customer Model Controller

Listing 28.11 implements the route handling code for the Customer model. There are four routes. The getCustomer() route finds a customer order, based on the hard-coded customerA value. If it is successful, the Customer object is returned to the client as JSON strings. If the request fails, a 404 error is returned. The updateShipping() route creates a new Address object from the updatedShipping parameter in the POST request and then uses an update() method to update the Customer object with the new shipping data. If it is successful, a success message is returned; if it fails, a 404 error is returned.

The updateBilling() route creates a new Billing object from the updatedBilling parameter in the POST request and then uses an update() method to update the Customer object with the new billing data. If it is successful, a success message is returned; if it fails, a 404 error is returned. The updateCart() route uses the update() method to update the cart field of the Customer object with the updatedCart object sent in the POST request. If it is successful, a success message is returned. If it fails, a 404 error is returned.

Listing 28.11 customers_controller.js: Implementing routes to get and update customers for the Express server


01 var mongoose = require('mongoose'),
02     Customer = mongoose.model('Customer'),
03     Address = mongoose.model('Address'),
04     Billing = mongoose.model('Billing'),
05 exports.getCustomer = function(req, res) {
06   Customer.findOne({ userid: 'customerA' })
07   .exec(function(err, customer) {
08     if (!customer){
09       res.json(404, {msg: 'Customer Not Found.'});
10     } else {
11       res.json(customer);
12     }
13   });
14 };
15 exports.updateShipping = function(req, res){
16   var newShipping = new Address(req.body.updatedShipping);
17   Customer.update({ userid: 'customerA' },
18       {$set:{shipping:[newShipping.toObject()]}})
19   .exec(function(err, results){
20     if (err || results < 1){
21      res.json(404, {msg: 'Failed to update Shipping.'});
22     } else {
23      res.json({msg: "Customer Shipping Updated"});
24     }
25   });
26 };
27 exports.updateBilling = function(req, res){
28   // This is where you could verify the credit card information
29   // and halt the checkout if it is invalid.
30   var newBilling = new Billing(req.body.updatedBilling);
31   Customer.update({ userid: 'customerA' },
32       {$set:{billing:[newBilling.toObject()]}})
33   .exec(function(err, results){
34     if (err || results < 1){
35      res.json(404, {msg: 'Failed to update Billing.'});
36     } else {
37      res.json({msg: "Customer Billing Updated"});
38     }
39   });
40 };
41 exports.updateCart = function(req, res){
42   Customer.update({ userid: 'customerA' },
43       {$set:{cart:req.body.updatedCart}})
44   .exec(function(err, results){
45     if (err || results < 1){
46      res.json(404, {msg: 'Failed to update Cart.'});
47     } else {
48      res.json({msg: "Customer Cart Updated"});
49     }
50   });
51 };


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset