filo_caspar/api/schedule/routes.mjs

55 lines
1.1 KiB
JavaScript
Raw Normal View History

import Schedule from './model.mjs'
2018-06-26 18:35:12 +00:00
export async function all(ctx) {
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
ctx.io.emit('schedule.all', data)
2018-06-26 18:35:12 +00:00
total(ctx)
}
export async function total(ctx) {
let data = ctx.db.get('schedule').size()
2018-06-26 18:35:12 +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
let schedule = ctx.db.get('schedule')
let last = schedule.sortBy('sort').last().value()
2018-06-26 18:35:12 +00:00
if (last) {
payload.sort = last.sort + 1
2018-06-26 18:35:12 +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)
}