From 06b9f3d31aa6a3d2b9f76a0e3bb49f07562bb56d Mon Sep 17 00:00:00 2001 From: Lewis <lgtg20@inf.ufpr.br> Date: Tue, 30 Jul 2024 14:41:22 -0300 Subject: [PATCH] praying --- src/libs/middlewares/aggregateData.js | 41 +++++++++++++++++++++++ src/libs/routes_v1/enrollmentAggregate.js | 21 +++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 src/libs/middlewares/aggregateData.js diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js new file mode 100644 index 00000000..a7d24c42 --- /dev/null +++ b/src/libs/middlewares/aggregateData.js @@ -0,0 +1,41 @@ +const id2str = require(`./id2str`); + +function aggregateData(req, res, next) { + const newResult = [] + const aggregateFields = ['gender'] + let id; + const fields = req.query.dims.split(','); + let currentAggregateField; + let currentNonAggregateField; + fields.forEach(field => {if (aggregateFields.includes(field)) currentAggregateField = field; else currentNonAggregateField = field}); + if (currentAggregateField) { + console.log(currentAggregateField); + req.result.forEach((r) => { + id = 1; + for (const property in r) { + if (property.includes("total_")) { + const data = { + total: r[property], + year: r.year, + [`${currentAggregateField}_id`]: id, + [`${currentAggregateField}_name`]: id2str[currentAggregateField](id) + } + + if (currentNonAggregateField) { + data[`${currentNonAggregateField}_id`] = r[`${currentNonAggregateField}_id`]; + console.log(currentNonAggregateField) + data[`${currentNonAggregateField}_name`] = id2str[`${currentNonAggregateField}_id`](id); + } + + newResult.push(data) + ++id; + } + } + }) + req.result = newResult; + } + console.log(req.result); + next(); +} + +module.exports = aggregateData; \ No newline at end of file diff --git a/src/libs/routes_v1/enrollmentAggregate.js b/src/libs/routes_v1/enrollmentAggregate.js index e5b877f0..6f225d65 100644 --- a/src/libs/routes_v1/enrollmentAggregate.js +++ b/src/libs/routes_v1/enrollmentAggregate.js @@ -35,6 +35,8 @@ const response = require(`${libs}/middlewares/response`); const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); +const aggregateData = require(`${libs}/middlewares/aggregateData`); + const id2str = require(`${libs}/middlewares/id2str`); const config = require(`${libs}/config`); @@ -294,22 +296,23 @@ enrollmentAggregateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { console.log(req.query); - if (req.query.dims.includes('gender')) + if (req.query.dims && req.query.dims.includes('gender')) { req.sql.from('escola') - .field('SUM(escola.qt_mat_bas_mas)', 'total_mas') + .field('SUM(escola.qt_mat_bas_masc)', 'total_mas') .field('SUM(escola.qt_mat_bas_fem)', 'total_fem') .field('escola.ano_censo', 'year') .group('escola.ano_censo') .order('escola.ano_censo'); } - - req.sql.from('escola') - .field('SUM(escola.qt_mat_bas)', 'total') - .field('escola.ano_censo', 'year') - .group('escola.ano_censo') - .order('escola.ano_censo'); + else { + req.sql.from('escola') + .field('SUM(escola.qt_mat_bas)', 'total') + .field('escola.ano_censo', 'year') + .group('escola.ano_censo') + .order('escola.ano_censo'); + } next(); -}, query, response('enrollment_aggregate')); +}, query, aggregateData, response('enrollment_aggregate')); module.exports = enrollmentAggregateApp; -- GitLab