-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaddLink.js
33 lines (28 loc) · 972 Bytes
/
addLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const addLink = require('express').Router();
const sendResponse = require('../../helpers/sendResponse');
const { createLink } = require('../../services/database/link');
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = 'error';
addLink.post('/', async (req, res) => {
// const {linkName, linkAdd} = req.body;
// validations
req.check('linkName', 'link name is required').exists().isAlpha().isLength({ min: 5 });
req.check('linkAdd', 'link address is required').exists();
const error = req.validationErrors();
if (error) {
return sendResponse(res, 400, [], error[0].msg);
}
try {
const linkData = {
linkName: req.body.linkName,
linkAdd: req.body.linkAdd,
};
await createLink(linkData);
return sendResponse(res, 200, [], 'data saved successfully');
} catch (err) {
logger.error(err);
return sendResponse(res, 500, [], 'something went wrong');
}
});
module.exports = addLink;