app/routes/authors/author.js   A
last analyzed

Complexity

Total Complexity 20
Complexity/F 1.25

Size

Lines of Code 123
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 20
c 2
b 0
f 0
nc 1
mnd 1
bc 22
fnc 16
dl 0
loc 123
rs 10
bpm 1.375
cpm 1.25
noi 3

5 Functions

Rating   Name   Duplication   Size   Complexity  
A author.js ➔ postAuthor 0 15 1
B author.js ➔ updateAuthor 0 26 1
A author.js ➔ getAuthors 0 9 1
A author.js ➔ getAuthor 0 16 1
A author.js ➔ deleteAuthor 0 22 1
1
'use strict';
2
3
const Author = require('../../models/author');
4
5
/**
6
 * GET /api/authors route to retreive all authors
7
 */
8
9
function getAuthors(req, res) {
10
    // Query the DB and if no errors, send all authors
11
    let query = Author.find({});
12
    query.exec((err, authors) => {
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(authors);
16
    });
17
}
18
19
/**
20
 * POST /api/authors to save a new Author
21
 */
22
23
function postAuthor(req, res) {
24
    // Create a new Author
25
    const newAuthor = new Author(req.body);
26
    //Save it into the DB
27
    newAuthor.save()
28
        .then((author) => {
29
            return res.status(201).send({
30
                message: 'Author successfully added!',
31
                author
32
            });
33
        })
34
        .catch((err) => {
35
            return res.status(400).send(err);
36
        });
37
}
38
39
/**
40
 * GET /api/authors/:id to retrieve an author given its ID
41
 */
42
43
function getAuthor(req, res) {
44
    Author.findById(req.params.id)
45
        .then((author) => {
46
            if (author) {
47
                return res.json(author);
48
            } else {
49
                res.status(404).send({
50
                    message: 'Author not found!',
51
                    id: req.params.id
52
                });
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...
53
            }
54
        })
55
        .catch((err) => {
56
            return res.status(400).send(err);
57
        });
58
}
59
60
/**
61
 * Delete /api/authors/:id to delete an author given its ID
62
 */
63
function deleteAuthor(req, res) {
64
    const query = {
65
        _id: req.params.id
66
    };
67
    Author.remove(query)
68
        .then((result) => {
69
            if (result.result.n === 0) {
70
                return res.status(404).send({
71
                    message: 'Author not found!',
72
                    id: req.params.id
73
                });
74
            } else {
75
                return res.json({
76
                    message: 'Author successfully deleted!',
77
                    result
78
                });
79
            }
80
        })
81
        .catch((err) => {
82
            return res.status(400).send(err);
83
        });
84
}
85
86
/**
87
 * PUT /api/authors/:id to update an author given its ID
88
 */
89
90
function updateAuthor(req, res) {
91
    Author.findById(req.params.id)
92
        .then((author) => {
93
            if (!author) {
94
                return res.status(404).send({
95
                    message: 'Author not found!',
96
                    id: req.params.id
97
                });
98
            } else {
99
                Object.assign(author, req.body)
100
                    .save()
101
                    .then((author) => {
102
                        return res.json({
103
                            message: 'Author updated!',
104
                            author
105
                        });
106
                    })
107
                    .catch((err) => {
108
                        return res.status(500).send(err);
109
                    });
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...
110
            }
111
        })
112
        .catch((err) => {
113
            return res.status(400).send(err);
114
        });
115
}
116
// Export all the functions
117
module.exports = {
118
    getAuthors,
119
    postAuthor,
120
    getAuthor,
121
    deleteAuthor,
122
    updateAuthor
123
};