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

server/src/models/bike.js   A

Complexity

Total Complexity 5
Complexity/F 1

Size

Lines of Code 59
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
mnd 0
bc 0
fnc 5
dl 0
loc 59
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
    /**
6
     * 
7
     * @param {Int} bikeId 
8
     * @returns {Object}
9
     */
10
    getOne: async function(bikeId) {
11
        const result = await db.queryWithArgs(`CALL single_bike(?);`, [bikeId]);
12
        return this.adjTypes(result[0][0]);
13
    },
14
15
    /**
16
     * 
17
     * @param {Int} bikeId 
18
     * @returns {Object}
19
     */
20
    activate: async function(bikeId) {
21
        const result = await db.queryWithArgs(`CALL activate(?);`, [bikeId]);
22
        return this.adjTypes(result[0][0]);
23
    },
24
25
    /**
26
     * 
27
     * @param {Int} bikeId 
28
     * @returns {Object}
29
     */
30
    deactivate: async function(bikeId) {
31
        const result = await db.queryWithArgs(`CALL deactivate(?);`, [bikeId]);
32
        return this.adjTypes(result[0][0]);
33
    },
34
35
    /**
36
     * 
37
     * @param {Int} bikeId 
38
     * @returns {Object}
39
     */
40
    statuses: async function() {
41
        const result = await db.queryWithArgs(`CALL bike_statuses();`);
42
        return result[0];
43
    },
44
45
    adjTypes(bikeObj) {
46
        const newObj = {
47
            id: bikeObj.id,
48
            city_id: bikeObj.city_id,
49
            status_id: bikeObj.status_id,
50
            status_descr: bikeObj.status_descr,
51
            charge_perc: parseFloat(bikeObj.charge_perc),
52
            coords: JSON.parse(bikeObj.coords),
53
            active: bikeObj.active === 1,
54
        };
55
        return newObj;
56
    }
57
}
58
59
export default bike;