/**
 * @file presetDataController.js
 * @author Radim Buráň (xburan03@stud.fit.vutbr.cz)
 */

const service = require('../services/presetDataService');
const utility = require('../utility');

const getByID = (req, res) => {
    const id = Number(req.params.id);
    service.getByID(id).then((user) => {
        res.status(utility.getResStatus(user))
            .json(user);
    });
}

const patch = (req, res) => {
    const changes = req.body;
    if (Object.keys(changes).length === 0) {
        res.status(200).json({ message: "Nothing changed!" });
    }
    service.update(changes).then((user) => {
        res.status(utility.getResStatus(user))
            .json(user);
    });
}

const getByFilter = (req, res) => {
    const filter = {};
    filter.id = req.query.id;
    filter.humidity = req.query.humidity;
    filter.humidityHysteresis = req.query.humidityHysteresis;
    const max_items = Number(req.query.max_items);
    const page = Number(req.query.page);
    service.getByFilter(filter, max_items || 20, page || 0).then((user) => {
        res.status(utility.getResStatus(user))
            .json(user);
    });
}

module.exports = {
    getByID,
    patch,
    getByFilter
}