From 9eeca255cfe41ca4a962422c3703274caa530021 Mon Sep 17 00:00:00 2001 From: Mateja Date: Mon, 3 May 2021 15:34:46 +0200 Subject: Started using controllers. --- controllers/post.js | 30 ++++++++++++++++++++++++++++++ routes/web.js | 29 ++++------------------------- 2 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 controllers/post.js diff --git a/controllers/post.js b/controllers/post.js new file mode 100644 index 0000000..7c66dec --- /dev/null +++ b/controllers/post.js @@ -0,0 +1,30 @@ +const Post = require('../models/post'); + +module.exports = { + + index(req, res) { + Post.find().lean().exec((err, posts) => { + res.render('home', { + title: 'Home Page', + home: true, + posts: posts + }); + }); + }, + + create(req, res) { + res.render('new-post', { + title: 'Make A New Post', + newPost: true + }); + }, + + store(req, res) { + const newPost = new Post({ + 'title': req.body.title, + 'text': req.body.text + }); + newPost.save().then(() => res.redirect('/')); + } + +}; diff --git a/routes/web.js b/routes/web.js index 49b9b85..732c87c 100644 --- a/routes/web.js +++ b/routes/web.js @@ -1,30 +1,9 @@ const express = require('express'); const router = express.Router(); -const Post = require('../models/post'); +const postController = require('../controllers/post'); -router.get('/', (req, res) => { - Post.find().lean().exec((err, posts) => { - res.render('home', { - title: 'Home Page', - home: true, - posts: posts - }); - }); -}); - -router.get('/new-post', (req, res) => { - res.render('new-post', { - title: 'Make A New Post', - newPost: true - }); -}); - -router.post('/new-post', (req, res) => { - const newPost = new Post({ - 'title': req.body.title, - 'text': req.body.text - }); - newPost.save().then(() => res.redirect('/')); -}); +router.get('/', postController.index); +router.get('/new-post', postController.create); +router.post('/new-post', postController.store); module.exports = router; -- cgit v1.2.3