summaryrefslogtreecommitdiffstats
path: root/server/api/backends.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/backends.js')
-rw-r--r--server/api/backends.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
new file mode 100644
index 0000000..e3e6014
--- /dev/null
+++ b/server/api/backends.js
@@ -0,0 +1,75 @@
+/* global __appdir */
+const path = require('path');
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends', 'external-backends.js'));
+var db = require(path.join(__appdir, 'lib', 'sequelize'));
+
+module.exports = {
+ getCredentialsByType: function(req, res) {
+ const backendType = req.query.type;
+ const b = new ExternalBackends();
+ const instance = b.getInstance(backendType);
+ res.status(200).send(instance.getCredentials());
+ },
+
+ getBackendInfoById: function(req, res) {
+ // TODO: Test (Not used for now) needed for edit // TODO: // TODO:
+ const backendId = req.query.id;
+ db.backend.findOne({ where: { id: backendId } }).then(backend => {
+ const b = {
+ backendId: backendId,
+ backendName: backend.name,
+ backendType: backend.type,
+ backendCredentials: backend.credentials
+ }
+ res.status(200).send(b);
+ });
+ },
+
+ getBackendTypes: function(req, res) {
+
+ const backends = new ExternalBackends();
+ var files = backends.getBackends();
+
+ for (var i = 0; i < files.length; i++) {
+
+ // Cut off -backends.js
+ files[i] = files[i].slice(0, -11);
+ }
+
+ res.status(200).send(files);
+ },
+
+ getBackendList: function(req, res) {
+ db.backend.findAll({
+ attributes: ['id', 'name', 'type']
+ }).then(function (backends) {
+ res.status(200).send(backends);
+ });
+ },
+
+ // POST REQUESTS
+ saveBackend: function(req, res) {
+ // Save credentials in the db.
+ const formData = req.body;
+ const credentialString = JSON.stringify(formData.backendCredentials);
+
+ if (formData.backendId == 0) {
+ // Insert new backend in the db.
+ db.backend.create({ name: formData.backendName, type: formData.backendType, credentials: credentialString });
+ } else {
+ // Update an existing backend in the db.
+ db.backend.update({ name: formData.backendName, type: formData.backendType, credentials: credentialString }, {where: {id: formData.backendId} });
+ }
+ //db.backend.findOne({})
+
+ res.status(200).send('success');
+ },
+
+ deleteBackendById: function(req, res) {
+ const backendId = req.body.id;
+
+ db.backend.destroy({ where: { id: backendId } }).then(function() {
+ res.status(200).send('success');
+ });
+ }
+}