{"size": 630, "ext": "js", "max_stars_repo_path": "_/Chapter06/Server/api/controllers/posts.js", "max_stars_repo_name": "paullewallencom/javascript-978-1-7882-9370-9", "max_stars_repo_stars_event_min_datetime": "2017-09-11T13:43:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-08T04:42:53.000Z", "max_issues_repo_path": "Chapter06/Server/api/controllers/posts.js", "max_issues_repo_name": "MabelM1/JavaScript-by-Example", "max_issues_repo_issues_event_min_datetime": "2017-09-19T05:06:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-20T02:37:32.000Z", "max_forks_repo_path": "Chapter06/Server/api/controllers/posts.js", "max_forks_repo_name": "MabelM1/JavaScript-by-Example", "max_forks_repo_forks_event_min_datetime": "2017-09-01T01:59:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-27T12:10:18.000Z"}
{"max_stars_count": 54, "max_issues_count": 11, "max_forks_count": 93, "avg_line_length": 21.0, "max_line_length": 61, "alphanum_fraction": 0.5682539683}
module.exports = {
  posts,
  author,
};

const db = require('../helpers/db').db;
const _ = require('lodash');

function posts(req, res) {
    const posts = db.get('posts').value();

    setTimeout(() => {
      res.status(200).json(posts);
    }, 3000);
}

function author(req, res) {
    const author = decodeURI(req.swagger.params.name.value);
    const allPosts = db.get('posts').value();
    const posts = _.filter(allPosts, {author});

    setTimeout(() => {
      if(posts.length) {
          res.status(200).json(posts);
      } else {
          res.status(400).json({message: 'No Posts found!'});
      }
    }, 3000);
}
