2020-04-06 22:47:58 +00:00
|
|
|
import Schedule from './model.mjs'
|
2018-06-26 18:35:12 +00:00
|
|
|
|
|
|
|
export async function all(ctx) {
|
2020-04-06 22:47:58 +00:00
|
|
|
let graphics = ctx.db.get('graphics')
|
|
|
|
let data = ctx.db.get('schedule').forEach(function(s) {
|
|
|
|
s.graphic = graphics.getById(s.graphic_id).value()
|
|
|
|
}).sortBy('sort').value()
|
|
|
|
// let data = await Schedule.getAll({ }, ['graphic'], 'sort')
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
ctx.io.emit('schedule.all', data)
|
2018-06-26 18:35:12 +00:00
|
|
|
total(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function total(ctx) {
|
2020-04-06 22:47:58 +00:00
|
|
|
let data = ctx.db.get('schedule').size()
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
ctx.io.emit('schedule.total', { total: data })
|
2018-06-26 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function add(ctx, payload) {
|
|
|
|
payload.sort = 1
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
let schedule = ctx.db.get('schedule')
|
|
|
|
|
|
|
|
let last = schedule.sortBy('sort').last().value()
|
2018-06-26 18:35:12 +00:00
|
|
|
|
|
|
|
if (last) {
|
2020-04-06 22:47:58 +00:00
|
|
|
payload.sort = last.sort + 1
|
2018-06-26 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
await schedule.insert(payload).write()
|
2018-06-26 18:35:12 +00:00
|
|
|
|
|
|
|
await all(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function patch(ctx, payload) {
|
|
|
|
await Promise.all(payload.map(async item => {
|
|
|
|
let scheduleItem = await Schedule.getSingle(item.id)
|
|
|
|
|
|
|
|
scheduleItem.set({ sort: item.sort })
|
|
|
|
|
|
|
|
await scheduleItem.save()
|
|
|
|
}))
|
|
|
|
|
|
|
|
await all(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function remove(ctx, payload) {
|
|
|
|
let scheduleItem = await Schedule.getSingle(payload.id)
|
|
|
|
|
|
|
|
await scheduleItem.destroy()
|
|
|
|
|
|
|
|
await all(ctx)
|
|
|
|
}
|