Passed
Pull Request — main (#12)
by Julia
02:05
created

server/src/models/bike.js   A

Complexity

Total Complexity 6
Complexity/F 1

Size

Lines of Code 71
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 28
mnd 0
bc 0
fnc 6
dl 0
loc 71
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
1
import { db } from "./db.js"
2
3
4
const bike = {
5
    adjTypes(bikeObj) {
6
        const newObj = {
7
            id: bikeObj.id,
8
            city_id: bikeObj.city_id,
9
            status_id: bikeObj.status_id,
10
            status_descr: bikeObj.status_descr,
11
            charge_perc: parseFloat(bikeObj.charge_perc),
12
            coords: JSON.parse(bikeObj.coords),
13
            active: bikeObj.active === 1,
14
        };
15
        return newObj;
16
    },
17
18
    /**
19
     * 
20
     * @param {Int} bikeId 
21
     * @returns {Object}
22
     */
23
    getOne: async function(bikeId) {
24
        const result = await db.queryWithArgs(`CALL single_bike(?);`, [bikeId]);
25
        return this.adjTypes(result[0][0]);
26
    },
27
28
    /**
29
     * 
30
     * @param {Int} bikeId 
31
     * @returns {Object}
32
     */
33
    activate: async function(bikeId) {
34
        const result = await db.queryWithArgs(`CALL activate(?);`, [bikeId]);
35
        return this.adjTypes(result[0][0]);
36
    },
37
38
    /**
39
     * 
40
     * @param {Int} bikeId 
41
     * @returns {Object}
42
     */
43
    deactivate: async function(bikeId) {
44
        const result = await db.queryWithArgs(`CALL deactivate(?);`, [bikeId]);
45
        return this.adjTypes(result[0][0]);
46
    },
47
48
    /**
49
     * 
50
     * @returns {Array}
51
     */
52
    statuses: async function() {
53
        const result = await db.queryNoArgs(`CALL bike_statuses();`);
54
        return result[0];
55
    },
56
57
    /**
58
     * 
59
     * @param {Int} bikeId 
60
     * @param {Int} statusId
61
     * @returns {Object}
62
     */
63
    updStatus: async function(bikeId, statusId) {
64
        const result = await db.queryWithArgs(`CALL upd_bike_status(?, ?);`, [bikeId, statusId]);
65
        return this.adjTypes(result[0][0]);
66
    }
67
68
69
}
70
71
export default bike;