Code Duplication    Length = 97-97 lines in 2 locations

v2/models/order_items.js 1 location

@@ 1-97 (lines=97) @@
1
const db = require("../db/database.js");
2
3
module.exports = (function () {
4
    function addOrderItem(res, body) {
5
        db.run("INSERT INTO order_items (orderId, productId, amount, apiKey) VALUES (?, ?, ?, ?)",
6
            body.order_id,
7
            body.product_id,
8
            body.amount,
9
            body.api_key, (err) => {
10
                if (err) {
11
                    return res.status(500).json({
12
                        errors: {
13
                            status: 500,
14
                            source: "POST /order_item",
15
                            title: "Database error",
16
                            detail: err.message
17
                        }
18
                    });
19
                } else {
20
                    res.status(201).json({ data: body });
21
                }
22
            });
23
    }
24
25
    function updateOrderItem(res, body) {
26
        if (Number.isInteger(parseInt(body.order_id)) &&
27
            Number.isInteger(parseInt(body.product_id))) {
28
            db.run("UPDATE order_items SET orderId = ?, productId = ?, amount = ?" +
29
                " WHERE apiKey = ? AND orderId = ? AND productId = ?",
30
            body.order_id,
31
            body.product_id,
32
            body.amount,
33
            body.api_key,
34
            body.order_id,
35
            body.product_id, (err) => {
36
                if (err) {
37
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "PUT /order_item",
41
                            title: "Database error",
42
                            detail: err.message
43
                        }
44
                    });
45
                } else {
46
                    res.status(204).send();
47
                }
48
            });
49
        } else {
50
            res.status(400).json({
51
                errors: {
52
                    status: 400,
53
                    detail: "Required attributes order id and product id" +
54
                        " was not included in the request."
55
                }
56
            });
57
        }
58
    }
59
60
    function deleteOrderItem(res, body) {
61
        if (Number.isInteger(parseInt(body.order_id)) &&
62
            Number.isInteger(parseInt(body.product_id))) {
63
            db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ? AND productId = ?",
64
                body.api_key,
65
                body.order_id,
66
                body.product_id, (err) => {
67
                    if (err) {
68
                        return res.status(500).json({
69
                            errors: {
70
                                status: 500,
71
                                source: "DELETE /order_item",
72
                                title: "Database error",
73
                                detail: err.message
74
                            }
75
                        });
76
                    } else {
77
                        res.status(204).send();
78
                    }
79
                });
80
        } else {
81
            res.status(400).json({
82
                errors: {
83
                    status: 400,
84
                    detail: "Required attributes order id and product id" +
85
                        " was not included in the request."
86
                }
87
            });
88
        }
89
    }
90
91
    return {
92
93
        addOrderItem: addOrderItem,
94
        updateOrderItem: updateOrderItem,
95
        deleteOrderItem: deleteOrderItem,
96
    };
97
}());
98

v1/models/order_items.js 1 location

@@ 1-97 (lines=97) @@
1
const db = require("../db/database.js");
2
3
module.exports = (function () {
4
    function addOrderItem(res, body) {
5
        db.run("INSERT INTO order_items (orderId, productId, amount, apiKey) VALUES (?, ?, ?, ?)",
6
            body.order_id,
7
            body.product_id,
8
            body.amount,
9
            body.api_key, (err) => {
10
                if (err) {
11
                    return res.status(500).json({
12
                        errors: {
13
                            status: 500,
14
                            source: "POST /order_item",
15
                            title: "Database error",
16
                            detail: err.message
17
                        }
18
                    });
19
                } else {
20
                    res.status(201).json({ data: body });
21
                }
22
            });
23
    }
24
25
    function updateOrderItem(res, body) {
26
        if (Number.isInteger(parseInt(body.order_id)) &&
27
            Number.isInteger(parseInt(body.product_id))) {
28
            db.run("UPDATE order_items SET orderId = ?, productId = ?, amount = ?" +
29
                " WHERE apiKey = ? AND orderId = ? AND productId = ?",
30
            body.order_id,
31
            body.product_id,
32
            body.amount,
33
            body.api_key,
34
            body.order_id,
35
            body.product_id, (err) => {
36
                if (err) {
37
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "PUT /order_item",
41
                            title: "Database error",
42
                            detail: err.message
43
                        }
44
                    });
45
                } else {
46
                    res.status(204).send();
47
                }
48
            });
49
        } else {
50
            res.status(400).json({
51
                errors: {
52
                    status: 400,
53
                    detail: "Required attributes order id and product id" +
54
                        " was not included in the request."
55
                }
56
            });
57
        }
58
    }
59
60
    function deleteOrderItem(res, body) {
61
        if (Number.isInteger(parseInt(body.order_id)) &&
62
            Number.isInteger(parseInt(body.product_id))) {
63
            db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ? AND productId = ?",
64
                body.api_key,
65
                body.order_id,
66
                body.product_id, (err) => {
67
                    if (err) {
68
                        return res.status(500).json({
69
                            errors: {
70
                                status: 500,
71
                                source: "DELETE /order_item",
72
                                title: "Database error",
73
                                detail: err.message
74
                            }
75
                        });
76
                    } else {
77
                        res.status(204).send();
78
                    }
79
                });
80
        } else {
81
            res.status(400).json({
82
                errors: {
83
                    status: 400,
84
                    detail: "Required attributes order id and product id" +
85
                        " was not included in the request."
86
                }
87
            });
88
        }
89
    }
90
91
    return {
92
93
        addOrderItem: addOrderItem,
94
        updateOrderItem: updateOrderItem,
95
        deleteOrderItem: deleteOrderItem,
96
    };
97
}());
98