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

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

const write = (req, res) => {
    const data = req.body;

    service.write(data).then((record) => {
        res.status(utility.getResStatus(record))
            .json(record);
    });
}

const read = (req, res) => {
    const props = {};
    props.range = req.query.range;
    props.device = req.query.device;
    props.measurement = req.query.measurement;

    service.read(props).then((records) => {
        res.status(utility.getResStatus(records))
            .json(records);
    });
}

const readLast = (req, res) => {
    const props = {};
    props.device = req.query.device;
    props.measurement = req.query.measurement;

    service.readLast(props).then((records) => {
        res.status(utility.getResStatus(records))
            .json(records);
    });
}

module.exports = {
    write,
    read,
    readLast
}