summaryrefslogtreecommitdiffstats
path: root/server/api/backends.js
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-15 05:49:30 +0100
committerJannik Schönartz2019-03-15 05:49:30 +0100
commitc2c64dafe7f2a4fc2accd7ee5f87d382886bbedb (patch)
tree08ac85332cdff8af7e682add94223dcc2bc29e60 /server/api/backends.js
parentmerge (diff)
downloadbas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.tar.gz
bas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.tar.xz
bas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.zip
[external-backeds] Big idoit rework, to match the updated api
Diffstat (limited to 'server/api/backends.js')
-rw-r--r--server/api/backends.js133
1 files changed, 60 insertions, 73 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 7738ec7..838467a 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -59,17 +59,15 @@ router.getAsync('/:id', async (req, res) => {
*
* @return: Returns a list with all objects of the backend.
*/
-router.get('/:id/objects', (req, res) => {
+router.getAsync('/:id/objects', async (req, res) => {
const id = req.params.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObjects(backend.credentials).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObjects(backend.credentials)
+ res.status(200).send(result)
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
/*
@@ -78,18 +76,16 @@ router.get('/:id/objects', (req, res) => {
*
* @return: Returns information about a given object and all childs.
*/
-router.get('/:id/objects/:oid', (req, res) => {
+router.getAsync('/:id/objects/:oid', async (req, res) => {
const id = req.params.id
const oid = req.params.oid
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObject(backend.credentials, oid).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObject(backend.credentials, oid)
+ res.status(200).send(result)
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
/*
@@ -97,15 +93,13 @@ router.get('/:id/objects/:oid', (req, res) => {
*
* @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
*/
-router.get('/:id/objecttypes', (req, res) => {
+router.getAsync('/:id/objecttypes', async (req, res) => {
const id = req.params.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObjectTypes(backend.credentials).then(result => {
- res.status(200).send(result)
- })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObjectTypes(backend.credentials)
+ res.status(200).send(result)
})
/*
@@ -301,41 +295,37 @@ router.get('/:id/import', (req, res) => {
// TODO: Move to clients?
// TODO: receive files should be authenticated!
// Gets a list of all the files uploaded and connected to the client.
-noAuthRouter.get('/:id/:uuid/files', (req, res) => {
+noAuthRouter.getAsync('/:id/:uuid/files', async (req, res) => {
const id = req.params.id
const uuid = req.params.uuid
- db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] }).then(backend => {
- if (backend && backend.mappedClients.length === 1) {
- const externalId = backend.mappedClients[0].backend_x_client.externalId
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.getFileList(backend.credentials, externalId).then(fileList => {
- res.send({ success: true, data: { backendId: id, clientUUID: uuid, externalId: externalId, fileList: fileList } })
- })
- } else {
- res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
- }
- })
+ const backend = await db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] })
+ if (backend && backend.mappedClients.length === 1) {
+ const externalId = backend.mappedClients[0].backend_x_client.externalId
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const fileList = await instance.getFileList(backend.credentials, externalId)
+ res.send({ success: true, data: { backendId: id, clientUUID: uuid, externalId: externalId, fileList: fileList } })
+ } else {
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
+ }
})
// Returns the content of a file
-noAuthRouter.get('/:id/:uuid/files/:filename', (req, res) => {
+noAuthRouter.getAsync('/:id/:uuid/files/:filename', async (req, res) => {
const id = req.params.id
const uuid = req.params.uuid
const filename = req.params.filename
- db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] }).then(backend => {
- if (backend && backend.mappedClients.length === 1) {
- const externalId = backend.mappedClients[0].backend_x_client.externalId
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.getFile(backend.credentials, externalId, filename).then(file => {
- file.decoded = Buffer.from(file.value, 'base64').toString('UTF-8')
- res.send(file)
- })
- } else {
- res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
- }
- })
+ const backend = await db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] })
+ if (backend && backend.mappedClients.length === 1) {
+ const externalId = backend.mappedClients[0].backend_x_client.externalId
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const file = await instance.getFile(backend.credentials, externalId, filename)
+ file.decoded = Buffer.from(file.value, 'base64').toString('UTF-8')
+ res.send(file)
+ } else {
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
+ }
})
// POST requests
@@ -347,24 +337,22 @@ noAuthRouter.get('/:id/:uuid/files/:filename', (req, res) => {
* type: <BACKEND_TYPE>
* credentials: <BACKEND_CREDENTIALS>
*
- * If the id is set, the backend in the db ist testet.
- * Else the backend is postet in the request.
+ * If the id is set, the backend in the db is testet.
+ * Else the backend is posted in the request.
*/
-router.post('/:id/connection', (req, res) => {
+router.postAsync('/:id/connection', async (req, res) => {
const id = req.params.id
- var getBackend = null
- if (id !== '0') getBackend = db.backend.findOne({ where: { id: id } })
- else getBackend = new Promise(resolve => resolve(req.body))
+ let backend = null
+ if (id !== '0') backend = await db.backend.findOne({ where: { id: id } })
+ else backend = req.body
- getBackend.then(backend => {
- // Creating the backend instance and calling the specific checkConnection.
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.checkConnection(backend.credentials).then(connection => {
- res.status(200).send({ success: connection.success, error: connection.error })
- })
- })
+ // Creating the backend instance and calling the specific checkConnection.
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const response = await instance.checkConnection(backend.credentials)
+ if (response.error) return res.status(500).send(response)
+ else if (response) res.status(200).send()
})
// PUT requests
@@ -436,12 +424,11 @@ router.put('/:id/syncsettings', (req, res) => {
*
* Deletes the backend to the given id.
*/
-router.post('/delete', (req, res) => {
+router.postAsync('/delete', async (req, res) => {
const backendIds = req.body.id
- db.backend.destroy({ where: { id: backendIds } }).then(function () {
- res.status(200).send('success')
- })
+ await db.backend.destroy({ where: { id: backendIds } })
+ res.status(200).send('success')
})
module.exports.router = router