Permalink
Browse files

Refactored, unnested routes

  • Loading branch information...
1 parent aca6312 commit c9b8239629f0823e523f499eb1aa24ac1281c099 @benjreinhart benjreinhart committed Mar 18, 2013
@@ -3,18 +3,16 @@ Edit = require './model'
EditPresenter = require './presenter'
exports.create = (req, res) ->
- attributes = extend req.body.edit, { userId: req.params.userId.toString() }
-
- (new Edit attributes).save (err, edit) ->
- return res.json 500, { message: 'fuck it, sorry' } if err?
+ (new Edit req.body.edit).save (err, edit) ->
+ return res.json 500, { message: 'An error occurred while tryingt to persist edit' } if err?
res.json 201, { edit: (new EditPresenter edit).toHash() }
exports.show = (req, res) ->
- {id, userId} = req.params
+ {id} = req.params
- Edit.findByUserIdAndId userId, id, (err, edit) ->
- console.log edit
+ Edit.findById id, (err, 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?
res.json 200, { edit: (new EditPresenter edit).toHash() }
@@ -3,13 +3,12 @@ Base = require "#{ process.env.APP_ROOT }/core/lib/models/base"
EditStore = require './datastore'
module.exports = class Edit extends Base
- @findByUserIdAndId: (userId, editId, cb) ->
- EditStore
- .where('userId', userId)
- .where('_id', editId).exec (err, edit) ->
- return cb err if err?
+ @findById: (id, cb) ->
+ EditStore.find({ _id: id }).exec (err, edit) ->
+ return cb err if err?
+ return cb() if isEmpty edit
- if isEmpty edit then cb() else cb null, (new Edit edit[0])
+ cb null, (new Edit edit[0])
constructor: (@attributes={}) ->
@@ -1,4 +1,5 @@
-{APP_ROOT} = process.env
+{APP_ROOT} = process.env
+{isFunction} = require 'underscore'
Path = require 'path'
app = do require "#{ APP_ROOT }/core/lib/app"
@@ -12,8 +13,14 @@ namespace = (namespace, func) ->
for verb in ['get', 'post', 'put', 'delete']
routeMethods[verb] = do (verb) ->
(path, cb) ->
- path = Path.normalize "/#{ namespace }/#{ path }"
- app[verb] path, cb
+ fullPath = "/#{ namespace }"
+
+ if isFunction path
+ cb = path
+ else
+ fullPath = Path.normalize "#{ fullPath }/#{ path }"
+
+ app[verb] fullPath, cb
func(routeMethods)
View
@@ -11,7 +11,8 @@ Edits = requireController 'edits'
namespace 'users', ({ get, post }) ->
get '/:id', Users.show
- get '/:userId/edits/:id', Edits.show
+ post Users.create
- post '/', Users.create
- post '/:userId/edits', Edits.create
+namespace 'edits', ({ get, post }) ->
+ get '/:id', Edits.show
+ post Edits.create
View
@@ -1,28 +0,0 @@
-#!/bin/bash
-
-echo "Creating Promise... Response:"
-echo
-
-curl -d '{
- "edit": {
- "original": "Matt",
- "modified": "The Dude",
- "accepted": false
- }
-}' -H 'content-type:application/json' "http://localhost:3000/users/1/edits"
-
-echo
-echo
-
-# echo "Creating Promise... Response:"
-# echo
-
-# curl -d '{
-# "promise": {
-# "expiresAt": "Sun Mar 03 2013 13:30:04 GMT-0800 (PST)",
-# "description": "Blackmail walker"
-# }
-# }' -H 'content-type:application/json' "http://localhost:3000/api/users/124267827/promises"
-
-# echo
-# echo
View
@@ -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.