Skip to content

Commit

Permalink
add city routes and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ridhlab committed Mar 31, 2023
1 parent 5194079 commit dbf7892
Show file tree
Hide file tree
Showing 5 changed files with 691 additions and 16 deletions.
36 changes: 21 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{
"name": "jadwal-adzan-api",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.3.4",
"cheerio": "^1.0.0-rc.12",
"dotenv": "^16.0.3"
}
"name": "jadwal-adzan-api",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"devDependencies": {
"nodemon": "^2.0.22"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.js",
"dev": "nodemon src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.3.4",
"cheerio": "^1.0.0-rc.12",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2"
}
}
22 changes: 22 additions & 0 deletions src/controllers/city.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { data: cities } = require("../data/city.json");

class CityController {
static getAllCity(req, res) {
return res.status(200).send({ statusCode: 200, data: cities });
}

static getCity(req, res) {
let id = req.params.id;
id = parseInt(id);

const city = cities.find((item) => item.id === id);

if (!city) {
return res.status(400).send({ statusCode: 400, message: "City not found" });
}

return res.status(200).send({ statusCode: 200, data: city });
}
}

module.exports = CityController;
18 changes: 18 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require("dotenv").config();

const express = require("express");
const cors = require("cors");
const routes = require("./routes");

const port = process.env.PORT || 3000;
const server = express();

server.set("trust proxy", 1);
server.use(cors());
server.use(express.json());

server.use(routes);

server.listen(port, () => {
console.log("Server running at port:", port);
});
13 changes: 13 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { Router } = require("express");
const CityController = require("./controllers/city");

const router = Router();

router.get("/", (req, res) => {
res.status(200).send({ message: "success" });
});

router.get("/city", CityController.getAllCity);
router.get("/city/:id", CityController.getCity);

module.exports = router;
Loading

0 comments on commit dbf7892

Please sign in to comment.