summaryrefslogtreecommitdiffstats
path: root/server/api/configurator.js
blob: 4ae1f359408304e5113a891544d484c971008d55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())

// ############################################################################
// ###########################  GET requests  #################################

router.getAsync('/configs', async (req, res) => {
  const configs = await db.config.findAll({ order: [['name', 'ASC']] })
  res.status(200).send(configs)
})

router.getAsync('/entries', async (req, res) => {
  const entries = await db.entry.findAll({ order: [['name', 'ASC']] })
  res.status(200).send(entries)
})

router.getAsync('/configs/:id/entries', async (req, res) => {
  const config = await db.config.findOne({
    where: { id: req.params.id },
    include: ['entries'],
    order: [[db.config.associations.entries, db.config.associations.entries.through, 'sortValue', 'ASC']]
  })
  if (config) res.status(200).send(config.entries)
  else res.status(404).end()
})

router.getAsync('/configs/:id/groups', async (req, res) => {
  const config = await db.config.findOne({ where: { id: req.params.id }, include: ['groups'] })
  if (config) res.status(200).send(config.groups)
  else res.status(404).end()
})

router.getAsync('/configs/:id/clients', async (req, res) => {
  const config = await db.config.findOne({ where: { id: req.params.id }, include: ['clients'] })
  if (config) res.status(200).send(config.clients)
  else res.status(404).end()
})

// ############################################################################
// ##########################  POST requests  #################################

router.postAsync(['/configs', '/configs/:id'], async (req, res) => {
  if (req.query.delete !== undefined && req.query.delete !== 'false') {
    const count = await db.config.destroy({ where: { id: req.body.ids } })
    res.status(200).send({ count })
  } else {
    let config
    if (req.params.id === undefined) config = await db.config.create(req.body.data)
    else {
      config = await db.config.findOne({ where: { id: req.params.id } })
      if (config) await config.update(req.body.data)
    }
    if (config) {
      await config.setEntries([])
      if (req.body.entries.length > 0) {
        const promises = []
        req.body.entries.forEach((entry, index) => {
          promises.push(config.addEntry(entry.id, { through: { sortValue: index, customName: entry.customName, keyBind: entry.keyBind } }))
        })
        await Promise.all(promises)
      }
      res.status(200).send({ id: config.id })
    } else {
      res.status(404).end()
    }
  }
})

router.postAsync(['/entries', '/entries/:id'], async (req, res) => {
  if (req.query.delete !== undefined && req.query.delete !== 'false') {
    const count = await db.entry.destroy({ where: { id: req.body.ids } })
    res.status(200).send({ count })
  } else {
    let entry
    if (req.params.id === undefined) entry = await db.entry.create(req.body.data)
    else {
      entry = await db.entry.findOne({ where: { id: req.params.id } })
      if (entry) await entry.update(req.body.data)
    }
    if (entry) {
      res.status(200).send({ id: entry.id })
    } else {
      res.status(404).end()
    }
  }
})

// ############################################################################
// ##########################  POST requests  #################################

router.putAsync('/configs/:id/groups', async (req, res) => {
  const config = await db.config.findOne({ where: { id: req.params.id } })
  if (config) {
    await config.setGroups(req.body.ids)
    res.status(200).end()
  } else { res.status(404).end() }
})

router.putAsync('/configs/:id/clients', async (req, res) => {
  const config = await db.config.findOne({ where: { id: req.params.id } })
  if (config) {
    await config.setClients(req.body.ids)
    res.status(200).end()
  } else { res.status(404).end() }
})

// ############################################################################
// ##########################  DELETE requests  ###############################

router.deleteAsync('/configs/:id', async (req, res) => {
  const count = await db.config.destroy({ where: { id: req.params.id } })
  if (count) res.status(200).end()
  else res.status(404).end()
})

router.deleteAsync('/entries/:id', async (req, res) => {
  const count = await db.entry.destroy({ where: { id: req.params.id } })
  if (count) res.status(200).end()
  else res.status(404).end()
})

// ############################################################################
// ############################################################################

module.exports.router = router