const express = require('express');

const regionApp = express.Router();

const libs = `${process.cwd()}/libs`;

const squel = require('squel');

const query = require(`${libs}/middlewares/query`);

const response = require(`${libs}/middlewares/response`);

const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);

let rqf = new ReqQueryFields();

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addValue({
    name: 'id',
    table: 'regiao',
    tableField: 'pk_regiao_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'pk_regiao_id',
        table: 'regiao'
    }
}).addField({
    name: 'search',
    field: false,
    where: true
}).addValueToField({
    name: 'name',
    table: 'regiao',
    tableField: 'nome',
    where: {
        relation: 'LIKE',
        type: 'string',
        field: 'nome',
        table: 'regiao'
    }
}, 'search');

regionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
    req.sql.from('regiao');
    next();
}, query, response('region'));

module.exports = regionApp;