Passed
Push — master ( 1adf40...f067da )
by lv
01:05
created

module.exports.edit   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 1
dl 0
loc 19
rs 8.6666
nop 3
1
const ModelProduct = require('../../model/catering/product')
2
const ApiError = require('../../util/api_error')
3
const Validate = require('request-validate')
4
const _ = require('underscore')
5
const ConfigOss = require('../../../config/oss')
6
7
module.exports = {
8
    list: async function (storeId, filter = {}) {
9
        let result = await ModelProduct.list(storeId, filter)
10
        result.forEach(element => {
11
            element.preview_thumb = ''
12
            if (element.thumb) element.preview_thumb = ConfigOss.catering.view_server + element.thumb + '?x-oss-process=style/preview'
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...
13
            element.status_text = element.status == 1 ? '上架' : '下架'
0 ignored issues
show
Best Practice introduced by
Comparing element.status to 1 using the == operator is not safe. Consider using === instead.
Loading history...
14
        })
15
        return result
16
    },
17
18
    detail: async function (storeId, id) {
19
        let result = await ModelProduct.first(id)
20
        if (_.isEmpty(result)) {
21
            throw new ApiError('common.notExist', 'product')
22
        }
23
24
        if (result.store_id != storeId) {
25
            throw new ApiError('common.notExist', 'product')
26
        }
27
28
        if (result.status == -1) {
29
            throw new ApiError('common.notExist', 'product')
30
        }
31
        
32
        return result
33
    },
34
35
    add: async function (storeId, data) {
36
        Validate(data, {
37
            title: 'required',
38
            thumb: 'required',
39
            description: 'required',
40
            price: 'required',
41
        })
42
        let insertData = {
43
            title: data.title,
44
            thumb: data.thumb,
45
            description: data.description,
46
            price: data.price,
47
            store_id: storeId
48
        }
49
        let insertResult = await ModelProduct.add(insertData)
50
        insertData.id = insertResult.insertId
51
52
        return insertData
53
    },
54
55
    edit: async function (storeId, id, data) {
56
        let detail = await this.detail(storeId, id)
57
58
        let where = {
59
            id: id
60
        }
61
62
        let updateData = {
63
            title: _.has(data, 'title') ? data.title : detail.title,
64
            thumb: _.has(data, 'thumb') ? data.thumb : detail.thumb,
65
            description: _.has(data, 'description') ? data.description : detail.description,
66
            price: _.has(data, 'price') ? data.price : detail.price,
67
            status: _.has(data, 'status') ? data.status : detail.status
68
        }
69
70
        let editResult = await ModelProduct.edit(updateData, where)
0 ignored issues
show
Unused Code introduced by
The variable editResult seems to be never used. Consider removing it.
Loading history...
71
72
        return updateData
73
    },
74
75
}
76