From 51ebfd25ca7b59fb894b81c670a253a468e0f82d Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 22 Nov 2016 08:37:02 +0100 Subject: [PATCH] basic author crud --- .gitignore | 1 + app.js | 24 +++++++++++++++++++ controllers/authors.js | 51 +++++++++++++++++++++++++++++++++++++++++ models/authors.js | 9 ++++++++ package.json | 11 +++++++++ views/authors/edit.ejs | 29 +++++++++++++++++++++++ views/authors/index.ejs | 32 ++++++++++++++++++++++++++ views/authors/new.ejs | 27 ++++++++++++++++++++++ views/authors/show.ejs | 34 +++++++++++++++++++++++++++ views/index.ejs | 19 +++++++++++++++ 10 files changed, 237 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 controllers/authors.js create mode 100644 models/authors.js create mode 100644 package.json create mode 100644 views/authors/edit.ejs create mode 100644 views/authors/index.ejs create mode 100644 views/authors/new.ejs create mode 100644 views/authors/show.ejs create mode 100644 views/index.ejs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/app.js b/app.js new file mode 100644 index 0000000..fefbf01 --- /dev/null +++ b/app.js @@ -0,0 +1,24 @@ +var express = require('express'); +var app = express(); +var bodyParser = require('body-parser'); +var mongoose = require('mongoose'); +var methodOverride = require('method-override') + +app.use(methodOverride('_method')); +app.use(bodyParser.urlencoded({extended:false})); + +var authorsController = require('./controllers/authors.js'); +app.use('/authors', authorsController); + +app.get('/', function(req, res){ + res.render('index.ejs'); +}); + +mongoose.connect('mongodb://localhost:27017/blog'); + +mongoose.connection.once('open', function(){ + console.log('connected to mongo'); +}); +app.listen(3000, function(){ + console.log('listening....'); +}); diff --git a/controllers/authors.js b/controllers/authors.js new file mode 100644 index 0000000..4301ebe --- /dev/null +++ b/controllers/authors.js @@ -0,0 +1,51 @@ +var express = require('express'); +var Author = require('../models/authors.js'); +var router = express.Router(); + +router.get('/', function(req, res){ + Author.find({}, function(err, foundAuthors){ + res.render('authors/index.ejs', { + authors: foundAuthors + }); + }) +}); + +router.post('/', function(req, res){ + Author.create(req.body, function(err, createdAuthor){ + res.redirect('/authors'); + }); +}); + +router.get('/new', function(req, res){ + res.render('authors/new.ejs'); +}); + +router.get('/:id', function(req, res){ + Author.findById(req.params.id, function(err, foundAuthor){ + res.render('authors/show.ejs', { + author: foundAuthor + }); + }); +}); + +router.delete('/:id', function(req, res){ + Author.findByIdAndRemove(req.params.id, function(){ + res.redirect('/authors'); + }); +}); + +router.put('/:id', function(req, res){ + Author.findByIdAndUpdate(req.params.id, req.body, function(){ + res.redirect('/authors'); + }); +}); + +router.get('/:id/edit', function(req, res){ + Author.findById(req.params.id, function(err, foundAuthor){ + res.render('authors/edit.ejs', { + author: foundAuthor + }); + }); +}); + +module.exports = router; diff --git a/models/authors.js b/models/authors.js new file mode 100644 index 0000000..673ff6b --- /dev/null +++ b/models/authors.js @@ -0,0 +1,9 @@ +var mongoose = require('mongoose'); + +var authorSchema = mongoose.Schema({ + name: String +}); + +var author = mongoose.model('Author', authorSchema); + +module.exports = author; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8f4845c --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "blog", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/views/authors/edit.ejs b/views/authors/edit.ejs new file mode 100644 index 0000000..edf83f6 --- /dev/null +++ b/views/authors/edit.ejs @@ -0,0 +1,29 @@ + + + + + + + +
+

Edit <%=author.name%>'s Info

+ +
+

Author Attributes:

+
+ + +
+
+
+ + diff --git a/views/authors/index.ejs b/views/authors/index.ejs new file mode 100644 index 0000000..ad6e1ec --- /dev/null +++ b/views/authors/index.ejs @@ -0,0 +1,32 @@ + + + + + + + +
+

Authors

+ +
+
+

List of Authors

+ +
+ + diff --git a/views/authors/new.ejs b/views/authors/new.ejs new file mode 100644 index 0000000..2429278 --- /dev/null +++ b/views/authors/new.ejs @@ -0,0 +1,27 @@ + + + + + + + +
+

Create an Author

+ +
+
+
+ +
+
+ + diff --git a/views/authors/show.ejs b/views/authors/show.ejs new file mode 100644 index 0000000..6e4a01e --- /dev/null +++ b/views/authors/show.ejs @@ -0,0 +1,34 @@ + + + + + + + +
+

Show Page for <%=author.name%>

+ +
+
+

Author Attributes:

+
    +
  • Name: <%=author.name%>
  • +
+ Edit +
+ +
+
+
+
+ + diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..d94ac1e --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,19 @@ + + + + + + + +
+

Welcome to the Blog

+ +
+ +