Skip to content

Commit

Permalink
Refactored, unnested routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Reinhart committed Mar 18, 2013
1 parent aca6312 commit c9b8239
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
12 changes: 5 additions & 7 deletions components/edits/controller.coffee
Expand Up @@ -3,18 +3,16 @@ Edit = require './model'
EditPresenter = require './presenter' EditPresenter = require './presenter'


exports.create = (req, res) -> exports.create = (req, res) ->
attributes = extend req.body.edit, { userId: req.params.userId.toString() } (new Edit req.body.edit).save (err, edit) ->

return res.json 500, { message: 'An error occurred while tryingt to persist edit' } if err?
(new Edit attributes).save (err, edit) ->
return res.json 500, { message: 'fuck it, sorry' } if err?


res.json 201, { edit: (new EditPresenter edit).toHash() } res.json 201, { edit: (new EditPresenter edit).toHash() }


exports.show = (req, res) -> exports.show = (req, res) ->
{id, userId} = req.params {id} = req.params


Edit.findByUserIdAndId userId, id, (err, edit) -> Edit.findById id, (err, edit) ->
console.log edit return res.json 500, { message: 'There was an error while trying to find edit' } if err?
return res.json 404, { message: "No Edit with id #{ id } found" } unless edit? return res.json 404, { message: "No Edit with id #{ id } found" } unless edit?


res.json 200, { edit: (new EditPresenter edit).toHash() } res.json 200, { edit: (new EditPresenter edit).toHash() }
Expand Down
11 changes: 5 additions & 6 deletions components/edits/model.coffee
Expand Up @@ -3,13 +3,12 @@ Base = require "#{ process.env.APP_ROOT }/core/lib/models/base"
EditStore = require './datastore' EditStore = require './datastore'


module.exports = class Edit extends Base module.exports = class Edit extends Base
@findByUserIdAndId: (userId, editId, cb) -> @findById: (id, cb) ->
EditStore EditStore.find({ _id: id }).exec (err, edit) ->
.where('userId', userId) return cb err if err?
.where('_id', editId).exec (err, edit) -> return cb() if isEmpty edit
return cb err if err?


if isEmpty edit then cb() else cb null, (new Edit edit[0]) cb null, (new Edit edit[0])


constructor: (@attributes={}) -> constructor: (@attributes={}) ->


Expand Down
13 changes: 10 additions & 3 deletions core/lib/route_helpers.coffee
@@ -1,4 +1,5 @@
{APP_ROOT} = process.env {APP_ROOT} = process.env
{isFunction} = require 'underscore'


Path = require 'path' Path = require 'path'
app = do require "#{ APP_ROOT }/core/lib/app" app = do require "#{ APP_ROOT }/core/lib/app"
Expand All @@ -12,8 +13,14 @@ namespace = (namespace, func) ->
for verb in ['get', 'post', 'put', 'delete'] for verb in ['get', 'post', 'put', 'delete']
routeMethods[verb] = do (verb) -> routeMethods[verb] = do (verb) ->
(path, cb) -> (path, cb) ->
path = Path.normalize "/#{ namespace }/#{ path }" fullPath = "/#{ namespace }"
app[verb] path, cb
if isFunction path
cb = path
else
fullPath = Path.normalize "#{ fullPath }/#{ path }"

app[verb] fullPath, cb


func(routeMethods) func(routeMethods)


Expand Down
7 changes: 4 additions & 3 deletions core/routes.coffee
Expand Up @@ -11,7 +11,8 @@ Edits = requireController 'edits'


namespace 'users', ({ get, post }) -> namespace 'users', ({ get, post }) ->
get '/:id', Users.show get '/:id', Users.show
get '/:userId/edits/:id', Edits.show post Users.create


post '/', Users.create namespace 'edits', ({ get, post }) ->
post '/:userId/edits', Edits.create get '/:id', Edits.show
post Edits.create
28 changes: 0 additions & 28 deletions scripts/tests/create_edit

This file was deleted.

16 changes: 16 additions & 0 deletions scripts/tests/edits
@@ -0,0 +1,16 @@
#!/bin/bash

echo "Creating Promise... Response:"
echo

curl -d '{
"edit": {
"userId": "123138ur2jd238j",
"original": "Matt",
"modified": "The Dude",
"accepted": false
}
}' -H 'content-type:application/json' "http://localhost:3000/edits"

echo
echo

0 comments on commit c9b8239

Please sign in to comment.