var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var InMemoryDataProxy = require('../data_proxies/in-memory/InMemoryDataProxy.js');
var CustomerDataProxy = require('../data_proxies/mongo/customerDataProxy');
var CustomerService = require('../business_logic/services/customerService');
var CategoryDataProxy = require('../data_proxies/mongo/categoryDataProxy');
var CategoryService = require('../business_logic/services/categoryService');
var ProductDataProxy = require('../data_proxies/mongo/productDataProxy');
var ProductService = require('../business_logic/services/productService');
var createController = require('./controllers/createController');

// MIDDLEWARE
app.use(function(req, res, next) {
  for (var key in req.query) {
    req.query[key.toLowerCase()] = req.query[key];
  }
  next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

createController('/customers', app, new CustomerService(new CustomerDataProxy()));
createController('/categories', app, new CategoryService(new CategoryDataProxy()));
createController('/products', app, new ProductService(new ProductDataProxy()));

app.get('/customers/:id/addresses', function(req, res) {
  res.json({'hi':req.params.id});
});

app.get('/inventoryItems?:productId', function(req, res) {
  res.json({'items':req.query.productid});
});

//app.get('/', function(req, res) {
  ////res.send('hello world');
  //res.json({'hi':'world'});
//});

var server = app.listen(3000, function() {
  console.log("server running ...");
});

@@ -85,24 +85,56 @@
 y-parser');%0A
+var utils = require('./utils');%0A
 var InMemory
@@ -958,16 +958,81 @@
 e%0A%7D));%0A%0A
+var OK = 200;%0Avar BAD_REQUEST = 400;%0Avar NOT_FOUND = 404;%0A%0Autils.
 createCo
@@ -1098,32 +1098,38 @@
 rDataProxy()));%0A
+utils.
 createController
@@ -1200,42 +1200,94 @@
 ));%0A
-createController('/products', app,
+utils.addRouteHandler(() =%3E %7B%0A  app.get('/products', (req, res) =%3E %7B%0A    var service =
  new
@@ -1329,213 +1329,548 @@
 y())
-)
 ;%0A
-%0Aapp.get('/customers/:id/addresses', function(req, res) %7B%0A  res.json(%7B'hi':req.params.id%7D);%0A%7D);%0A%0Aapp.get('/inventoryItems?:
+    var command = service.getAllCommand();%0A    if (req.query.categoryid) %7B%0A      command = service.getByCategoryCommand(req.query.categoryid);%0A    %7D%0A    command.execute((err, result) =%3E %7B%0A      if (result.success) %7B%0A        if (result.value) %7B%0A          res.status(OK).json(result.value);%0A        %7D else %7B%0A          res.status(NOT_FOUND).send(%22%22);%0A        %7D%0A      %7D else %7B%0A        res.status(BAD_REQUEST).json(result.errors);%0A      %7D%0A    %7D);%0A  %7D);%0A%7D);%0Autils.createController('/
 product
-Id', function(req, res) %7B%0A  res.json(%7B'items':req.query.productid%7D);%0A%7D);
+s', app, new ProductService(new ProductDataProxy()));%0A
 %0A%0A//

add route to handle products?categoryid