(ctx, next)
| 1 | const service = require('../service/label.service'); |
| 2 | |
| 3 | const verifyLabelExists = async (ctx, next) => { |
| 4 | // 1.取出要添加的所有的标签 |
| 5 | const { labels } = ctx.request.body; |
| 6 | |
| 7 | // 2.判断每一个标签在label表中是否存在 |
| 8 | const newLabels = []; |
| 9 | for (let name of labels) { |
| 10 | const labelResult = await service.getLabelByName(name); |
| 11 | const label = { name }; |
| 12 | if (!labelResult) { |
| 13 | // 创建标签数据 |
| 14 | const result = await service.create(name); |
| 15 | label.id = result.insertId; |
| 16 | } else { |
| 17 | label.id = labelResult.id; |
| 18 | } |
| 19 | newLabels.push(label); |
| 20 | } |
| 21 | ctx.labels = newLabels; |
| 22 | await next(); |
| 23 | } |
| 24 | |
| 25 | module.exports = { |
| 26 | verifyLabelExists |
nothing calls this directly
no test coverage detected