-1
Good evening, I’m starting to learn about Ode and React.js and I’m having a problem because I need to use more than one function on the same route and by get method. I did the code below, it’s working, but I don’t know if it’s a right way to do it. I wonder if there’s another way less verbose. In this case, I used these two querys to show that I need to use more than one function per route in the Get method. The select won’t stay like this, it was just an example
module.exports = {
async listar(request, response) {
const ano = request.body;
const mes = request.body;
const valor = {mensal:0,
anual:0};
valor.mensal = await connection('entrada')
.sum('valor_entrada')
.whereRaw(`Year(data_entrada) = ${ano}`)
valor.anual = await connection('entrada')
.sum('valor_entrada')
.whereRaw(`Month(data_entrada) = ${mes}`)
return response.json(valor)
}
}
Here is my route
const express = require('express');
const connection = require('./database/connection');
const EntradaController = require('./controllers/EntradaController');
const routes = express.Router();
routes.get('/entradas', EntradaController.listar);
module.exports = routes;
You could do these operations in one
SELECT
only, plus your code is susceptible to SQL injection therewhereRaw
...– Luiz Felipe