all files / example/modules/blog/actions/ posts.js

7.14% Statements 1/14
0% Branches 0/2
0% Functions 0/6
7.14% Lines 1/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                                                                                         
'use strict'
 
module.exports = [ {
  name: 'createPost',
  description: 'Create a new post',
 
  inputs: {
    title: {required: true},
    content: {required: true}
  },
 
  run: function (api, action, next) {
    var Post = api.models.get('post')
 
    var newPost = new Post(action.params)
 
    // newPost.create('post', params, callback)
 
    newPost.save(function (err) {
      if (err) {
        // return an error message to the client
        next(new Error('We can create that resource!'))
      } else {
        // put the new post available to the response object
        action.response.post = newPost
 
        // finish the action execution
        next()
      }
    })
  }
}, {
  name: 'getPosts',
  description: 'Get all posts',
 
  run: function (api, action, next) {
    api.models.get('post').find({}, function (err, posts) {
      action.response.posts = posts
      next()
    })
  }
},
  {
    name: 'getPost',
    description: 'Get a post',
 
    inputs: {
      id: {required: true}
    },
 
    run: function (api, action, next) {
      // search for the request post on the DB
      api.models.get('post').findById(action.params.id, function (err, post) {
        // put post information in response object
        action.response.post = post
 
        // finish the action execution
        next()
      })
    }
  } ]