const express = require('express');
const router = express.Router();
const consumableService = require('./consumable.service');

router.post('/create', create);
router.get('/', getAll);
router.get('/:id', getById);
router.put('/:id', update);
router.delete('/:id', _delete);
router.post('/checkout', checkout);

module.exports = router;

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

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

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

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

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

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