Skip to content

Commit

Permalink
Very simple API for products
Browse files Browse the repository at this point in the history
  • Loading branch information
john@traider.io committed Apr 23, 2014
0 parents commit 929a497
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api/api.products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var db = require("../db/db.products.js");

// define the routes for /api/users
module.exports = function attachHandlers (router){ //, passport) {
// get requests
router.get('/api/products', list);
router.get('/api/products/:id', view);
};


function list(req, res)
{
db.getAll(function(data){
res.json(data);
});
}

function view(req, res)
{
db.getById(req.params.id, function(data){
res.json(data);
});
}
18 changes: 18 additions & 0 deletions db/db.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var mongo = require("mongodb");
var MongoClient = mongo.MongoClient,
Server = require('mongodb').Server,
BSON = mongo.BSONPure;

var mongoclient = new MongoClient(new Server("127.0.0.1", 27017), {native_parser: true});

exports.getDbClient = function(){
return mongoclient;
};

exports.dbName = function(){
return "traider";
};

exports.makeObjectID = function(id){
return new BSON.ObjectID(id);
};
65 changes: 65 additions & 0 deletions db/db.products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var mongoHandler = require("./db.client.js");
var mongoclient = mongoHandler.getDbClient();
var collectionName = "products";


exports.getById = function(id, callback){

// Open the connection to the server
mongoclient.open(function(err, mongoclient) {
var dbName = mongoHandler.dbName();
var db = mongoclient.db(dbName);

db.collection(collectionName).findOne({"_id": mongoHandler.makeObjectID(id)}, function(err, result) {
mongoclient.close();
if (err){
mongoclient.close();
throw err.Message;
return;
}
else
{
// Close the connection
callback(result);
return;
}
});
});
};

exports.getAll = function(callback)
{
if(callback === null || typeof(callback) !== "function"){throw "Call to db method must include callback function"}
mongoclient.open(function(err, mongoclient) {

if (err){
mongoclient.close();
throw err.Message;
return;
}

var dbName = mongoHandler.dbName();
var db = mongoclient.db(dbName);
console.log(dbName+"."+collectionName);

db.collection(collectionName).find({}, function(err, result) {
if (err){
mongoclient.close();
throw err.Message;
return;
}
else
{
result.toArray(function(err, resultArray)
{
// Close the connection
mongoclient.close();

console.log("Got data: " + resultArray.length + " records.");
callback(resultArray);
return;
});
}
});
});
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "traider.io",
"description": "",
"version": "0.0.1",
"main": "traider.js",
"author": {
"name": "john",
"email": "john@traider.io"
},
"keywords": [],
"licenses": {
"type": "mit"
},
"dependencies": {
"express": "3.x" ,
"mongodb": "1.3.23"
},
"repository": {
"type": "git",
"url": "git@github.com:EastpointSoftware/traider.io.git"
},
"bugs": {
"url": "https://github.com/EastpointSoftware/traider.io/issues"
}
}
7 changes: 7 additions & 0 deletions routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


exports.attachHandlers = function attachHandlers (server){ //, passport) {

require('../api/api.products.js')(server); //, passport);

};
25 changes: 25 additions & 0 deletions traider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var express = require('express');
var routes = require('./routes/routes.js');


createServer = function createServer () {

var server = express();
// specify middleware
//server.use(express.cookieParser());

//server.use(express.bodyParser());
server.use('/css', express.static(__dirname + '/media/css'));
server.use('/img', express.static(__dirname + '/media/img'));
server.use('/js', express.static(__dirname + '/media/js'));

// attach router handlers
routes.attachHandlers(server); //, passport);

return server;

};


var server = createServer();
server.listen(8003);

0 comments on commit 929a497

Please sign in to comment.