From 29af288c439d13349dbe1a859a149e8ac4532a9d Mon Sep 17 00:00:00 2001 From: Colin Griffiths Date: Mon, 15 Jul 2019 23:53:50 -0700 Subject: [PATCH 1/3] Adding books model with subdocument validation --- api/models/books.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 api/models/books.js diff --git a/api/models/books.js b/api/models/books.js new file mode 100644 index 0000000..f5cf6ed --- /dev/null +++ b/api/models/books.js @@ -0,0 +1,36 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const booksSchema = new Schema({ + title: { + type: String, + required: true, + }, + published: { + type: Number, + required: true + }, + authors: [ + { + name: { + type: String, + required: true + }, + dob: { + type: String, + validate: { + validator: (input) => { + return /\d{2}-\d{2}-\d{4}/.test(input); + }, + msg: `dob must be a string in the format mm-dd-yyyy` + } + } + } + ] +}, +{ + timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } +} +) + +module.exports = mongoose.model('Books', booksSchema) \ No newline at end of file From c2bba791f6b14f1370161afc569cc74e513b18a1 Mon Sep 17 00:00:00 2001 From: Colin Griffiths Date: Tue, 16 Jul 2019 15:54:26 -0700 Subject: [PATCH 2/3] Creating first two author GET routes --- api/routes/books.js | 89 +++++++++++++++++++++++++++++++++------------ app.js | 5 ++- 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/api/routes/books.js b/api/routes/books.js index 10a56a9..cc52e28 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,7 +1,8 @@ const router = require('express').Router() const { generate: generateId } = require('shortid') +const Books = require('../models/books') -const books = [ +/* const books = [ { id: 'j9U3iNIQi', title: 'The Colour of Magic', @@ -24,50 +25,90 @@ const books = [ } ] } -]; +]; */ -router.get('/', (req, res, next) => { +// Books + +router.get('/', async (req, res, next) => { const status = 200 - const response = books - + const response = await Books.find() res.json({ status, response }) }) -router.post('/', (req, res, next) => { +router.post('/', async (req, res, next) => { const status = 201 - - books.push({ id: generateId(), ...req.body }) - const response = books - - res.json({ status, response }) + await Books.create(req.body) + .then(response => { + res.json({ status, response }) + }) + .catch(err => { + res.json(err.message) + }) }) -router.get('/:id', (req, res, next) => { +router.get('/:id', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - + const response = await Books.findById(req.params.id) res.json({ status, response }) }) -router.put('/:id', (req, res, next) => { +router.put('/:id', async (req, res, next) => { const status = 200 - const response = { id: req.params.id, ...req.body } - const single = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(single) + const response = await Books.findOneAndUpdate({ + _id: req.params.id + },{ + ...req.body + }, { + new: true + }) + res.json({ status, response }) +}) - books.splice(index, 1, response) - +router.delete('/:id', async (req, res, next) => { + const status = 200 + const response = await Books.findOneAndDelete({ + _id: req.params.id + }) res.json({ status, response }) }) -router.delete('/:id', (req, res, next) => { +router.get('/:id/authors', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) + const response = await Books.findById(req.params.id) + const authors = response.authors; + res.json({ status, authors }) +}) - books.splice(index, 1) +router.get('/:id/authors/:authorId', async (req, res, next) => { + const status = 200 + const response = await Books.findById(req.params.id) + const author = response.authors.id(req.params.authorId) + console.log(author) + res.json({ status, author }) +}) + +//TODO +router.post('/:id/authors', async (req, res, next) => { + const status = 201 + await Books.create(req.body) + .then(response => { + res.json({ status, response }) + }) + .catch(err => { + res.json(err.message) + }) +}) +//TODO +router.put('/:id/authors/:authorId', async (req, res, next) => { + const status = 200 + const response = await Books.findById(req.params.id) + response. res.json({ status, response }) }) +//TODO +router.delete('/:id/authors/:authorId', (req, res, next) => {}) + + module.exports = router \ No newline at end of file diff --git a/app.js b/app.js index 075ce75..04d56bb 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,10 @@ const app = express() // Database Connection if (MONGO_DB_CONNECTION) { - mongoose.connect(MONGO_DB_CONNECTION, { useNewUrlParser: true }) + mongoose.connect(MONGO_DB_CONNECTION, { + useNewUrlParser: true, + useFindAndModify: false + }); console.log('Connected to database...') } else { console.log('Could not connect to database!') From b7b91dae05aeb405eaef8115433d25f1d15849f9 Mon Sep 17 00:00:00 2001 From: Colin Griffiths Date: Tue, 16 Jul 2019 21:54:29 -0700 Subject: [PATCH 3/3] Final routes for Authors --- api/routes/books.authors.js | 64 +++++++++++++++++++++++++++++++++++++ api/routes/books.js | 64 ------------------------------------- app.js | 1 + 3 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 api/routes/books.authors.js diff --git a/api/routes/books.authors.js b/api/routes/books.authors.js new file mode 100644 index 0000000..285e9f5 --- /dev/null +++ b/api/routes/books.authors.js @@ -0,0 +1,64 @@ +const router = require('express').Router({mergeParams: true}) +const { generate: generateId } = require('shortid') +const Books = require('../models/books') + +// Authors + +router.get('/', async (req, res, next) => { + const status = 200 + const Book = await Books.findById(req.params.bookId) + .then(Book => { + const authors = Book.authors; + res.json({ status, authors }) + }) + .catch (error => { + res.json({status, error}) + }) + + +}) + +router.get('/:authorId', async (req, res, next) => { + const status = 200 + const response = await Books.findById(req.params.bookId) + const author = response.authors.id(req.params.authorId) + res.json({ status, author }) +}) + +router.post('/', async (req, res, next) => { + const status = 201 + const Book = await Books.findById(req.params.bookId) + Book.authors.push(req.body) + const author = Book.authors[Book.authors.length-1] + Book.save(function (err) { + if (err) return console.log(err) + console.log('Author created successfully!'); + }); + res.json({status, author}) +}) + +router.put('/:authorId', async (req, res, next) => { + const status = 200 + const Book = await Books.findById(req.params.bookId) + const author = Book.authors.id(req.params.authorId) + author.set(req.body) + Book.save(function (err) { + if (err) return console.log(err) + console.log('Author updated successfully!'); + }); + res.json({status, author}) +}) + +router.delete('/:authorId', async (req, res, next) => { + const status = 200 + const Book = await Books.findById(req.params.bookId) + const author = Book.authors.id(req.params.authorId).remove() + Book.save(function (err) { + if (err) return handleError(err); + console.log('Author removed successfully'); + }); + res.json({ status, author }) + +}) + +module.exports = router \ No newline at end of file diff --git a/api/routes/books.js b/api/routes/books.js index cc52e28..6e8cd57 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -2,31 +2,6 @@ const router = require('express').Router() const { generate: generateId } = require('shortid') const Books = require('../models/books') -/* const books = [ - { - id: 'j9U3iNIQi', - title: 'The Colour of Magic', - published: 1983, - authors: [ - { - name: 'Sir Terry Pratchett', - dob: '04-28-1948' - } - ] - }, - { - id: 'ubQnXOfJV', - title: 'Stardust', - published: 1997, - authors: [ - { - name: 'Neil Gaiman', - dob: '11-10-1960' - } - ] - } -]; */ - // Books router.get('/', async (req, res, next) => { @@ -72,43 +47,4 @@ router.delete('/:id', async (req, res, next) => { res.json({ status, response }) }) -router.get('/:id/authors', async (req, res, next) => { - const status = 200 - const response = await Books.findById(req.params.id) - const authors = response.authors; - res.json({ status, authors }) -}) - -router.get('/:id/authors/:authorId', async (req, res, next) => { - const status = 200 - const response = await Books.findById(req.params.id) - const author = response.authors.id(req.params.authorId) - console.log(author) - res.json({ status, author }) -}) - -//TODO -router.post('/:id/authors', async (req, res, next) => { - const status = 201 - await Books.create(req.body) - .then(response => { - res.json({ status, response }) - }) - .catch(err => { - res.json(err.message) - }) -}) - -//TODO -router.put('/:id/authors/:authorId', async (req, res, next) => { - const status = 200 - const response = await Books.findById(req.params.id) - response. - res.json({ status, response }) -}) - -//TODO -router.delete('/:id/authors/:authorId', (req, res, next) => {}) - - module.exports = router \ No newline at end of file diff --git a/app.js b/app.js index 04d56bb..fc2a831 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,7 @@ app.use(require('body-parser').json()) // Routes app.use('/api/books', require('./api/routes/books')) +app.use('/api/books/:bookId/authors', require('./api/routes/books.authors')) // Not Found Handler app.use((req, res, next) => {