Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Get all edits by user

  • Loading branch information...
commit cd3c0e7d57db2e995a6655ba5cb6226219719d43 1 parent 1d0f479
@benjreinhart benjreinhart authored
View
29 components/edits/controller.coffee
@@ -1,12 +1,12 @@
-Edit = require './model'
-{extend} = require 'underscore'
-EditPresenter = require './presenter'
+Edit = require './model'
+EditPresenter = require './presenter'
+{extend, isEmpty} = require 'underscore'
exports.create = (req, res) ->
(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() }
+ renderEdit edit, 201, res
exports.show = (req, res) ->
{id} = req.params
@@ -15,5 +15,24 @@ exports.show = (req, res) ->
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() }
+ renderEdit edit, 200, res
+
+exports.userEdits = (req, res) ->
+ {id} = req.params
+
+ Edit.findAllByUserId id, (err, edits) ->
+ return res.json 500, { error: "An error occurred while looking up edits for user with id #{ id }" } if err?
+ return res.json 200, { edits } if isEmpty edits
+
+ edits = ((new EditPresenter edit).toHash() for edit in edits)
+ res.json 200, { edits }
+
+
+###########
+# PRIVATE #
+###########
+
+renderEdit = (edit, status, res) ->
+ edit = (new EditPresenter edit).toHash()
+ res.json status, { edit }
View
13 components/edits/model.coffee
@@ -4,11 +4,18 @@ EditStore = require './datastore'
module.exports = class Edit extends Base
@findById: (id, cb) ->
- EditStore.find({ _id: id }).exec (err, edit) ->
+ EditStore.findOne { _id: id }, (err, record) ->
return cb err if err?
- return cb() if isEmpty edit
+ return cb() unless record?
- cb null, (new Edit edit[0])
+ cb null, (new Edit record)
+
+ @findAllByUserId: (id, cb) ->
+ EditStore.where('userId', id).exec (err, records) ->
+ return cb err if err?
+ edits = ((new Edit record) for record in records)
+
+ cb null, edits ? []
constructor: (@attributes={}) ->
View
1  core/routes.coffee
@@ -11,6 +11,7 @@ Edits = requireController 'edits'
namespace 'users', ({ get, post }) ->
get '/:id', Users.show
+ get '/:id/edits', Edits.userEdits
post Users.create
namespace 'edits', ({ get, post }) ->
Please sign in to comment.
Something went wrong with that request. Please try again.