app/routes/books/book.js   A
last analyzed

Complexity

Total Complexity 20
Complexity/F 1.82

Size

Lines of Code 121
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 121
rs 10
wmc 20
mnd 2
bc 23
fnc 11
bpm 2.0909
cpm 1.8181
noi 2

5 Functions

Rating   Name   Duplication   Size   Complexity  
A book.js ➔ postBook 0 16 1
B book.js ➔ updateBook 0 25 1
A book.js ➔ deleteBook 0 20 1
A book.js ➔ getBook 0 15 1
A book.js ➔ getBooks 0 9 1
1
'use strict';
2
3
const Book = require('../../models/book');
4
5
/**
6
 * GET /book route to retrieve all the books
7
 */
8
9
function getBooks(req, res) {
10
    // Query the DB and if no errors, send all the books
11
    let query = Book.find({});
12
    query.exec((err, books) => {
13
        if (err) res.send(err);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
        //If no errors, send them back to the clients
15
        res.json(books);
16
    });
17
}
18
19
/**
20
 * POST /book to save a new book
21
 */
22
23
function postBook(req, res) {
24
    // Create a new book
25
    const newBook = new Book(req.body);
26
    // Save it into the DB
27
    newBook.save((err, book) => {
28
        if (err) {
29
            res.send(err);
30
        } else {
31
            // If no erros, send it back to the clients
32
            res.send({
33
                message: 'Book successfully added!',
34
                book
35
            });
36
        }
37
    });
38
}
39
40
/**
41
 * GET /book/:id route to retrieve a book given its id.
42
 */
43
function getBook(req, res) {
44
    Book.findById(req.params.id, (err, book) => {
45
        if (err) {
46
            return res.status(400).send(err);
47
        }
48
        if (book) {
49
            return res.send(book);
50
        } else {
51
            return res.status(404).send({
52
                message: 'Book not found!',
53
                id: req.params.id
54
            });
55
        }
56
    });
57
}
58
59
/**
60
 * DELETE /book/:id to delete a book given its id.
61
 */
62
function deleteBook(req, res) {
63
    Book.remove({
64
        _id: req.params.id
65
    }, (err, result) => {
66
        if (err) {
67
            return res.status(400).send(err);
68
        }
69
        if (result.result.n === 0) {
70
            return res.status(404).send({
71
                message: 'Book not found!',
72
                id: req.params.id
73
            });
74
        } else {
75
            return res.json({
76
                message: 'Book successfully deleted!',
77
                result
78
            });
79
        }
80
    });
81
}
82
83
/**
84
 * PUT /book/:id to update a book given its id
85
 */
86
87
function updateBook(req, res) {
88
    Book.findById({
89
        _id: req.params.id
90
    }, (err, book) => {
91
        if (err) {
92
            return res.status(400).send(err);
93
        }
94
        if (!book) {
95
            return res.status(404).send({
96
                message: 'Book not found!',
97
                id: req.params.id
98
            });
99
        } else {
100
            Object.assign(book, req.body).save((err, book) => {
101
                if (err) {
102
                    return res.status(500).send(err);
103
                }
104
                return res.json({
105
                    message: 'Book updated!',
106
                    book
107
                });
108
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
109
        }
110
    });
111
}
112
113
// Export all the functions 
114
115
module.exports = {
116
    getBooks,
117
    postBook,
118
    getBook,
119
    deleteBook,
120
    updateBook
121
};