const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const respiratoryRateService = require('./respiratoryRate.service');

router.post('/create', authorize(), create);
router.post('/', authorize(), getAll);
router.post('/option', authorize(), option);
router.get('/count', authorize(), count);
router.put('/:id', authorize(), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete('/:id', authorize(), _delete);

module.exports = router;

/**
 * Create a new respiratoryRate
 * @param {Object} req.body - The respiratoryRate data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created respiratoryRate
 */
function create(req, res, next) {
    respiratoryRateService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Get a list of all respiratoryRates
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to an array of respiratoryRates
 */
function getAll(req, res, next) {
    respiratoryRateService.getAll(req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Get an array of respiratoryRate objects to be used as options in a select box
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to an array of respiratoryRate objects
 */
function option(req, res, next) {
    respiratoryRateService.option(req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Count the number of respiratoryRates matching a query
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the count of respiratoryRates
 */
function count(req, res, next) {
    respiratoryRateService.count(req.query)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Update an existing respiratoryRate
 * @param {number} req.params.id - The respiratoryRate ID to be updated
 * @param {Object} req.body - The respiratoryRate data to be updated
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the updated respiratoryRate
 */
function update(req, res, next) {
    respiratoryRateService.update(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Enable or disable a respiratoryRate
 * @param {number} req.params.id - The respiratoryRate ID to be enabled/disabled
 * @param {Object} req.body - The respiratoryRate data to be updated
 * @param {boolean} req.body.enabled - The enabled status of the respiratoryRate
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the enable/disable process
 */
function enabled(req, res, next) {
    respiratoryRateService.enabled(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Delete a respiratoryRate
 * @param {number} req.params.id - The respiratoryRate ID to be deleted
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the deletion process
 */
function _delete(req, res, next) {
    respiratoryRateService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
