diff --git a/.cfignore b/.cfignore new file mode 100644 index 0000000..56aec12 --- /dev/null +++ b/.cfignore @@ -0,0 +1,2 @@ +launchConfigurations/ +.git/ diff --git a/api/controllers/garages.js b/api/controllers/garages.js new file mode 100644 index 0000000..495e0c9 --- /dev/null +++ b/api/controllers/garages.js @@ -0,0 +1,22 @@ +/*jslint node: true */ +'use strict'; + +var garageModel = require ('../models/garage'); + +module.exports = { + garages: garages, +}; + +/* return a list of garages (garage objects) */ +function garages(req, res) { + // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name} + if (req.swagger.params && req.swagger.params.garageId) { + var garageId = req.swagger.params.garageId.value; + var garageData = garageModel.getGarageById(garageId); + + res.json(garageData); + } else { + var garages = garageModel.getAllGarages(); + res.json(garages); + } +} diff --git a/api/controllers/hello_world.js b/api/controllers/hello_world.js index cd665db..96cbfc3 100755 --- a/api/controllers/hello_world.js +++ b/api/controllers/hello_world.js @@ -38,7 +38,8 @@ module.exports = { function hello(req, res) { // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name} var name = req.swagger.params.name.value || 'stranger'; - var helloname = util.format('Hello, %s!', name); + // 'Hello there' was 'Hello' + var helloname = util.format('Hello there, %s!', name); // this sends back a JSON response which is a single string res.json(helloname); diff --git a/api/models/garage.js b/api/models/garage.js new file mode 100644 index 0000000..973b30d --- /dev/null +++ b/api/models/garage.js @@ -0,0 +1,50 @@ +/*jslint node: true */ +'use strict'; + +module.exports = { + getGarageById: getGarageById, + getAllGarages: getAllGarages, +}; + +/* return a single Garage object */ +function getGarageById(garageId) { + var foundApp = {}; + for (var i = 0; i < GARAGES.length; i++) { + if (GARAGES[i].id == garageId) { + foundApp = GARAGES[i]; + } + } + + return foundApp; +} + +function getAllGarages() { + return GARAGES; +} + +// sample data +var GARAGES = [{ + id: 6212, + name: 'Main door home', + lastUpdated: '2016-07-10T14:48:00', + desiredState: 'open', + status: 'open', +}, { + id: 6213, + name: 'Main door home', + lastUpdated: '2016-07-10T17:48:00', + desiredState: 'closed', + status: 'open', +}, { + id: 6214, + name: 'Main door home', + lastUpdated: '2016-07-10T17:48:00', + desiredState: 'closed', + status: 'closed', +}, { + id: 6215, + name: 'Main door home', + lastUpdated: '2016-07-10T17:48:00', + desiredState: 'open', + status: 'closed', +}]; diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index fdf275e..ca8d2d0 100755 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -65,6 +65,51 @@ paths: description: Error schema: $ref: "#/definitions/ErrorResponse" + /garages: + # binds app logic to a route (api/controllers/applications.js) + x-swagger-router-controller: garages + get: + description: Returns a list of garages to the caller + # used as the method name of the controller inside garages.js + operationId: garages + responses: + "200": + description: Success + schema: + type: array + items: + # a pointer to a definition + $ref: "#/definitions/Garage" + # responses may fall through to errors + default: + description: Error + schema: + $ref: "#/definitions/ErrorResponse" + /garages/{garageId}: + # Use the house address as the garageId + # binds app logic to a route (api/controllers/garages.js) + x-swagger-router-controller: garages + get: + description: Returns a single garage configuration to the caller + # used as the method name of the controller inside garages.js + operationId: garages + parameters: + - name: garageId + in: path + description: returns a garage object for the given id + # the path params are explicitly required by the spec + required: true + type: string + responses: + "200": + description: Successß + schema: + $ref: "#/definitions/Garage" + # responses may fall through to errors + default: + description: Error + schema: + $ref: "#/definitions/ErrorResponse" /hello: # binds app logic to a route (api/controllers/hello_world.js) x-swagger-router-controller: hello_world @@ -100,6 +145,18 @@ definitions: type: string status: type: string + Garage: + properties: + garageId: + type: string + name: + type: string + description: + type: string + desiredState: + type: string + status: + type: string ErrorResponse: required: - message diff --git a/config/README.md b/config/README.md index d0535bd..f43440b 100755 --- a/config/README.md +++ b/config/README.md @@ -1 +1 @@ -Place configuration files in this directory. +Place configuration my files in this directory. diff --git a/manifest.yml b/manifest.yml index 37eea6a..4afc7c3 100644 --- a/manifest.yml +++ b/manifest.yml @@ -1,5 +1,6 @@ applications: - disk_quota: 1024M + buildpack: nodejs_buildpack host: devops-tutorial name: devops-tutorial command: node app.js diff --git a/test/api/controllers/hello_world.js b/test/api/controllers/hello_world.js index c5c4652..44dbe0e 100755 --- a/test/api/controllers/hello_world.js +++ b/test/api/controllers/hello_world.js @@ -20,7 +20,7 @@ describe('controllers', function() { .end(function(err, res) { should.not.exist(err); - res.body.should.eql('Hello, stranger!'); + res.body.should.eql('Hello there, stranger!'); done(); }); @@ -39,7 +39,7 @@ describe('controllers', function() { .end(function(err, res) { should.not.exist(err); - res.body.should.eql('Hello, Scott!'); + res.body.should.eql('Hello there, Scott!'); done(); }); diff --git a/views/index.hbs b/views/index.hbs index 2819396..b9c1ad5 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -16,6 +16,8 @@
/hello?name=joe GET request to /hello with a query string parameter of 'name=joe'
/applications GET request to /applications to get all the applications
/applications/1234 GET request to /applications/ with a path parameter of '1234' to get a specific application
+/garages GET request to /garages to get all the garages
+/garages/6212 GET request to /garages/ with a path parameter of '6212' to get a specific garage configuration