├── .gitignore ├── public ├── images │ ├── OG.png │ └── PinClipart.com_recreational-vehicle-clip-art_3227183.png └── main.css ├── src ├── routes │ ├── province.js │ ├── district.js │ └── city.js └── schema │ ├── Districts.js │ ├── Province.js │ └── City.js ├── package.json ├── LICENSE ├── index.js ├── views └── index.ejs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | #node_modules 2 | .DS_Store 3 | node_modules 4 | 5 | #env 6 | .env -------------------------------------------------------------------------------- /public/images/OG.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasinduS96/location-api-sl/HEAD/public/images/OG.png -------------------------------------------------------------------------------- /public/images/PinClipart.com_recreational-vehicle-clip-art_3227183.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasinduS96/location-api-sl/HEAD/public/images/PinClipart.com_recreational-vehicle-clip-art_3227183.png -------------------------------------------------------------------------------- /public/main.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | margin: 0; 4 | padding: 0; 5 | width: 100%; 6 | height: 100%; 7 | display: table; 8 | } 9 | .content { 10 | display: table-cell; 11 | text-align: center; 12 | vertical-align: middle; 13 | } 14 | 15 | .text-main { 16 | font-family: 'Raleway', sans-serif; 17 | font-size: larger; 18 | } 19 | 20 | .styled-text { 21 | font-family: 'Raleway', sans-serif; 22 | } 23 | -------------------------------------------------------------------------------- /src/routes/province.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const express = require('express'); 3 | const router = express.Router(); 4 | 5 | require('../schema/Province') 6 | const province = mongoose.model('provinces'); 7 | 8 | router.get('/', (req,res) => { 9 | province.find().then(result => { 10 | res.send(result); 11 | }).catch(err =>{ 12 | console.log(err); 13 | }) 14 | }) 15 | 16 | module.exports = router; -------------------------------------------------------------------------------- /src/schema/Districts.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const DistrictSchema = mongoose.Schema({ 4 | id: { 5 | type: Number, 6 | required: true 7 | }, 8 | province_id: { 9 | type: Number, 10 | required: true 11 | }, 12 | name_en: { 13 | type: String, 14 | required: true 15 | }, 16 | name_si: { 17 | type: String, 18 | required: true 19 | }, 20 | name_ta: { 21 | type: String, 22 | required: true 23 | } 24 | }) 25 | 26 | mongoose.model('districts', DistrictSchema); -------------------------------------------------------------------------------- /src/schema/Province.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const ProvinceSchema = mongoose.Schema({ 4 | id: { 5 | type: Number, 6 | required: true 7 | }, 8 | province_id: { 9 | type: Number, 10 | required: true 11 | }, 12 | name_en: { 13 | type: String, 14 | required: true 15 | }, 16 | name_si: { 17 | type: String, 18 | required: true 19 | }, 20 | name_ta: { 21 | type: String, 22 | required: true 23 | } 24 | }) 25 | 26 | mongoose.model('provinces', ProvinceSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "location_api", 3 | "version": "1.0.0", 4 | "description": "This API can be use to all developers to get location details of Sri Lanka including major cities, sub areas, districts and Provinces", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "nodemon app.js" 9 | }, 10 | "author": "Pasindu Senarathne", 11 | "license": "MIT", 12 | "dependencies": { 13 | "body-parser": "^1.19.0", 14 | "cors": "^2.8.5", 15 | "dotenv": "^10.0.0", 16 | "ejs": "^3.1.6", 17 | "express": "^4.17.1", 18 | "express-ejs-layouts": "^2.5.0", 19 | "mongoose": "^5.12.12", 20 | "nodemon": "^2.0.7" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/routes/district.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const express = require('express'); 3 | const router = express.Router(); 4 | 5 | require('../schema/Districts') 6 | const districts = mongoose.model('districts'); 7 | 8 | router.get('/', (req,res) => { 9 | districts.find().then(result => { 10 | res.send(result); 11 | }).catch(err =>{ 12 | console.log(err); 13 | }) 14 | }) 15 | 16 | router.get('/byprovince/:id', (req,res) => { 17 | districts.find({province_id: req.params.id},{ name_en: 1, name_si: 1, name_ta:1 }).then(result => { 18 | res.send(result); 19 | }).catch(err =>{ 20 | console.log(err); 21 | }) 22 | }) 23 | 24 | router.get('/districtcount/:id', (req,res) => { 25 | districts.countDocuments({province_id: req.params.id}).then(result => { 26 | res.status(200).send({count: result});; 27 | }).catch(err =>{ 28 | console.log(err); 29 | }) 30 | }) 31 | 32 | 33 | 34 | 35 | module.exports = router; -------------------------------------------------------------------------------- /src/schema/City.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const CitySchema = mongoose.Schema({ 4 | district_id: { 5 | type: Number, 6 | required: true 7 | }, 8 | name_en: { 9 | type: String, 10 | required: true 11 | }, 12 | name_si: { 13 | type: String, 14 | required: true 15 | }, 16 | name_ta: { 17 | type: String, 18 | required: true 19 | }, 20 | sub_name_en: { 21 | type: String, 22 | required: true 23 | }, 24 | sub_name_si: { 25 | type: String, 26 | required: true 27 | }, 28 | sub_name_ta: { 29 | type: String, 30 | required: true 31 | }, 32 | postcode: { 33 | type: String, 34 | required: true 35 | }, 36 | latitude: { 37 | type: String, 38 | required: true 39 | }, 40 | longitude: { 41 | type: String, 42 | required: true 43 | }, 44 | }) 45 | 46 | mongoose.model('cities', CitySchema); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Pasindu Senarathne 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const mongoose = require('mongoose'); 4 | const bodyParser = require('body-parser'); 5 | const cors = require('cors'); 6 | 7 | const cities = require('./src/routes/city'); 8 | const districts = require('./src/routes/district'); 9 | const provinces = require('./src/routes/province'); 10 | 11 | require("dotenv").config(); 12 | 13 | app.use(bodyParser.urlencoded({extended: false})) 14 | app.use(bodyParser.json()); 15 | 16 | app.use(express.static('public')) 17 | 18 | app.use(cors()) 19 | 20 | app.set('view engine', 'ejs') 21 | 22 | app.listen(process.env.PORT, () =>{ 23 | 24 | console.log(`server started at ${process.env.PORT}`); 25 | 26 | //Connect to the database 27 | mongoose.Promise = global.Promise; 28 | mongoose 29 | .connect(process.env.MONGO_URI,{ 30 | useFindAndModify: false, 31 | useNewUrlParser: true, 32 | useUnifiedTopology: true 33 | }, (err, db) =>{ 34 | if(!err){ 35 | console.log("MongoDB Connected Successfully!!") 36 | }else{ 37 | console.log(err); 38 | } 39 | }) 40 | }) 41 | 42 | app.get('/', (req,res) => { 43 | res.render('index') 44 | }) 45 | 46 | app.use('/cities', cities) 47 | app.use('/districts', districts) 48 | app.use('/provinces', provinces) -------------------------------------------------------------------------------- /src/routes/city.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const express = require('express'); 3 | const router = express.Router(); 4 | 5 | require('../schema/City') 6 | const city = mongoose.model('cities'); 7 | 8 | router.get('/', (req,res) => { 9 | city.find().then(result => { 10 | res.send(result); 11 | }).catch(err =>{ 12 | console.log(err); 13 | }) 14 | }) 15 | 16 | router.get('/cordinates/:city', (req,res) => { 17 | city.find({name_en: req.params.city},{ name_en: 1,latitude: 1, longitude: 1, _id: 0 }).then(result => { 18 | res.send(result); 19 | }).catch(err =>{ 20 | console.log(err); 21 | }) 22 | }) 23 | 24 | router.get('/byDistrict/:id', (req,res) => { 25 | city.find({district_id: req.params.id}).then(result => { 26 | res.send(result); 27 | }).catch(err =>{ 28 | console.log(err); 29 | }) 30 | }) 31 | 32 | router.get('/colombo_mca/:id', (req,res) => { 33 | city.find({name_en: "Colombo "+ req.params.id}).then(result => { 34 | res.send(result); 35 | }).catch(err =>{ 36 | console.log(err); 37 | }) 38 | }) 39 | 40 | router.get('/colombo_mca', (req,res) => { 41 | city.find({name_en: /Colombo/}).then(result => { 42 | res.send(result); 43 | }).catch(err =>{ 44 | console.log(err); 45 | }) 46 | }) 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | module.exports = router; -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 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 |
60 |