From 5048fb76950a9fea1571ed579bcd1e715755a29a Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Fri, 10 Apr 2015 11:51:22 -0800 Subject: [PATCH] Roughing in users resource --- app/adapters/user.js | 8 +++ app/models/user.js | 10 ++++ app/router.js | 1 + app/routes/users.js | 4 ++ app/routes/users/index.js | 7 +++ app/templates/users/index.hbs | 3 ++ package.json | 5 +- server/mocks/users.js | 75 +++++++++++++++++++++++++++ tests/unit/models/user-test.js | 15 ++++++ tests/unit/routes/users-test.js | 14 +++++ tests/unit/routes/users/index-test.js | 14 +++++ 11 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 app/adapters/user.js create mode 100644 app/models/user.js create mode 100644 app/routes/users.js create mode 100644 app/routes/users/index.js create mode 100644 app/templates/users/index.hbs create mode 100644 server/mocks/users.js create mode 100644 tests/unit/models/user-test.js create mode 100644 tests/unit/routes/users-test.js create mode 100644 tests/unit/routes/users/index-test.js diff --git a/app/adapters/user.js b/app/adapters/user.js new file mode 100644 index 0000000..d4b9efb --- /dev/null +++ b/app/adapters/user.js @@ -0,0 +1,8 @@ +import DS from 'ember-data'; +import config from '../config/environment'; + +export default DS.RESTAdapter.reopen({ + namespace: 'api', + host: config.apiURL, + coalesceFindRequests: true, +}); diff --git a/app/models/user.js b/app/models/user.js new file mode 100644 index 0000000..c8ec93d --- /dev/null +++ b/app/models/user.js @@ -0,0 +1,10 @@ +import DS from 'ember-data'; + +export default DS.Model.extend({ + email: DS.attr('string'), + name: DS.attr('string'), + role: DS.attr('string'), + createdAt: DS.attr('date'), + updatedAt: DS.attr('date'), + deletedAt: DS.attr('date') +}); diff --git a/app/router.js b/app/router.js index 332bd1e..6616959 100644 --- a/app/router.js +++ b/app/router.js @@ -14,6 +14,7 @@ Router.map(function() { }); }); this.resource('characteristics', function() {}); + this.resource('users', function() {}); }); export default Router; diff --git a/app/routes/users.js b/app/routes/users.js new file mode 100644 index 0000000..5b14286 --- /dev/null +++ b/app/routes/users.js @@ -0,0 +1,4 @@ +import Ember from 'ember'; +import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin'; + +export default Ember.Route.extend(AuthenticatedRouteMixin); diff --git a/app/routes/users/index.js b/app/routes/users/index.js new file mode 100644 index 0000000..b578e0b --- /dev/null +++ b/app/routes/users/index.js @@ -0,0 +1,7 @@ +import Ember from 'ember'; + +export default Ember.Route.extend({ + model: function() { + return this.store.findAll('user'); + } +}); diff --git a/app/templates/users/index.hbs b/app/templates/users/index.hbs new file mode 100644 index 0000000..005836e --- /dev/null +++ b/app/templates/users/index.hbs @@ -0,0 +1,3 @@ +{{#each user in model}} + {{user.email}}
+{{/each}} diff --git a/package.json b/package.json index 6103c89..85b3ab1 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "devDependencies": { "body-parser": "^1.12.2", "broccoli-asset-rev": "^2.0.2", + "connect-restreamer": "^1.0.2", "ember-cli": "0.2.2", "ember-cli-app-version": "0.3.3", "ember-cli-babel": "^4.0.0", @@ -38,7 +39,7 @@ "ember-export-application-global": "^1.0.2", "express": "^4.12.3", "glob": "^4.5.3", - "morgan": "^1.5.2", - "jsonwebtoken": "^4.2.1" + "jsonwebtoken": "^4.2.1", + "morgan": "^1.5.2" } } diff --git a/server/mocks/users.js b/server/mocks/users.js new file mode 100644 index 0000000..a01f5a8 --- /dev/null +++ b/server/mocks/users.js @@ -0,0 +1,75 @@ +module.exports = function(app) { + var express = require('express'); + var usersRouter = express.Router(); + + var USERS = [ + { + id: 1, + email: 'testA', + name: 'Test Admin User', + role: 'A', + createdAt: "0001-01-01T00:00:00Z", + updatedAt: "0001-01-01T00:00:00Z", + deletedAt: null + }, + { + id: 2, + email: 'testR', + name: 'Test Read User', + role: 'R', + createdAt: "0001-01-01T00:00:00Z", + updatedAt: "0001-01-01T00:00:00Z", + deletedAt: null + }, + { + id: 3, + email: 'testW', + name: 'Test Write User', + role: 'W', + createdAt: "0001-01-01T00:00:00Z", + updatedAt: "0001-01-01T00:00:00Z", + deletedAt: null + } + ] + + usersRouter.get('/', function(req, res) { + var users; + if (req.query.ids) { + users = USERS.filter(function(u) { + return req.query.ids.indexOf(u.id.toString()) > -1; + }); + } else { + users = USERS; + } + res.send({ + 'users': users + }); + }); + + usersRouter.post('/', function(req, res) { + res.status(201).end(); + }); + + usersRouter.get('/:id', function(req, res) { + var user = USERS.filter(function(u) { + return u.id == req.params.id; + }); + res.send({ + 'user': user + }); + }); + + usersRouter.put('/:id', function(req, res) { + res.send({ + 'users': { + id: req.params.id + } + }); + }); + + usersRouter.delete('/:id', function(req, res) { + res.status(204).end(); + }); + + app.use('/api/users', usersRouter); +}; diff --git a/tests/unit/models/user-test.js b/tests/unit/models/user-test.js new file mode 100644 index 0000000..4f13832 --- /dev/null +++ b/tests/unit/models/user-test.js @@ -0,0 +1,15 @@ +import { + moduleForModel, + test +} from 'ember-qunit'; + +moduleForModel('user', { + // Specify the other units that are required for this test. + needs: [] +}); + +test('it exists', function(assert) { + var model = this.subject(); + // var store = this.store(); + assert.ok(!!model); +}); diff --git a/tests/unit/routes/users-test.js b/tests/unit/routes/users-test.js new file mode 100644 index 0000000..843f9ae --- /dev/null +++ b/tests/unit/routes/users-test.js @@ -0,0 +1,14 @@ +import { + moduleFor, + test +} from 'ember-qunit'; + +moduleFor('route:users', { + // Specify the other units that are required for this test. + // needs: ['controller:foo'] +}); + +test('it exists', function(assert) { + var route = this.subject(); + assert.ok(route); +}); diff --git a/tests/unit/routes/users/index-test.js b/tests/unit/routes/users/index-test.js new file mode 100644 index 0000000..86efdaf --- /dev/null +++ b/tests/unit/routes/users/index-test.js @@ -0,0 +1,14 @@ +import { + moduleFor, + test +} from 'ember-qunit'; + +moduleFor('route:users/index', { + // Specify the other units that are required for this test. + // needs: ['controller:foo'] +}); + +test('it exists', function(assert) { + var route = this.subject(); + assert.ok(route); +});