Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET routes to book #11

Merged
merged 6 commits into from
Jun 18, 2019
Merged
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
19 changes: 19 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ module.exports = function (app) {
.get(function (req, res){
//response will be array of book objects
//json res format: [{"_id": bookid, "title": book_title, "commentcount": num_of_comments },...]

Book.find({},(err, doc) => {
if(err) console.log(err);
const result = doc.map(book => {
const {_id, title, comments} = book;
const commentcount = comments.length;
return {_id, title, commentcount}
})
res.status(200).json(result);
})
})

.post(function (req, res){
Expand Down Expand Up @@ -50,6 +60,15 @@ module.exports = function (app) {
.get(function (req, res){
var bookid = req.params.id;
//json res format: {"_id": bookid, "title": book_title, "comments": [comment,comment,...]}
Book.findById(bookid, '_id title comments', (err, doc) => {
if(err) {
console.log(err);
}else if(!doc){
res.status(500).send('no book exists');
}else{
res.status(200).json(doc);
}
})
})

.post(function(req, res){
Expand Down
42 changes: 35 additions & 7 deletions tests/2_functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var chaiHttp = require('chai-http');
var chai = require('chai');
var assert = chai.assert;
var server = require('../server');
var bookid = 0;

chai.use(chaiHttp);

Expand Down Expand Up @@ -47,6 +48,7 @@ suite('Functional Tests', function() {
title: 'This is my first book!'
})
.end(function(err, res) {
bookid = res.body._id;
assert.equal(res.status, 200);
assert.exists(res.body._id);
assert.equal(res.body.title, 'This is my first book!');
Expand Down Expand Up @@ -74,22 +76,48 @@ suite('Functional Tests', function() {
suite('GET /api/books => array of books', function(){

test('Test GET /api/books', function(done){
//done();
chai.request(server)
.get('/api/books')
.end(function(err, res){
assert.equal(res.status, 200);
assert.isArray(res.body, 'response should be an array');
assert.property(res.body[0], 'commentcount', 'Books in array should contain commentcount');
assert.property(res.body[0], 'title', 'Books in array should contain title');
assert.property(res.body[0], '_id', 'Books in array should contain _id');
assert.equal(res.body[0].title, 'This is my first book!')
assert.equal(res.body[0].commentcount, 0);
done();
});
});

});


suite('GET /api/books/[id] => book object with [id]', function(){

test('Test GET /api/books/[id] with id not in db', function(done){
//done();
chai.request(server)
.get('/api/books/B4Di6fjyh76f')
.end(function(err, res){
assert.equal(res.status, 500);
assert.equal(res.text, 'no book exists')
done();
});
});

test('Test GET /api/books/[id] with valid id in db', function(done){
//done();
chai.request(server)
.get(`/api/books/${bookid}`)
.end(function(err, res){
assert.equal(res.status, 200);
assert.property(res.body, 'comments', 'Books in array should contain commentcount');
assert.property(res.body, 'title', 'Books in array should contain title');
assert.property(res.body, '_id', 'Books in array should contain _id');
assert.equal(res.body.title, 'This is my first book!')
assert.isArray(res.body.comments);
assert.equal(res.body._id, bookid)
done();
});
});
});


Expand All @@ -101,6 +129,6 @@ suite('Functional Tests', function() {

});

});
});

});