host route next() for errors and removed all vaildation

This commit is contained in:
William Mantly 2019-12-20 20:25:59 -05:00
parent b3c668adbf
commit 9b0f60d30f
Signed by: wmantly
GPG Key ID: E1EEC7650BA97160

View File

@ -4,15 +4,17 @@ const router = require('express').Router();
const Host = require('../models/hosts'); const Host = require('../models/hosts');
router.get('/:host', async function(req, res){ router.get('/:host', async function(req, res, next){
let host = req.params.host; try{
let info = await Host.getInfo({host}); return res.json({
host: req.params.host,
results: await Host.getInfo({host: req.params.host})
});
}catch(error){
return next(error);
}
return res.status(info ? 200 : 404).json({
host: req.params.host,
results: info
});
}); });
router.get('/', async function(req, res, next){ router.get('/', async function(req, res, next){
@ -21,56 +23,49 @@ router.get('/', async function(req, res, next){
hosts: req.query.detail ? await Host.listAllDetail() : await Host.listAll() hosts: req.query.detail ? await Host.listAllDetail() : await Host.listAll()
}); });
}catch(error){ }catch(error){
res.status(500)
next(error) next(error)
} }
}); });
router.post('/', async function(req, res){ router.put('/:host', async function(req, res, next){
let ip = req.body.ip;
let host = req.body.host;
let targetPort = req.body.targetPort;
if(!host || !ip || !targetPort ){
return res.status(400).json({
message: `Missing fields: ${!host ? 'host' : ''} ${!ip ? 'ip' : ''} ${!targetPort ? 'targetPort' : ''}`
});
}
try{ try{
await Host.add({ req.body.username = req.user.username;
host, ip, targetPort, await Host.edit(req.body, req.params.host);
username: req.user.username,
forceSSL: req.body.forceSSL,
targetSSL: req.body.targetSSL,
});
return res.json({ return res.json({
message: `Host ${host} Added` message: `Host "${req.params.host}" updated.`
});
}catch(error){
return next(error)
}
});
router.post('/', async function(req, res, next){
try{
req.body.username = req.user.username;
await Host.add(req.body);
return res.json({
message: `Host "${req.body.host}" added.`
}); });
} catch (error){ } catch (error){
next(error)
return res.status(500).json({
message: `ERROR: ${error}`
});
} }
}); });
router.delete('/:host', async function(req, res, next){ router.delete('/:host', async function(req, res, next){
let host = req.params.host;
try{ try{
let host = req.params.host;
let count = await Host.remove({host}); let count = await Host.remove({host});
return res.json({ return res.json({
message: `Host ${host} deleted`, message: `Host ${req.params.host} deleted`,
}); });
}catch(error){ }catch(error){
return res.status(500).json({ return next(error)
message: `ERROR: ${error}`
});
} }
}); });