filo_caspar/api/preset/routes.js

51 lines
1 KiB
JavaScript
Raw Normal View History

2016-04-14 04:01:51 +00:00
import Preset from './model'
2017-12-03 11:34:43 +00:00
export async function all(ctx, payload) {
2016-04-14 04:01:51 +00:00
let id = Number(payload.graphic_id || payload.id)
2018-06-26 18:35:12 +00:00
let data = await Preset.getAll({ graphic_id: id }, [], 'sort')
2016-04-14 04:01:51 +00:00
2017-12-03 11:34:43 +00:00
ctx.io.emit(`preset.all:${id}`, data.toJSON())
2016-04-14 04:01:51 +00:00
}
export async function add(ctx, payload) {
payload.is_deleted = false
payload.sort = 1
let last = await Preset.query(q => {
q.where({ graphic_id: payload.graphic_id })
q.orderBy('sort', 'desc')
q.limit(1)
}).fetch({ require: false })
if (last) {
payload.sort = last.get('sort') + 1
}
await Preset.create(payload)
2017-12-03 11:34:43 +00:00
await all(ctx, payload)
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
export async function patch(ctx, payload) {
await Promise.all(payload.map(async item => {
let preset = await Preset.getSingle(item.id)
preset.set({ sort: item.sort })
await preset.save()
}))
await all(ctx, payload[0])
}
2016-04-14 04:01:51 +00:00
export async function remove(ctx, payload) {
let preset = await Preset.getSingle(payload.id)
preset.set('is_deleted', true)
await preset.save()
2017-12-03 11:34:43 +00:00
await all(ctx, payload)
2016-04-14 04:01:51 +00:00
}