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 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 10a56a9..6e8cd57 100644 --- a/api/routes/books.js +++ b/api/routes/books.js @@ -1,72 +1,49 @@ 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('/', (req, res, next) => { +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) - - books.splice(index, 1, response) - + const response = await Books.findOneAndUpdate({ + _id: req.params.id + },{ + ...req.body + }, { + new: true + }) res.json({ status, response }) }) -router.delete('/:id', (req, res, next) => { +router.delete('/:id', async (req, res, next) => { const status = 200 - const response = books.find(({ id }) => id === req.params.id) - const index = books.indexOf(response) - - books.splice(index, 1) - + const response = await Books.findOneAndDelete({ + _id: req.params.id + }) res.json({ status, response }) }) diff --git a/app.js b/app.js index 075ce75..fc2a831 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!') @@ -17,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) => {