filo_caspar/api/graphic/routes.js

97 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-04-14 04:01:51 +00:00
import Graphic from './model'
2018-06-26 18:35:12 +00:00
/*
* Event: 'graphic.all'
*
* Request all graphics in store
*/
2017-12-03 11:34:43 +00:00
export async function all(ctx) {
2016-04-14 04:01:51 +00:00
let data = await Graphic.getAll()
2018-06-26 18:35:12 +00:00
ctx.socket.emit('graphic.all', data.toJSON())
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'graphic.single'
*
* Request a single graphic
*/
2017-12-03 11:34:43 +00:00
export async function single(ctx, data) {
2016-04-14 04:01:51 +00:00
if (!data || !data.id) {
ctx.log.warn('called graphic get single but no id specified')
return
}
let graphic = await Graphic.getSingle(data.id)
2018-06-26 18:35:12 +00:00
ctx.socket.emit('graphic.single', graphic.toJSON())
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'graphic.create'
*
* Create a single graphic and emit to all clients.
*
* @body {string} engine - Engine for the graphic
* @body {string} name - Name of graphic
*/
2016-04-14 04:01:51 +00:00
export async function create(ctx, data) {
data.settings = {}
data.is_deleted = false
if (data.engine === 'countdown') {
data.settings.html = `<span id="${data.name}-countdown-timer">countdown appears here</span>`
2018-06-26 18:35:12 +00:00
data.settings.main = '<%- text %> - <%- finished %>'
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
let graphic = await Graphic.create(data)
2016-04-14 04:01:51 +00:00
2018-06-26 18:35:12 +00:00
ctx.io.emit('graphic.single', graphic.toJSON())
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'graphic.remove'
*
* Remove a single graphic
*
* @body {int} id - Id of the graphic to remove
*/
2016-04-14 04:01:51 +00:00
export async function remove(ctx, data) {
if (!data || !data.id) {
ctx.log.warn('called graphic get single but no id specified')
return
}
let graphic = await Graphic.getSingle(data.id)
graphic.set({ is_deleted: true })
await graphic.save()
2018-06-26 18:35:12 +00:00
let output = await Graphic.getAll()
ctx.io.emit('graphic.all', output.toJSON())
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'graphic.update'
*
* Update a single graphic
*
* @body {int} id - Id of the graphic to update
* @body {string} [name] - Name of the graphic
* @body {string} [engine] - Engine for the graphic
* @body {object} [settings] - Settings for the graphic, JSON object
*/
2016-04-14 04:01:51 +00:00
export async function update(ctx, data) {
if (!data || !data.id) {
ctx.log.warn('called graphic update but no id specified')
return
}
let graphic = await Graphic.getSingle(data.id)
graphic.set(data)
await graphic.save()
2018-06-26 18:35:12 +00:00
ctx.io.emit('graphic.single', graphic.toJSON())
2016-04-14 04:01:51 +00:00
}