{"action":"created","issue":{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/862","labels_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/862/labels{/name}","comments_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/862/comments","events_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/862/events","html_url":"https://github.com/DaftMonk/generator-angular-fullstack/issues/862","id":59584035,"number":862,"title":"Ideas: Promises or Callbacks? Better 'MVCish' pattern?","user":{"login":"ericmdantas","id":7040162,"avatar_url":"https://avatars.githubusercontent.com/u/7040162?v=3","gravatar_id":"","url":"https://api.github.com/users/ericmdantas","html_url":"https://github.com/ericmdantas","followers_url":"https://api.github.com/users/ericmdantas/followers","following_url":"https://api.github.com/users/ericmdantas/following{/other_user}","gists_url":"https://api.github.com/users/ericmdantas/gists{/gist_id}","starred_url":"https://api.github.com/users/ericmdantas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ericmdantas/subscriptions","organizations_url":"https://api.github.com/users/ericmdantas/orgs","repos_url":"https://api.github.com/users/ericmdantas/repos","events_url":"https://api.github.com/users/ericmdantas/events{/privacy}","received_events_url":"https://api.github.com/users/ericmdantas/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-03-03T02:02:33Z","updated_at":"2015-03-13T13:07:42Z","closed_at":null,"body":"Hey, guys.\r\n\r\nI'd like to know your opinion about the servers's api generated files.\r\n\r\nToday, when you run this generator, you'll have the following files in ```server/api/thing```:\r\n\r\n```\r\nindex.js\r\nthing.model.js\r\nthing.controller.js\r\n```\r\n\r\n(Just forget for a second the socket and spec files)\r\n\r\nAlright, inside **index.js** we have all the routes for ```thing```, and the functions that will take care of each route. I, for one, love this approach. So, in my opinion, nothing to change there.\r\n\r\n\r\nInside **thing.model.js** we have the skeleton of ```thing```, and that's it. IMHO, that's also a very nice way to approach it, so all good.\r\n\r\n\r\nNow, inside **thing.controller.js** we have: \r\n- requisition;\r\n- response;\r\n- model; \r\n\r\nThe last one interacting with your database, doing the logic business and suchs and then, retrieving a response to the client.\r\n\r\nI believe we could make this better. We could make the controller take care of the requisition and response objects - by knowing how to understand what the client wants and responding to it - but not necessarily knowing what happens in the DB logic.\r\n\r\nI'd create a file called **things.dao.js** and have my data acess logic there.\r\n\r\nSo, instead of having:\r\n\r\n```js\r\n\r\nexports.create = function(req, res) {\r\n  Thing.create(req.body, function(err, thing) {\r\n    if(err) { return handleError(res, err); }\r\n    return res.json(201, thing);\r\n  });\r\n};\r\n\r\n```\r\n\r\nWe'd have:\r\n\r\n```js\r\n\r\nvar ThingDAO = require('./thing.dao');\r\n\r\nexports.create = function(req, res) \r\n{\r\n  var _thing = req.body;\r\n\r\n  var _onSuccess = function(thing)\r\n  {\r\n       res.status(200).json(thing);\r\n  }\r\n\r\n  var _onError = function(error)\r\n  {\r\n       res.status(500).json(error);    \r\n  }\r\n\r\n  ThingDAO\r\n     .create(_thing)\r\n     .then(_onSuccess)\r\n     .catch(_onError); \r\n};\r\n\r\n```\r\n\r\nAnd inside my new file (things.dao.js):\r\n\r\n```js\r\n\r\nvar Thing = require('./thing.model');\r\nvar Promise = require('bluebird'); // IMO, the best Promise lib out there\r\n\r\nexports.create = function(thing)\r\n{\r\n      return new Promise(function(resolve, reject)\r\n      {\r\n             var _onSave = function(error, thing)\r\n             {\r\n                  error ? reject(error)\r\n                        : resolve(thing);\r\n             }\r\n\r\n             var _newThing = new Thing(thing);\r\n\r\n             _newThing.save(_onSave);\r\n      })\r\n}\r\n\r\n```\r\n\r\n\r\nBy doing so, we'd gain a lot. Not only by simplifying the controllers, but making sure each 'layer' of the project really knows how to take care of itself. \r\n\r\nControllers'll talk to the endpoints and call services, DAOs, models validations and suchs; nothing more.\r\n\r\nDAOs will know how to interact with the database. Get data from it, insert data into it, update and delete it. Always returning a [Promise](https://promisesaplus.com/).\r\n\r\nBy doing so, we could easily chain DAOs that depend on each other for whatever situation, like:\r\n\r\n```js\r\n\r\n    SomethingDAO\r\n            .getById(id)\r\n            .then(someOtherDAO.doSomethingWithIt)\r\n            .then(whatever.someVerb) \r\n            .catch(ErrorHandler.handlePossibleException);\r\n\r\n```\r\n\r\n\r\nI'd say if enough people like the idea, we could easily organize a PR and change it as soon as possible.\r\n\r\nLet me know if you have any suggestions =]"},"comment":{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/comments/78962925","html_url":"https://github.com/DaftMonk/generator-angular-fullstack/issues/862#issuecomment-78962925","issue_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/862","id":78962925,"user":{"login":"ericmdantas","id":7040162,"avatar_url":"https://avatars.githubusercontent.com/u/7040162?v=3","gravatar_id":"","url":"https://api.github.com/users/ericmdantas","html_url":"https://github.com/ericmdantas","followers_url":"https://api.github.com/users/ericmdantas/followers","following_url":"https://api.github.com/users/ericmdantas/following{/other_user}","gists_url":"https://api.github.com/users/ericmdantas/gists{/gist_id}","starred_url":"https://api.github.com/users/ericmdantas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ericmdantas/subscriptions","organizations_url":"https://api.github.com/users/ericmdantas/orgs","repos_url":"https://api.github.com/users/ericmdantas/repos","events_url":"https://api.github.com/users/ericmdantas/events{/privacy}","received_events_url":"https://api.github.com/users/ericmdantas/received_events","type":"User","site_admin":false},"created_at":"2015-03-13T13:07:42Z","updated_at":"2015-03-13T13:07:42Z","body":"Noted."}}