From c79afe469347ce4f0cffa1ba268939fd00662e81 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sun, 1 May 2022 01:01:22 -0400 Subject: added gender distribution to main page --- interface/controllers/citizens.controller.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'interface/controllers/citizens.controller.js') diff --git a/interface/controllers/citizens.controller.js b/interface/controllers/citizens.controller.js index 64522b1..b50ee74 100644 --- a/interface/controllers/citizens.controller.js +++ b/interface/controllers/citizens.controller.js @@ -1,11 +1,26 @@ const db = require("../models"); const citizens = db.citizens; -const op = db.Sequelize.Op; // Retrieve all citizens from the database. Limit the number of citizens returned to 10. exports.findXCitizens = () => { - const limit = 10; - return citizens.findAll({ - limit: limit - }); -} + const limit = 10; + return citizens.findAll({ + limit: limit, + }); +}; + +// Get total number of male and female citizens +exports.findGenderDistribution = () => { + // group by the 'gender' column + + /** + * This code is equivalent to the following SQL query: + * select count(gender), gender from citizens group by gender; + */ + + return citizens.findAll({ + group: ["gender"], + attributes: ["gender", [db.sequelize.fn("COUNT", "gender"), "genderCount"]], + raw: true + }); +}; -- cgit v1.2.3