Socialify

Folder ..

Viewing index.js
107 lines (98 loc) • 2.9 KB

  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
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
const express = require("express");
const router = express.Router();
const dashboardController = require("../controllers/dashboard.controller");
const citizensController = require("../controllers/citizens.controller");
const api = require("./api");
const citizensAPI = require("./api/citizens");
const geographyAPI = require("./api/geography");
const transactionsRoute = require("./transactions");

// Setup api routes
router.use("/api", api);
router.use("/api/citizens", citizensAPI);
router.use("/api/geography", geographyAPI);
router.use("/transactions", transactionsRoute);

router.get("/", (req, res) => {
  Promise.all([
    dashboardController.genderDist(),
    dashboardController.ageDist(),
    dashboardController.casteDist(),
    dashboardController.maritalDist(),
    dashboardController.disablePercentage(),
    dashboardController.citizensByDistrict(),
  ]).then((results) => {
    const [
      genderDist,
      ageDist,
      casteDist,
      maritalDist,
      disableDist,
      citizenDist,
    ] = results;
    res.render("index", {
      title: "Home Page",
      genderDist,
      ageDist,
      casteDist,
      maritalDist,
      disableDist,
      citizenDist,
    });
  });
});

router.get("/citizens", (req, res) => {
  // Get the limit and offset from the query string
  const limit = parseInt(req.query.limit, 10) || 10;
  const page = req.query.page ? (req.query.page - 1) * limit : 0;

  // Get the citizens from the database
  Promise.all([
    citizensController.findXCitizens(limit, page),
    citizensController.getCountOfCitizens(),
  ]).then((results) => {
    const [citizens, count] = results;
    res.render("citizens", {
      title: "Citizens",
      citizens,
      count: count[0].count,
    });
  });
});

router.get("/search", (req, res) => {
  // Get the limit and offset from the query string
  const limit = parseInt(req.query.limit, 10) || 10;
  const page = req.query.page ? (req.query.page - 1) * limit : 0;
  const query = req.query.query;

  // Get the citizens from the database
  Promise.all([
    citizensController.searchCitizens(query, limit, page),
    citizensController.countSearchedCitizens(query),
  ]).then((results) => {
    const [citizens, count] = results;
    res.render("citizens", {
      title: `Search results for "${query}"`,
      citizens,
      count: count[0].count,
    });
  });
});

router.get("/addUser", (req, res) => {
  res.render("adduser", {
    title: "Add User",
  });
});

router.get("/beneficiaries", (req, res) => {
  const limit = parseInt(req.query.limit, 10) || 100;
  const page = req.query.page ? (req.query.page - 1) * limit : 0;
  Promise.all([
    citizensController.getBeneficiaries(limit, page),
    citizensController.getCountOfCitizens(),
  ]).then((results) => {
    const [beneficiaries, count] = results;
    res.render("beneficiaries", {
      title: "Beneficiaries",
      beneficiaries,
      count: count[0].count,
    });
  });
});

// export the router
module.exports = router;