summaryrefslogblamecommitdiffstats
path: root/server/api/clients.js
blob: 3e257e29dfae226ab2b202ebda935d2861edfb9f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                     

                                                         
 


                                
                                                                                              

                    
    
 


                                                                                            

                                  
      

   



                                                                 
                             

                                                                        

                           
                                                                        
                                                                                   
                                                                             
                                        

            


                                                                                                              
     



                               
                                                                                             

   
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))

// GET Requests
module.exports.get = {
  getList: function (req, res) {
    db.client.findAll({ attributes: ['id', 'name'], order: [['name', 'ASC']] }).then(list => {
      res.send(list)
    })
  },

  // get name, description, ip, mac and uuid of a client (by id)
  getClient: function (req, res) {
    db.client.findOne({ where: { id: req.query.id }, include: ['groups'] }).then(client => {
      if (client) res.send(client)
      else res.status(404).end()
    })
  }
}

// POST Requests
module.exports.post = {
  // create client or update information of a client (returns id)
  save: function (req, res) {
    if (req.body.id > 0) {
      db.client.findOne({ where: { id: req.body.id } }).then(client => {
        if (client) {
          var promises = []
          if (req.body.info) promises.push(client.update(req.body.info))
          if (req.body.groupIds) promises.push(client.setGroups(req.body.groupIds))
          Promise.all(promises).then(() => { res.send({ id: req.body.id }) })
        } else { res.status(404).end() }
      })
    } else {
      db.client.create(req.body.info).then(client => {
        if (req.body.groupIds) client.setGroups(req.body.groupIds).then(() => { res.send({ id: client.id }) })
      })
    }
  },

  // delete clients
  delete: function (req, res) {
    db.client.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
  }
}