<%# LATER: add it only when there is at least one parameter of boolean type -%>
const parseBoolean = (value) => {
    return value === 'true'
}

const register = (app, pool) => {
<%
endpoints.forEach(function(endpoint) {
    const path = endpoint.path

    endpoint.methods.forEach(function(method) {
        if (!method.query) {
            // filter out aggregated_queries for a while (see #17)
            return
        }
        const hasGetOne = method.name === 'get'
        const hasGetMany = method.name === 'get_list'
        const sql = formatQueryForJs(method.query, 12)
        const params = extractParamsFromQuery(method.query)
        const formattedParams = formatParamsAsJavaScriptObject(params, method)

        if (hasGetOne || hasGetMany) {
%>
    app.get('<%- path %>', (req, res, next) => {
        pool.query(<%- sql %>,<%- formattedParams %>
            (err, rows, fields) => {
                if (err) {
                    return next(err)
                }
<%          if (hasGetMany) { -%>
                res.json(rows)
<%          } else { -%>
                if (rows.length === 0) {
                    res.status(404).end()
                    return
                }
                res.json(rows[0])
<%          } -%>
            }
        )
    })
<%
        }
        if (method.name === 'post') {
%>
    app.post('<%- path %>', (req, res, next) => {
        pool.query(<%- sql %>,<%- formattedParams %>
            (err, rows, fields) => {
                if (err) {
                    return next(err)
                }
                res.sendStatus(204)
            }
        )
    })
<%
        }
        if (method.name === 'put') {
%>
    app.put('<%- path %>', (req, res, next) => {
        pool.query(<%- sql %>,<%- formattedParams %>
            (err, rows, fields) => {
                if (err) {
                    return next(err)
                }
                res.sendStatus(204)
            }
        )
    })
<%
        }
        if (method.name === 'delete') {
%>
    app.delete('<%- path %>', (req, res, next) => {
        pool.query(<%- sql %>,<%- formattedParams %>
            (err, rows, fields) => {
                if (err) {
                    return next(err)
                }
                res.sendStatus(204)
            }
        )
    })
<%
        }
    })
})
%>
}

exports.register = register
