From b861c79f03429313d05ff0a8105b7715aec0ef4d Mon Sep 17 00:00:00 2001 From: jmreddy2106 Date: Tue, 3 May 2022 18:29:31 -0400 Subject: added geography controls --- interface/controllers/citizens.controller.js | 26 +++++ interface/controllers/district.controller.js | 19 +++ interface/controllers/mandal.controller.js | 16 +++ interface/controllers/state.controller.js | 13 +++ interface/controllers/village.controller.js | 18 +++ interface/models/state_master.model.js | 2 +- interface/public/citizens.js | 15 +++ interface/routes/api/citizens.js | 34 ++++++ interface/routes/api/geography.js | 42 +++++++ interface/routes/index.js | 11 ++ interface/views/adduser.ejs | 165 +++++++++++++++++++++++++++ interface/views/citizens.ejs | 1 + interface/views/partials/navbar.ejs | 5 +- 13 files changed, 365 insertions(+), 2 deletions(-) create mode 100644 interface/controllers/district.controller.js create mode 100644 interface/controllers/mandal.controller.js create mode 100644 interface/controllers/state.controller.js create mode 100644 interface/controllers/village.controller.js create mode 100644 interface/routes/api/geography.js create mode 100644 interface/views/adduser.ejs diff --git a/interface/controllers/citizens.controller.js b/interface/controllers/citizens.controller.js index ee9355b..0ee1d42 100644 --- a/interface/controllers/citizens.controller.js +++ b/interface/controllers/citizens.controller.js @@ -15,6 +15,15 @@ exports.findXCitizens = () => { return sequelize.query(query, { type: QueryTypes.SELECT }) }; + +exports.deleteCitizenbyId = (citizen_id) =>{ + + return citizens.destroy({ + where: { citizen_id } + }) + +}; + exports.editCitizen = (citizen_id, address, mobile_num, dob, marital_status) => { return citizens.update({ address, mobile_num, dob, marital_status @@ -25,6 +34,23 @@ exports.editCitizen = (citizen_id, address, mobile_num, dob, marital_status) => }); }; +//Check Citizen exists or not +exports.checkCitizenId = (citizen_id) => { + return citizens.findOne({ + where: { + citizen_id + } + }).then( + citizen_id => { + if (citizen_id) { + return true; + } + return false; + } + ) +} + + // Get total number of male and female citizens exports.findGenderDistribution = () => { // group by the 'gender' column diff --git a/interface/controllers/district.controller.js b/interface/controllers/district.controller.js new file mode 100644 index 0000000..5b21530 --- /dev/null +++ b/interface/controllers/district.controller.js @@ -0,0 +1,19 @@ +const db = require("../models"); +const district = db.district_master; + +exports.allDistricts = () => { + return district.findAll({ + attributes: ['district_id', 'district_name'] + }) +}; + + +exports.allDistrictsByStateId = (state_id) => { + const query =`SELECT * FROM district_master WHERE state_id = ${state_id}`; + return db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); +}; + + + + + diff --git a/interface/controllers/mandal.controller.js b/interface/controllers/mandal.controller.js new file mode 100644 index 0000000..43842fe --- /dev/null +++ b/interface/controllers/mandal.controller.js @@ -0,0 +1,16 @@ +const db = require("../models"); +const mandal = db.mandal_master; + + +exports.allMandals = () => { + return mandal.findAll({ + attributes: ['mandal_id', 'mandal_name'] + }) +}; + + +exports.allMandalsByDistrictId = (district_id) => { + const query =`SELECT * FROM mandal_master WHERE district_id = ${district_id}`; + return db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); + +}; diff --git a/interface/controllers/state.controller.js b/interface/controllers/state.controller.js new file mode 100644 index 0000000..1a3a4ed --- /dev/null +++ b/interface/controllers/state.controller.js @@ -0,0 +1,13 @@ +const db = require("../models"); +const state = db.state_master; + +// function to get all states +exports.allStates = () => { + const query = "SELECT * FROM state_master"; + return db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); + +}; + + + + diff --git a/interface/controllers/village.controller.js b/interface/controllers/village.controller.js new file mode 100644 index 0000000..0a488d4 --- /dev/null +++ b/interface/controllers/village.controller.js @@ -0,0 +1,18 @@ +const db = require("../models"); +const village = db.village_master; + + +exports.allVillages = () => { + return village.findAll({ + attributes: ['village_id', 'village_name'] + }) +}; + + +exports.allVillagesByMandalId = (mandal_id) => { + const query =`SELECT * FROM village_master WHERE mandal_id = ${mandal_id}`; + return db.sequelize.query(query, { type: db.sequelize.QueryTypes.SELECT }); + +}; + + diff --git a/interface/models/state_master.model.js b/interface/models/state_master.model.js index e3129dd..450b90e 100644 --- a/interface/models/state_master.model.js +++ b/interface/models/state_master.model.js @@ -7,7 +7,7 @@ module.exports = (Sequelize, sequelize) => { autoIncrement: true, }, state_name: { - type: Sequelize.STRING(155), + type: Sequelize.STRING(255), allowNull: false, }, }); diff --git a/interface/public/citizens.js b/interface/public/citizens.js index ef112d1..27130ec 100644 --- a/interface/public/citizens.js +++ b/interface/public/citizens.js @@ -41,3 +41,18 @@ function editCitizen(event) { }, }); } + +function deleteCitizenRecord(citizen) { + citizen_id = JSON.parse(citizen).citizen_id; + $.ajax({ + url: "/api/citizens/delete", + type: "POST", + data: { citizen_id }, + success: function (response) { + console.log(response); + location.reload(); + } + }); + + +} \ No newline at end of file diff --git a/interface/routes/api/citizens.js b/interface/routes/api/citizens.js index bab5222..6dad084 100644 --- a/interface/routes/api/citizens.js +++ b/interface/routes/api/citizens.js @@ -18,4 +18,38 @@ router.post('/edit', (req, res) => { } }); + +router.post('/delete', (req, res) => { + const { citizen_id } = req.body; + if (!citizen_id) { + res.status(400).json({ message: "Please fill in all fields" }); + } else { + citizensController.deleteCitizenbyId(citizen_id) + .then(() => { + res.status(200).json({ message: "Citizen deleted successfully" }); + }) + .catch((err) => { + res.status(400).json({ message: err }); + }); + } +} ); + + +router.post('/validate', (req, res) => { + const { citizen_id } = req.body; + if (!citizen_id) { + res.status(400).json({ message: "Please fill in all fields" }); + } else { + citizensController.checkCitizenId(citizen_id) + .then(( isValid ) => { + res.status(200).json({ isValid: !isValid }); + } + ) + .catch((err) => { + res.status(400).json({ message: err }); + }); + } +}); + + module.exports = router; \ No newline at end of file diff --git a/interface/routes/api/geography.js b/interface/routes/api/geography.js new file mode 100644 index 0000000..4667db8 --- /dev/null +++ b/interface/routes/api/geography.js @@ -0,0 +1,42 @@ +const express = require("express"); +const router = express.Router(); +const stateController = require("../../controllers/state.controller"); +const mandalController = require("../../controllers/mandal.controller"); +const villageController = require("../../controllers/village.controller"); +const districtController = require("../../controllers/district.controller"); + + +router.get("/states", (req, res) => { + stateController.allStates().then(states => { + res.send(states); + }); + +}); + +router.get("/districts/:state_id", (req, res) => { + const state_id = req.params.state_id; + districtController.allDistrictsByStateId(state_id).then(districts => { + res.send(districts); + }); +}); +router.get("/mandals/:district_id", (req, res) => { + const district_id = req.params.district_id; + mandalController.allMandalsByDistrictId(district_id).then(mandals => { + res.send(mandals); + }); +}); + +router.get("/villages/:mandal_id", (req, res) => { + const mandal_id = req.params.mandal_id; + villageController.allVillagesByMandalId(mandal_id).then(villages => { + res.send(villages); + }); +}); + + + + +module.exports = router; + + + diff --git a/interface/routes/index.js b/interface/routes/index.js index cdab54d..b99abbf 100644 --- a/interface/routes/index.js +++ b/interface/routes/index.js @@ -3,10 +3,12 @@ const router = express.Router(); const citizensController = require("../controllers/citizens.controller"); const api = require('./api'); const citizensAPI = require('./api/citizens'); +const geographyAPI = require('./api/geography'); // Setup api routes router.use('/api', api); router.use('/api/citizens', citizensAPI); +router.use('/api/geography', geographyAPI); router.get('/', (req, res) => { @@ -29,5 +31,14 @@ router.get("/citizens", (req, res) => { }); }); + +router.get("/addUser", (req, res) => { + res.render("addUser", { + title: "Add User" + }); + } +); + + // export the router module.exports = router; diff --git a/interface/views/adduser.ejs b/interface/views/adduser.ejs new file mode 100644 index 0000000..1a96601 --- /dev/null +++ b/interface/views/adduser.ejs @@ -0,0 +1,165 @@ + + + + <%- include('partials/head') %> + + + + + <%- include('partials/navbar') %> + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + +
+
+ + +
+
+ Mandal + +
+
+ + +
+ + + + + + + +
Submit
+
+ + +
+ + + <%- include('partials/scripts') %> + + + + diff --git a/interface/views/citizens.ejs b/interface/views/citizens.ejs index a1c6f5e..0e703b2 100644 --- a/interface/views/citizens.ejs +++ b/interface/views/citizens.ejs @@ -13,6 +13,7 @@ style="border: none; box-shadow: none" >
+
View Data
+ + Add User +