Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions api/models/books.js
Original file line number Diff line number Diff line change
@@ -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)
64 changes: 64 additions & 0 deletions api/routes/books.authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const router = require('express').Router({mergeParams: true})
const { generate: generateId } = require('shortid')
const Books = require('../models/books')

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on these routes!

// 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
75 changes: 26 additions & 49 deletions api/routes/books.js
Original file line number Diff line number Diff line change
@@ -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 })
})

Expand Down
6 changes: 5 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand All @@ -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) => {
Expand Down