Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / setting / setting.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var Settings = require('../../models').Settings;
5 var Agent = require('../../models').User;
6
7 // Get list of settings
8 exports.index = function(req, res) {
9   Settings
10     .findAll()
11     .then(function(settings) {
12       return res.status(200).send(settings);
13     })
14     .catch(function(err) {
15       return handleError(res, err);
16     });
17 };
18
19 // Get list of settings
20 exports.getInternal = function(req, res) {
21   Settings
22     .findOne()
23     .then(function(result) {
24       Agent
25         .findAll({
26           where: {
27             internal: {
28               $gte: result.min_internal
29             }
30           }
31         })
32         .then(function(internals) {
33           var pluckdInternals = _.pluck(_.pluck(internals, 'dataValues'), 'internal');
34           var lastChecked = result.min_internal;
35           var internalNumber = {
36             value: null
37           };
38           do {
39             if (pluckdInternals.indexOf(lastChecked) === -1) {
40               internalNumber.value = lastChecked;
41             } else {
42               lastChecked++;
43             }
44           } while (!internalNumber.value)
45           return res.status(200).send(internalNumber);
46         })
47         .catch(function(err) {
48           console.log(err);
49           return handleError(res, err);
50         });
51     })
52     .catch(function(err) {
53       return handleError(res, err);
54     });
55 };
56
57 // Get list of settings
58 exports.getInfo = function(req, res) {
59
60   var info = require('../../../package.json');
61
62   return res.status(200).send({
63     name: info.name,
64     version: info.version,
65     engines: info.engines
66   });
67 };
68
69 // Get a single setting
70 exports.show = function(req, res) {
71   Settings
72     .findById(req.params.id)
73     .then(function(setting) {
74       if (!setting) {
75         return res.sendStatus(404);
76       }
77       return res.send(setting);
78     })
79     .catch(function(err) {
80       return handleError(res, err);
81     });
82 };
83
84 // Creates a new setting in the DB.
85 exports.create = function(req, res) {
86   Settings
87     .create(req.body)
88     .then(function(setting) {
89       return res.status(201).send(setting);
90     })
91     .catch(function(err) {
92       return handleError(res, err);
93     });
94 };
95
96 // Updates an existing setting in the DB.
97 exports.update = function(req, res) {
98   if (req.body.id) {
99     delete req.body.id;
100   }
101   Settings
102     .find({
103       where: {
104         id: req.params.id
105       }
106     })
107     .then(function(setting) {
108       if (!setting) {
109         return res.sendStatus(404);
110       }
111       var updated = _.merge(setting, req.body);
112       updated.save()
113         .then(function() {
114           return res.status(200).send(setting);
115         })
116         .catch(function(err) {
117           return handleError(res, err);
118         });
119     })
120     .catch(function(err) {
121       return handleError(res, err);
122     });
123 };
124
125 // Deletes a setting from the DB.
126 exports.destroy = function(req, res) {
127   Settings
128     .find({
129       where: {
130         id: req.params.id
131       }
132     })
133     .then(function(setting) {
134       if (!setting) {
135         return res.sendStatus(404);
136       }
137       setting.destroy()
138         .then(function() {
139           return res.sendStatus(204);
140         })
141         .catch(function(err) {
142           return handleError(res, err);
143         });
144     })
145     .catch(function(err) {
146       return handleError(res, err);
147     });
148 };
149
150 function handleError(res, err) {
151   return res.status(500).send(err);
152 }