aboutsummaryrefslogtreecommitdiff
path: root/interface/controllers/citizens.controller.js
blob: b50ee74a29be85420324eefe50672752b2f935fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const db = require("../models");
const citizens = db.citizens;

// Retrieve all citizens from the database. Limit the number of citizens returned to 10.
exports.findXCitizens = () => {
  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
  });
};