aboutsummaryrefslogtreecommitdiff
path: root/controllers/post.js
blob: 45da525c815ee121ff5d8a936c3c0f97849276ec (plain) (blame)
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
const Post = require('../models/post');

module.exports = {

  index(req, res) {
    Post.find().lean().exec((err, posts) => {
      if (err) console.log(err);
      res.render('home', {
        title: 'Home Page',
        auth: req.isAuthenticated(),
        home: true,
        posts: posts
      });
    });
  },

  create(req, res) {
    res.render('new-post', {
      title: 'Make A New Post',
      auth: req.isAuthenticated(),
      newPost: true
    });
  },

  store(req, res) {
    const newPost = new Post({
      'title': req.body.title,
      'text': req.body.text
    });
    newPost.save()
      .then(() => res.redirect('/'))
      .catch(err => {
        console.log(err);
        res.redirect('/new-post');
      });
  },

  destroy(req, res) {
    Post.findByIdAndRemove(req.params.id, (err, post) => {
      if (err) console.log(err);
      res.redirect('/');
    });
  }

};