Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Got users setup

  • Loading branch information...
commit d0a5124e4a3461120538c1b03902da5976f4b54b 1 parent cd3c0e7
@benjreinhart benjreinhart authored
View
26 components/users/controller.coffee
@@ -1,5 +1,27 @@
+User = require './model'
+UserPresenter = require './presenter'
+
exports.show = (req, res) ->
- res.send 200, 'als;djnadkjsn'
+ {id} = req.params
+
+ User.findById id, (err, user) ->
+ return res.json 500, { message: 'There was an error while trying to find user' } if err?
+ return res.json 404, { message: "No user with id #{ id } found" } unless user?
+
+ renderUser user, 200, res
+
exports.create = (req, res) ->
- res.send 200, 's;ldnasdkjan'
+ (new User req.body.user).save (err, user) ->
+ return res.json 500, { message: 'An error occurred while trying to persist user' } if err?
+
+ renderUser user, 201, res
+
+
+###########
+# PRIVATE #
+###########
+
+renderUser = (user, status, res) ->
+ user = (new UserPresenter user).toHash()
+ res.json status, { user }
View
10 components/users/datastore.coffee
@@ -0,0 +1,10 @@
+{ mongoose, db } = require "#{ process.env.APP_ROOT }/core/db"
+
+UserSchema = new mongoose.Schema
+ email : String
+ lastName : String
+ firstName : String
+ createdAt : { type: Date, default: (-> new Date) }
+ hashedPassword : String
+
+module.exports = db.model('User', UserSchema)
View
32 components/users/model.coffee
@@ -0,0 +1,32 @@
+Base = require "#{ process.env.APP_ROOT }/core/lib/models/base"
+bcrypt = require 'bcrypt'
+UserStore = require './datastore'
+{pick, extend} = require 'underscore'
+
+WHITELISTED_ATTRIBUTES = [
+ 'email'
+ 'lastName'
+ 'firstName'
+]
+
+module.exports = class User extends Base
+ @findById: (id, cb) ->
+ UserStore.findOne { _id: id }, (err, record) ->
+ return cb err if err?
+ return cb() unless record?
+
+ cb null, (new User record)
+
+ constructor: (@attributes={}) ->
+
+ save: (cb) =>
+ {password} = (attributes = @attributes)
+
+ bcrypt.hash password, 10, (err, hash) ->
+ return cb err if err?
+
+ user = extend (whitelist attributes), { hashedPassword: hash }
+ (new UserStore user).save cb
+
+ whitelist = (attributes) ->
+ pick attributes, WHITELISTED_ATTRIBUTES...
View
11 components/users/presenter.coffee
@@ -0,0 +1,11 @@
+module.exports = class User
+ constructor: (@user) ->
+
+ toHash: ->
+ {
+ id : @user.get '_id'
+ email : @user.get 'email'
+ lastName : @user.get 'lastName'
+ createdAt : @user.get 'createdAt'
+ firstName : @user.get 'firstName'
+ }
View
1  package.json
@@ -6,6 +6,7 @@
"author": "Matt Loseke & Ben Reinhart",
"dependencies": {
"async": ">=0.2.6",
+ "bcrypt": ">=0.7.5",
"express": ">=3.1.0",
"js-yaml": ">=2.0.3",
"mongoose": ">=3.5.7",
View
16 scripts/tests/users
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+echo "Creating Promise... Response:"
+echo
+
+curl -d '{
+ "user": {
+ "email": "page@rage.com",
+ "firstName": "Page",
+ "lastName": "McConnell",
+ "password": "password"
+ }
+}' -H 'content-type:application/json' "http://localhost:3000/users"
+
+echo
+echo
Please sign in to comment.
Something went wrong with that request. Please try again.