diff options
Diffstat (limited to 'interface/controllers')
| -rw-r--r-- | interface/controllers/citizens.controller.js | 27 | ||||
| -rw-r--r-- | interface/controllers/users.controller.js | 18 |
2 files changed, 39 insertions, 6 deletions
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 + }); +}; diff --git a/interface/controllers/users.controller.js b/interface/controllers/users.controller.js new file mode 100644 index 0000000..fff4469 --- /dev/null +++ b/interface/controllers/users.controller.js @@ -0,0 +1,18 @@ +const { users } = require("../models"); +const db = require("../models"); +const citizens = db.citizens; +const op = db.Sequelize.Op; + +// Create a new user +exports.create = (username, password) => { + return users.create({ username, password }); +} + +// Get a user by their username +exports.findByUsername = (username) => { + return users.findOne({ + where: { + username: username + } + }); +} |
