Slide 155
Slide 155 text
app.put('/products/:id([0-9]+)/stock', function (req, res) {
var stock = req.body.stock;
var action = req.body.action;
if(action == 'increment') {
redisClient.incrby(req.params.id+':stock',stock, function(err, reply) {
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
ch.assertExchange('stock', 'direct', {durable: false});
ch.publish('stock', 'info', new Buffer(JSON.stringify({id: req.params.id, stock:
parseInt(reply)})));
res.json('Stock for product '+req.params.id+' is now '+parseInt(reply));
});
});
});
} else {
redisClient.decrby(req.params.id+':stock',stock, function(err, reply) {
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
ch.assertExchange('stock', 'direct', {durable: false});
ch.publish('stock', 'info', new Buffer(JSON.stringify({id: req.params.id, stock:
parseInt(reply)})));
res.json('Stock for product '+req.params.id+' is now '+parseInt(reply));
});
});
});
}
});
Update
stock in
Redis
Send
message to
queue