From f77a7088a0e5c30f3a7f38188f0380c1e4b5cdad Mon Sep 17 00:00:00 2001 From: William Bruno Date: Mon, 30 Mar 2015 21:43:44 -0300 Subject: [PATCH] Users Endpoing --- crud-nodejs/Db/mysql.js | 11 ++++++ crud-nodejs/controller/UsersController.js | 44 +++++++++++++++++++++++ crud-nodejs/model/BaseController.js | 0 crud-nodejs/model/ProductsModel.js | 2 -- crud-nodejs/model/UsersModel.js | 31 ++++++++++++++++ crud-nodejs/package.json | 3 +- crud-nodejs/routes/index.js | 5 +++ 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 crud-nodejs/controller/UsersController.js create mode 100644 crud-nodejs/model/BaseController.js create mode 100644 crud-nodejs/model/UsersModel.js diff --git a/crud-nodejs/Db/mysql.js b/crud-nodejs/Db/mysql.js index e69de29..e3f80c2 100644 --- a/crud-nodejs/Db/mysql.js +++ b/crud-nodejs/Db/mysql.js @@ -0,0 +1,11 @@ +var mysql = require('mysql'); +var connection = mysql.createConnection({ + host : 'localhost', + user : 'root', + password : '', + database : 'nodecrud' +}); + +connection.connect(); + +module.exports = connection; diff --git a/crud-nodejs/controller/UsersController.js b/crud-nodejs/controller/UsersController.js new file mode 100644 index 0000000..5a1ae8f --- /dev/null +++ b/crud-nodejs/controller/UsersController.js @@ -0,0 +1,44 @@ +var UsersModel = require('../model/UsersModel'); +var Promise = require('bluebird'); + + +function UsersController(Model) { + this.Model = Promise.promisifyAll(Model); +} + +UsersController.prototype.create = function(req, res) { + +}; + +UsersController.prototype.findOne = function(req, res) { + var _id = req.params._id; + + this.Model.findOneAsync(_id) + .then(function(result) { + res.json(result[0] || []); + }) + .catch(function(err) { + console.log(err) + }); +}; + +UsersController.prototype.findAll = function(req, res) { + this.Model.findAllAsync() + .then(function(result) { + res.json(result || []); + }) + .catch(function(err) { + console.log(err) + }); +}; + +UsersController.prototype.update = function(req, res) { + +}; + + +UsersController.prototype.delete = function(req, res) { + +}; + +module.exports = new UsersController(UsersModel); diff --git a/crud-nodejs/model/BaseController.js b/crud-nodejs/model/BaseController.js new file mode 100644 index 0000000..e69de29 diff --git a/crud-nodejs/model/ProductsModel.js b/crud-nodejs/model/ProductsModel.js index 968cd2d..fa0c9f1 100644 --- a/crud-nodejs/model/ProductsModel.js +++ b/crud-nodejs/model/ProductsModel.js @@ -9,8 +9,6 @@ ProductsModel.prototype.create = function(data, callback) { }; ProductsModel.prototype.findOne = function(_id, callback) { - console.log('model _id', _id); - mongo.collection('products').findOne({ "_id": mongo.ObjectId(_id) }, callback); }; diff --git a/crud-nodejs/model/UsersModel.js b/crud-nodejs/model/UsersModel.js new file mode 100644 index 0000000..21c78bc --- /dev/null +++ b/crud-nodejs/model/UsersModel.js @@ -0,0 +1,31 @@ +var mysql = require('../Db/mysql'); + +function UsersModel() { + +} + +UsersModel.prototype.create = function(data, callback) { + +}; + +UsersModel.prototype.findOne = function(_id, callback) { + mysql.query('SELECT id, name, age FROM users WHERE id = ' + _id, function(err, rows, fields) { + callback(err, rows); + }); +}; + +UsersModel.prototype.findAll = function(callback) { + mysql.query('SELECT id, name, age FROM users', function(err, rows, fields) { + callback(err, rows); + }); +}; + +UsersModel.prototype.update = function(data, _id, callback) { + +}; + +UsersModel.prototype.delete = function(_id, callback) { + +}; + +module.exports = new UsersModel(); diff --git a/crud-nodejs/package.json b/crud-nodejs/package.json index cb73ab4..7532326 100644 --- a/crud-nodejs/package.json +++ b/crud-nodejs/package.json @@ -22,7 +22,8 @@ "body-parser": "^1.12.2", "express": "^4.12.3", "method-override": "^2.3.2", - "mongojs": "^0.18.2" + "mongojs": "^0.18.2", + "mysql": "^2.6.1" }, "devDependencies": { "mocha": "^2.2.1", diff --git a/crud-nodejs/routes/index.js b/crud-nodejs/routes/index.js index bfe9c1c..d67c209 100644 --- a/crud-nodejs/routes/index.js +++ b/crud-nodejs/routes/index.js @@ -2,6 +2,7 @@ var express = require('express'); var router = express.Router(); var ProductsController = require('../controller/ProductsController'); +var UsersController = require('../controller/UsersController'); router.get ('/products', ProductsController.findAll.bind(ProductsController)); router.get ('/products/:_id', ProductsController.findOne.bind(ProductsController)); @@ -9,4 +10,8 @@ router.post('/products', ProductsController.create.bind(ProductsController) router.put ('/products/:_id', ProductsController.update.bind(ProductsController)); router.delete('/products/:_id', ProductsController.delete.bind(ProductsController)); + +router.get ('/users', UsersController.findAll.bind(UsersController)); +router.get ('/users/:_id', UsersController.findOne.bind(UsersController)); + module.exports = router;