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' |
|
|
|
|
13
|
|
|
element.status_text = element.status == 1 ? '上架' : '下架' |
|
|
|
|
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) |
|
|
|
|
71
|
|
|
|
72
|
|
|
return updateData |
73
|
|
|
}, |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
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 you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.