|
1
|
|
|
const ModelCategory = require('../../model/catering/category') |
|
2
|
|
|
const ApiError = require('../../util/api_error') |
|
3
|
|
|
const Validate = require('request-validate') |
|
4
|
|
|
const _ = require('underscore') |
|
5
|
|
|
|
|
6
|
|
|
module.exports = { |
|
7
|
|
|
list: async function (storeId, filter = {}) { |
|
8
|
|
|
let result = await ModelCategory.list(storeId, filter) |
|
9
|
|
|
return result |
|
10
|
|
|
}, |
|
11
|
|
|
|
|
12
|
|
|
detail: async function (storeId, categoryId) { |
|
13
|
|
|
let result = await ModelCategory.first(categoryId) |
|
14
|
|
|
if (_.isEmpty(result)) { |
|
15
|
|
|
throw new ApiError('common.notExist', 'category') |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if (result.store_id != storeId) { |
|
19
|
|
|
throw new ApiError('common.notExist', 'category') |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
if (result.status == -1) { |
|
23
|
|
|
throw new ApiError('common.notExist', 'category') |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return result |
|
27
|
|
|
}, |
|
28
|
|
|
|
|
29
|
|
|
add: async function (storeId, data) { |
|
30
|
|
|
Validate(data, { |
|
31
|
|
|
name: 'required', |
|
32
|
|
|
}) |
|
33
|
|
|
|
|
34
|
|
|
let maxSortData = await ModelCategory.getMaxSort(storeId) |
|
35
|
|
|
let maxSort = 0 |
|
36
|
|
|
if (!_.isEmpty(maxSortData)) maxSort = parseInt(maxSortData.sort) |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
let insertData = { |
|
39
|
|
|
store_id: storeId, |
|
40
|
|
|
name: data.name, |
|
41
|
|
|
sort: maxSort + 1, |
|
42
|
|
|
} |
|
43
|
|
|
let insertResult = await ModelCategory.add(insertData) |
|
44
|
|
|
insertData.id = insertResult.insertId |
|
45
|
|
|
|
|
46
|
|
|
return insertData |
|
47
|
|
|
}, |
|
48
|
|
|
|
|
49
|
|
|
edit: async function (storeId, id, data) { |
|
50
|
|
|
let detail = await this.detail(storeId, id) |
|
51
|
|
|
|
|
52
|
|
|
let where = { |
|
53
|
|
|
id: id |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
let updateData = { |
|
57
|
|
|
name: _.has(data, 'name') ? data.name : detail.name, |
|
58
|
|
|
status: _.has(data, 'status') ? data.status : detail.status, |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
let editResult = await ModelCategory.edit(updateData, where) |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return updateData |
|
64
|
|
|
}, |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
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 = 42will 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.