diff options
| -rw-r--r-- | interface/controllers/citizens.controller.js | 10 | ||||
| -rw-r--r-- | interface/models/index.js | 3 | ||||
| -rw-r--r-- | interface/routes/index.js | 10 | ||||
| -rw-r--r-- | interface/server.js | 5 | ||||
| -rw-r--r-- | interface/views/index.ejs | 66 |
5 files changed, 84 insertions, 10 deletions
diff --git a/interface/controllers/citizens.controller.js b/interface/controllers/citizens.controller.js index fef549d..64522b1 100644 --- a/interface/controllers/citizens.controller.js +++ b/interface/controllers/citizens.controller.js @@ -3,15 +3,9 @@ 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 = (req, res) => { +exports.findXCitizens = () => { const limit = 10; - citizens.findAll({ + return citizens.findAll({ limit: limit - }).then(citizens => { - res.send(citizens); - }).catch(err => { - res.status(500).send({ - message: err.message || "Some error occurred while retrieving citizens." - }); }); } diff --git a/interface/models/index.js b/interface/models/index.js index 6a07907..852fb65 100644 --- a/interface/models/index.js +++ b/interface/models/index.js @@ -12,7 +12,8 @@ const sequelize = new Sequelize(databaseConfig.database, databaseConfig.username }, define: { timestamps: false - } + }, + logging: false }); const db = {}; db.Sequelize = Sequelize; diff --git a/interface/routes/index.js b/interface/routes/index.js index 60155eb..17c2021 100644 --- a/interface/routes/index.js +++ b/interface/routes/index.js @@ -3,7 +3,15 @@ const router = express.Router(); const citizensController = require("../controllers/citizens.controller"); // Setup Hello World route -router.get("/", citizensController.findXCitizens); +router.get("/", (req, res) => { + // Get the citizens from the database + citizensController.findXCitizens().then(citizens => { + res.render("index", { + citizens: citizens, + title: "Hello World" + }); + }); +}); // export the router module.exports = router; diff --git a/interface/server.js b/interface/server.js index ff88e45..4173d2f 100644 --- a/interface/server.js +++ b/interface/server.js @@ -8,6 +8,11 @@ app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); +// Set view engine to ejs +app.set("view engine", "ejs"); +app.set("views", "./views"); +app.use(express.static("./public")); + // Import routes const routes = require("./routes"); app.use("/", routes); diff --git a/interface/views/index.ejs b/interface/views/index.ejs new file mode 100644 index 0000000..d7e0c5f --- /dev/null +++ b/interface/views/index.ejs @@ -0,0 +1,66 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>Document</title> + <style> + table, + th, + td { + border: 1px solid black; + border-collapse: collapse; + padding: 4px 8px; + } + </style> + </head> + + <body> + <h1><%= title %></h1> + <!-- Print Citizens data in table --> + <table> + <thead> + <tr> + <th>Citizen ID</th> + <th>First Name</th> + <th>Middle Name</th> + <th>Last Name</th> + <th>Address</th> + <th>Mobile Number</th> + <th>Date of Birth</th> + <th>Gender</th> + <th>Marital Status</th> + <th>Disabled</th> + <th>Disabled Percentage</th> + <th>Caste</th> + <th>Village ID</th> + </tr> + </thead> + <tbody> + <% for(var i=0; i < citizens.length; i++) { %> + <tr> + <td><%= citizens[i].citizen_id %></td> + <td><%= citizens[i].first_name %></td> + <td><%= citizens[i].middle_name ? citizens[i].middle_name : '-' %></td> + <td><%= citizens[i].last_name %></td> + <td><%= citizens[i].address %></td> + <td><%= citizens[i].mobile_num %></td> + <td><%= citizens[i].dob %></td> + <td><%= citizens[i].gender %></td> + <td><%= citizens[i].marital_status %></td> + <td><%= citizens[i].disabled %></td> + <td><%= citizens[i].disbaled_percentage %></td> + <td><%= citizens[i].caste %></td> + <td><%= citizens[i].village_id %></td> + </tr> + <% } %> + </tbody> + </table> + <script> + // Print the result of the sequelize query in the console - variable is 'citizens' + const citizens = <%- JSON.stringify(citizens) %>; + console.log(citizens); + </script> + </body> +</html> |
