const express = require('express');
const router = express.Router();
const componentService = require('./component.service');

router.post('/create', create);
router.get('/', getAll);
router.get('/:id', getById);
router.put('/:id', update);
router.delete('/:id', _delete);
router.post('/assign-asset', assignToAsset);
router.post('/remove-asset/:id', removeFromAsset);

module.exports = router;

function create(req, res, next) {
	componentService.create(req.body)
		.then(result => res.json(result))
		.catch(next);
}

function getAll(req, res, next) {
	componentService.getAll(req.query)
		.then(result => res.json(result))
		.catch(next);
}

function getById(req, res, next) {
	componentService.getById(req.params.id)
		.then(result => res.json(result))
		.catch(next);
}

function update(req, res, next) {
	componentService.update(req.params.id, req.body)
		.then(result => res.json(result))
		.catch(next);
}

function _delete(req, res, next) {
	componentService.delete(req.params.id)
		.then(result => res.json(result))
		.catch(next);
}

function assignToAsset(req, res, next) {
	componentService.assignToAsset(req.body)
		.then(result => res.json(result))
		.catch(next);
}

function removeFromAsset(req, res, next) {
	componentService.removeFromAsset(req.params.id, req.body)
		.then(result => res.json(result))
		.catch(next);
}
