Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merged routesMongoHQ and mongoHQdb functionality to daos/mongoHQDao

  • Loading branch information...
commit b10c62d887a04723d0dda770c541f81afb26b0d9 1 parent 0099e40
@yesobo authored
View
13 app.coffee
@@ -2,12 +2,9 @@
express = require 'express'
# Create Application Server
app = express()
-# Set Route Init Path
-#routes = "./routes.js"
-routes = "./routesMongoHQ.js"
-# Set Database Init Path
+# Set dao init path
#database = "./database.js"
-database = "./mongoHQdb.js"
+routes = "./routesMongoHQ.js"
###
app
@@ -72,12 +69,6 @@ app.configure 'production', ->
app.get '/api', (req, res)=>
res.send 'Ecomm API is running'
-###
-db (database)
-@type {Object}
-@param Object [database_options] the database options
-###
-GLOBAL.db = require(database)(database_options)
# Routes
routes = require routes
# HTTP Server
View
0  daos/dao.coffee → daos/mongoHQDao.coffee
File renamed without changes
View
0  daos/dao.js → daos/mongoHQDao.js
File renamed without changes
View
0  daos/dao_test.coffee → daos/mongoHQDao_test.coffee
File renamed without changes
View
0  daos/dao_test.js → daos/mongoHQDao_test.js
File renamed without changes
View
80 mongoHQdb.coffee
@@ -1,80 +0,0 @@
-# npm install mongodb
-mongodb = require 'mongodb'
-url = require 'url'
-log = console.log
-###
-Database Connection
-###
-module.exports = () ->
- log 'connecting to database...'
- #connection_uri = "mongodb://admin:1234@alex.mongohq.com:10001/design_patterns"
- db = new mongodb.Db('design_patterns', new mongodb.Server('alex.mongohq.com', 10001, {auto_reconnect:true}), {});
- db.open (err, p_client) ->
- #Notice the USERNAME and PASSWORD!
- db.authenticate 'admin', '1234', (err) ->
- #Change error handler when going into production
- log 'authenticated!'
- db
-###
- dbOp =
- success: (callback) =>
- log 'waiting for result...'
- while @result != null then
- eval callback(@result)
- result: null
-
- findAll = ->
- log 'executing findAll'
- db.collection 'design_patterns', (err, collection) ->
- log err if err
- log 'executing find documents'
- collection.find().toArray (err, items) ->
- throw error if err
- log 'items is #{items}'
- dbOp.result = items
- dbOp
-
- log 'calling findAll'
- findAll().success (patterns) ->
- log patterns
- log 'PATTERNS'
- log '========'
- for p in patterns
- log p.id
- log p.name
- log p.intent
-
-
-db.connect connection_uri, (error, client) ->
- throw error if error
- log "Connected to design_patterns database"
-
-
-Pattern_model.findAll().success (patterns) ->
- res.send patterns
-
-dbOp =
- success: (callback) =>
- eval callback(this.result)
-
-findAll = ->
- db.collection 'design_patterns', (err, collection) ->
- collection.find().toArray (err, items) ->
- dbOp.result = items
- deOp
-
-log 'calling findAll'
-findAll().success (patterns) ->
- log 'PATTERNS'
- log '========'
- for p in patterns
- log p.id
- log p.name
- log p.intent
-
-create(pattern).success (pat) ->
-.count().success (c) ->
-.find(intId).success (pattern) ->
-pattern.save().success ->
-pattern.destroy().success ->
-###
View
95 mongoHQdb.js
@@ -1,95 +0,0 @@
-// Generated by CoffeeScript 1.3.3
-(function() {
- var log, mongodb, url;
-
- mongodb = require('mongodb');
-
- url = require('url');
-
- log = console.log;
-
- /*
- Database Connection
- */
-
-
- module.exports = function() {
- var db;
- log('connecting to database...');
- db = new mongodb.Db('design_patterns', new mongodb.Server('alex.mongohq.com', 10001, {
- auto_reconnect: true
- }), {});
- db.open(function(err, p_client) {
- return db.authenticate('admin', '1234', function(err) {
- return log('authenticated!');
- });
- });
- return db;
- };
-
- /*
- dbOp =
- success: (callback) =>
- log 'waiting for result...'
- while @result != null then
- eval callback(@result)
- result: null
-
- findAll = ->
- log 'executing findAll'
- db.collection 'design_patterns', (err, collection) ->
- log err if err
- log 'executing find documents'
- collection.find().toArray (err, items) ->
- throw error if err
- log 'items is #{items}'
- dbOp.result = items
- dbOp
-
- log 'calling findAll'
- findAll().success (patterns) ->
- log patterns
- log 'PATTERNS'
- log '========'
- for p in patterns
- log p.id
- log p.name
- log p.intent
-
-
- db.connect connection_uri, (error, client) ->
- throw error if error
- log "Connected to design_patterns database"
-
-
- Pattern_model.findAll().success (patterns) ->
- res.send patterns
-
- dbOp =
- success: (callback) =>
- eval callback(this.result)
-
- findAll = ->
- db.collection 'design_patterns', (err, collection) ->
- collection.find().toArray (err, items) ->
- dbOp.result = items
- deOp
-
- log 'calling findAll'
- findAll().success (patterns) ->
- log 'PATTERNS'
- log '========'
- for p in patterns
- log p.id
- log p.name
- log p.intent
-
- create(pattern).success (pat) ->
- .count().success (c) ->
- .find(intId).success (pattern) ->
- pattern.save().success ->
- pattern.destroy().success ->
- */
-
-
-}).call(this);
View
82 routes.coffee
@@ -1,64 +1,64 @@
-Pattern_model = db.models.Pattern
-
-# Adds a pattern to the db
-addPattern = (pattern, cb) ->
- Pattern_model.count().success (c)->
- pattern.id = c + 1
- Pattern_model.create(pattern).success (pat) ->
- cb()
+DBConnector = require "./daos/mongoHQDao.js"
+daoObj = new DBConnector 'design_patterns', 'alex.mongohq.com', 10001
# GET all patterns
app.get '/api/patterns', (req, res) ->
- Pattern_model.findAll().success (patterns) ->
- res.send patterns
+ daoObj.findAll (err, items) ->
+ res.send items
# GET the number of patterns
app.get '/api/patterns/count', (req, res) ->
- Pattern_model.count().success (c) ->
- res.send c.toString()
+ daoObj.count (err, count) ->
+ res.send count.toString()
# GET pattern by id
app.get '/api/patterns/:id', (req, res) ->
- intId = parseInt(req.params.id)
- Pattern_model.find(intId).success (pattern) ->
- if pattern?
- res.send(pattern)
- else
- res.send 404
+ intId = parseInt req.params.id
+ daoObj.findById intId, (err, item) ->
+ console.log "result: #{item}"
+ if err?
+ res.send 500
+ else
+ if item?
+ res.send item
+ else
+ res.send 404
# POST a new pattern
app.post '/api/patterns', (req, res) ->
new_pattern =
- id: null # generated automattically
+ id: req.body.id
name: req.body.name
category: req.body.category
intent: req.body.intent
motivation: req.body.motivation
applicability: req.body.applicability
structure: req.body.structure
- addPattern new_pattern, (pattern) ->
- res.send new_pattern
-
-# PUT update pattern by id
-app.put '/api/patterns/:id', (req, res)->
+ daoObj.insert new_pattern, (err, docs) ->
+ res.send 200
+
+# PUT upgrade pattern by id
+app.put '/api/patterns/:id', (req, res) ->
intId = parseInt req.params.id
- Pattern_model.find(intId).success (pattern) ->
- pattern.name = req.body.name
- pattern.category = req.body.category
- pattern.intent = req.body.intent
- pattern.motivation = req.body.motivation
- pattern.applicability = req.body.applicability
- pattern.structure = req.body.structure
-
- pattern.save().success ->
- res.send 200
+ updated_pattern =
+ name: req.body.name
+ category: req.body.category
+ intent: req.body.intent
+ motivation: req.body.motivation
+ applicability: req.body.applicability
+ structure: req.body.structure
+ daoObj.update updated_pattern, (err) ->
+ if err?
+ res.send 404
+ else
+ daoObj.findById updated_pattern intId, (err, item) ->
+ res.send item
# DELETE delete product by id
-app.del '/api/patterns/:id', (req, res)->
+app.del '/api/patterns/:id', (req, res) ->
intId = parseInt req.params.id
- Pattern_model.find(intId).success (pattern)->
- if pattern?
- pattern.destroy().success ->
- res.send 'pattern ' + pattern.id + ' deleted'
- else
- res.send 404
+ daoObj.delete intId, (err) ->
+ if err?
+ res.send 500
+ else
+ res.send 200
View
78 routes.js
@@ -1,38 +1,36 @@
// Generated by CoffeeScript 1.3.3
(function() {
- var Pattern_model, addPattern;
+ var DBConnector, daoObj;
- Pattern_model = db.models.Pattern;
+ DBConnector = require("./daos/mongoHQDao.js");
- addPattern = function(pattern, cb) {
- return Pattern_model.count().success(function(c) {
- pattern.id = c + 1;
- return Pattern_model.create(pattern).success(function(pat) {
- return cb();
- });
- });
- };
+ daoObj = new DBConnector('design_patterns', 'alex.mongohq.com', 10001);
app.get('/api/patterns', function(req, res) {
- return Pattern_model.findAll().success(function(patterns) {
- return res.send(patterns);
+ return daoObj.findAll(function(err, items) {
+ return res.send(items);
});
});
app.get('/api/patterns/count', function(req, res) {
- return Pattern_model.count().success(function(c) {
- return res.send(c.toString());
+ return daoObj.count(function(err, count) {
+ return res.send(count.toString());
});
});
app.get('/api/patterns/:id', function(req, res) {
var intId;
intId = parseInt(req.params.id);
- return Pattern_model.find(intId).success(function(pattern) {
- if (pattern != null) {
- return res.send(pattern);
+ return daoObj.findById(intId, function(err, item) {
+ console.log("result: " + item);
+ if (err != null) {
+ return res.send(500);
} else {
- return res.send(404);
+ if (item != null) {
+ return res.send(item);
+ } else {
+ return res.send(404);
+ }
}
});
});
@@ -40,7 +38,7 @@
app.post('/api/patterns', function(req, res) {
var new_pattern;
new_pattern = {
- id: null,
+ id: req.body.id,
name: req.body.name,
category: req.body.category,
intent: req.body.intent,
@@ -48,37 +46,41 @@
applicability: req.body.applicability,
structure: req.body.structure
};
- return addPattern(new_pattern, function(pattern) {
- return res.send(new_pattern);
+ return daoObj.insert(new_pattern, function(err, docs) {
+ return res.send(200);
});
});
app.put('/api/patterns/:id', function(req, res) {
- var intId;
+ var intId, updated_pattern;
intId = parseInt(req.params.id);
- return Pattern_model.find(intId).success(function(pattern) {
- pattern.name = req.body.name;
- pattern.category = req.body.category;
- pattern.intent = req.body.intent;
- pattern.motivation = req.body.motivation;
- pattern.applicability = req.body.applicability;
- pattern.structure = req.body.structure;
- return pattern.save().success(function() {
- return res.send(200);
- });
+ updated_pattern = {
+ name: req.body.name,
+ category: req.body.category,
+ intent: req.body.intent,
+ motivation: req.body.motivation,
+ applicability: req.body.applicability,
+ structure: req.body.structure
+ };
+ return daoObj.update(updated_pattern, function(err) {
+ if (err != null) {
+ return res.send(404);
+ } else {
+ return daoObj.findById(updated_pattern(intId, function(err, item) {
+ return res.send(item);
+ }));
+ }
});
});
app.del('/api/patterns/:id', function(req, res) {
var intId;
intId = parseInt(req.params.id);
- return Pattern_model.find(intId).success(function(pattern) {
- if (pattern != null) {
- return pattern.destroy().success(function() {
- return res.send('pattern ' + pattern.id + ' deleted');
- });
+ return daoObj["delete"](intId, function(err) {
+ if (err != null) {
+ return res.send(500);
} else {
- return res.send(404);
+ return res.send(200);
}
});
});
View
69 routesMongoHQ.coffee
@@ -1,69 +0,0 @@
-MongoDBConnector = require "./daos/dao.js"
-daoObj = new MongoDBConnector 'design_patterns', 'alex.mongohq.com', 10001
-
-# GET all patterns
-app.get '/api/patterns', (req, res) ->
- daoObj.findAll (err, items) ->
- res.send items
-
-# GET the number of patterns
-app.get '/api/patterns/count', (req, res) ->
- daoObj.count (err, count) ->
- res.send count.toString()
-
-# GET pattern by id
-app.get '/api/patterns/:id', (req, res) ->
- console.log "getting pattern..."
- console.log "req: #{req.params}"
- intId = parseInt req.params.id
- console.log intId
- console.log "calling findById..."
- daoObj.findById intId, (err, item) ->
- console.log "result: #{item}"
- if err?
- res.send 500
- else
- if item?
- res.send item
- else
- res.send 404
-
-# POST a new pattern
-app.post '/api/patterns', (req, res) ->
- new_pattern =
- id: req.body.id
- name: req.body.name
- category: req.body.category
- intent: req.body.intent
- motivation: req.body.motivation
- applicability: req.body.applicability
- structure: req.body.structure
- daoObj.insert new_pattern, (err, docs) ->
- res.send 200
-
-# PUT upgrade pattern by id
-app.put '/api/patterns/:id', (req, res) ->
- console.log "executing app.put"
- intId = parseInt req.params.id
- updated_pattern =
- name: req.body.name
- category: req.body.category
- intent: req.body.intent
- motivation: req.body.motivation
- applicability: req.body.applicability
- structure: req.body.structure
- daoObj.update updated_pattern, (err) ->
- if err?
- res.send 404
- else
- daoObj.findById updated_pattern intId, (err, item) ->
- res.send item
-
-# DELETE delete product by id
-app.del '/api/patterns/:id', (req, res) ->
- intId = parseInt req.params.id
- daoObj.delete intId, (err) ->
- if err?
- res.send 500
- else
- res.send 200
View
93 routesMongoHQ.js
@@ -1,93 +0,0 @@
-// Generated by CoffeeScript 1.3.3
-(function() {
- var MongoDBConnector, daoObj;
-
- MongoDBConnector = require("./daos/dao.js");
-
- daoObj = new MongoDBConnector('design_patterns', 'alex.mongohq.com', 10001);
-
- app.get('/api/patterns', function(req, res) {
- return daoObj.findAll(function(err, items) {
- return res.send(items);
- });
- });
-
- app.get('/api/patterns/count', function(req, res) {
- return daoObj.count(function(err, count) {
- return res.send(count.toString());
- });
- });
-
- app.get('/api/patterns/:id', function(req, res) {
- var intId;
- console.log("getting pattern...");
- console.log("req: " + req.params);
- intId = parseInt(req.params.id);
- console.log(intId);
- console.log("calling findById...");
- return daoObj.findById(intId, function(err, item) {
- console.log("result: " + item);
- if (err != null) {
- return res.send(500);
- } else {
- if (item != null) {
- return res.send(item);
- } else {
- return res.send(404);
- }
- }
- });
- });
-
- app.post('/api/patterns', function(req, res) {
- var new_pattern;
- new_pattern = {
- id: req.body.id,
- name: req.body.name,
- category: req.body.category,
- intent: req.body.intent,
- motivation: req.body.motivation,
- applicability: req.body.applicability,
- structure: req.body.structure
- };
- return daoObj.insert(new_pattern, function(err, docs) {
- return res.send(200);
- });
- });
-
- app.put('/api/patterns/:id', function(req, res) {
- var intId, updated_pattern;
- console.log("executing app.put");
- intId = parseInt(req.params.id);
- updated_pattern = {
- name: req.body.name,
- category: req.body.category,
- intent: req.body.intent,
- motivation: req.body.motivation,
- applicability: req.body.applicability,
- structure: req.body.structure
- };
- return daoObj.update(updated_pattern, function(err) {
- if (err != null) {
- return res.send(404);
- } else {
- return daoObj.findById(updated_pattern(intId, function(err, item) {
- return res.send(item);
- }));
- }
- });
- });
-
- app.del('/api/patterns/:id', function(req, res) {
- var intId;
- intId = parseInt(req.params.id);
- return daoObj["delete"](intId, function(err) {
- if (err != null) {
- return res.send(500);
- } else {
- return res.send(200);
- }
- });
- });
-
-}).call(this);
View
64 routesSequelize.coffee
@@ -0,0 +1,64 @@
+Pattern_model = db.models.Pattern
+
+# Adds a pattern to the db
+addPattern = (pattern, cb) ->
+ Pattern_model.count().success (c)->
+ pattern.id = c + 1
+ Pattern_model.create(pattern).success (pat) ->
+ cb()
+
+# GET all patterns
+app.get '/api/patterns', (req, res) ->
+ Pattern_model.findAll().success (patterns) ->
+ res.send patterns
+
+# GET the number of patterns
+app.get '/api/patterns/count', (req, res) ->
+ Pattern_model.count().success (c) ->
+ res.send c.toString()
+
+# GET pattern by id
+app.get '/api/patterns/:id', (req, res) ->
+ intId = parseInt(req.params.id)
+ Pattern_model.find(intId).success (pattern) ->
+ if pattern?
+ res.send(pattern)
+ else
+ res.send 404
+
+# POST a new pattern
+app.post '/api/patterns', (req, res) ->
+ new_pattern =
+ id: null # generated automattically
+ name: req.body.name
+ category: req.body.category
+ intent: req.body.intent
+ motivation: req.body.motivation
+ applicability: req.body.applicability
+ structure: req.body.structure
+ addPattern new_pattern, (pattern) ->
+ res.send new_pattern
+
+# PUT update pattern by id
+app.put '/api/patterns/:id', (req, res)->
+ intId = parseInt req.params.id
+ Pattern_model.find(intId).success (pattern) ->
+ pattern.name = req.body.name
+ pattern.category = req.body.category
+ pattern.intent = req.body.intent
+ pattern.motivation = req.body.motivation
+ pattern.applicability = req.body.applicability
+ pattern.structure = req.body.structure
+
+ pattern.save().success ->
+ res.send 200
+
+# DELETE delete product by id
+app.del '/api/patterns/:id', (req, res)->
+ intId = parseInt req.params.id
+ Pattern_model.find(intId).success (pattern)->
+ if pattern?
+ pattern.destroy().success ->
+ res.send 'pattern ' + pattern.id + ' deleted'
+ else
+ res.send 404
View
86 routesSequelize.js
@@ -0,0 +1,86 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var Pattern_model, addPattern;
+
+ Pattern_model = db.models.Pattern;
+
+ addPattern = function(pattern, cb) {
+ return Pattern_model.count().success(function(c) {
+ pattern.id = c + 1;
+ return Pattern_model.create(pattern).success(function(pat) {
+ return cb();
+ });
+ });
+ };
+
+ app.get('/api/patterns', function(req, res) {
+ return Pattern_model.findAll().success(function(patterns) {
+ return res.send(patterns);
+ });
+ });
+
+ app.get('/api/patterns/count', function(req, res) {
+ return Pattern_model.count().success(function(c) {
+ return res.send(c.toString());
+ });
+ });
+
+ app.get('/api/patterns/:id', function(req, res) {
+ var intId;
+ intId = parseInt(req.params.id);
+ return Pattern_model.find(intId).success(function(pattern) {
+ if (pattern != null) {
+ return res.send(pattern);
+ } else {
+ return res.send(404);
+ }
+ });
+ });
+
+ app.post('/api/patterns', function(req, res) {
+ var new_pattern;
+ new_pattern = {
+ id: null,
+ name: req.body.name,
+ category: req.body.category,
+ intent: req.body.intent,
+ motivation: req.body.motivation,
+ applicability: req.body.applicability,
+ structure: req.body.structure
+ };
+ return addPattern(new_pattern, function(pattern) {
+ return res.send(new_pattern);
+ });
+ });
+
+ app.put('/api/patterns/:id', function(req, res) {
+ var intId;
+ intId = parseInt(req.params.id);
+ return Pattern_model.find(intId).success(function(pattern) {
+ pattern.name = req.body.name;
+ pattern.category = req.body.category;
+ pattern.intent = req.body.intent;
+ pattern.motivation = req.body.motivation;
+ pattern.applicability = req.body.applicability;
+ pattern.structure = req.body.structure;
+ return pattern.save().success(function() {
+ return res.send(200);
+ });
+ });
+ });
+
+ app.del('/api/patterns/:id', function(req, res) {
+ var intId;
+ intId = parseInt(req.params.id);
+ return Pattern_model.find(intId).success(function(pattern) {
+ if (pattern != null) {
+ return pattern.destroy().success(function() {
+ return res.send('pattern ' + pattern.id + ' deleted');
+ });
+ } else {
+ return res.send(404);
+ }
+ });
+ });
+
+}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.