Skip to content

Commit

Permalink
fix: The backend calls the /filter APIs now
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSloanRanger committed Jan 31, 2024
1 parent 3b5a2fb commit a65f529
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 90 deletions.
15 changes: 0 additions & 15 deletions be/database.js

This file was deleted.

54 changes: 46 additions & 8 deletions be/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"axios": "^1.6.5",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.1",
"express": "^4.18.2",
Expand Down
153 changes: 97 additions & 56 deletions be/routes.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,76 @@
const express = require('express');
const express = require("express");
const router = express.Router();
const { fetchFromAPI, formatDataForDisplay, groupAndFormatOpeningHours } = require('./utils');
const {
fetchFromAPI,
formatDataForDisplay,
groupAndFormatOpeningHours,

Check failure on line 6 in be/routes.js

View workflow job for this annotation

GitHub Actions / BE-build

'groupAndFormatOpeningHours' is assigned a value but never used
} = require("./utils");

const axios = require("axios");
const bodyParser = require("body-parser");

let branchesData = [];
let atmsData = [];

router.use(bodyParser.json());

// Function to fetch and store data
async function fetchDataAndStore() {
try {
branchesData = await fetchFromAPI("branches", {});
atmsData = await fetchFromAPI("atms", {});
} catch (error) {
console.error("Error fetching and storing data:", error.message);
}
try {
branchesData = await fetchFromAPI("branches", {});
atmsData = await fetchFromAPI("atms", {});
} catch (error) {
console.error("Error fetching and storing data:", error.message);
}
}

fetchDataAndStore();

// Root endpoint
router.get("/", (req, res) => {
res.send("Hello World!");
res.send("Hello World!");
});

// backend endpoint for getting branches from API
router.get("/branches", async (req, res) => {
try {
// fetching branch data from the API and returning as JSON
const data = await fetchFromAPI("branches", req.query);
res.json(data);
} catch (error) {
// error handling for fetch operations
res.status(500).send("Error processing request");
}
try {
// fetching branch data from the API and returning as JSON
const data = await fetchFromAPI("branches", req.query);
res.json(data);
} catch (error) {
// error handling for fetch operations
res.status(500).send("Error processing request");
}
});

// backend endpoint for getting ATMs from API
router.get("/atms", async (req, res) => {
try {
// fetching ATM data from the API and returning as JSON
const data = await fetchFromAPI("atms", req.query, );
res.json(data);
} catch (error) {
// error handling for fetch operations
res.status(500).send("Error processing request");
}
try {
// fetching ATM data from the API and returning as JSON
const data = await fetchFromAPI("atms", req.query);
res.json(data);
} catch (error) {
// error handling for fetch operations
res.status(500).send("Error processing request");
}
});

// endpoint to get formatted data for list view
router.get("/list-view-data", (req, res) => {
try {
// combine and format data from branches and ATMs
const formattedBranches = formatDataForDisplay(branchesData, false);
const formattedATMs = formatDataForDisplay(atmsData, true);

// combine formatted data from both branches and ATMs
const combinedData = [...formattedBranches, ...formattedATMs];

// send the combined data as a response
res.json(combinedData);
} catch (error) {
console.error("Error fetching data for list view:", error.message);
res.status(500).send("Error processing request");
}
try {
// combine and format data from branches and ATMs
const formattedBranches = formatDataForDisplay(branchesData, false);
const formattedATMs = formatDataForDisplay(atmsData, true);

// combine formatted data from both branches and ATMs
const combinedData = [...formattedBranches, ...formattedATMs];

// send the combined data as a response
res.json(combinedData);
} catch (error) {
console.error("Error fetching data for list view:", error.message);
res.status(500).send("Error processing request");
}
});

/*router.get("/map-locations", (req, res) => {
Expand All @@ -72,27 +81,59 @@ router.get("/list-view-data", (req, res) => {
});*/

router.post("/atms/filter", async (req, res) => {
try {
// Sending a filtered request to the API and getting the response
const filteredAtms = await fetchFromAPI("atms/filter", req.body, 'POST');
res.json(filteredAtms);
} catch (error) {
console.error("Error in /atms/filter route:", error.message);
res.status(500).send("Error processing request");
}
const {
Accessibility,
ATMServices,
Access24HoursIndicator,
Latitude,
Longitude,
Radius,
} = req.body;

const filterATMsConfig = {
method: "post",
url: "https://wombo-412213.nw.r.appspot.com/api/atms/filter",
data: {
Accessibility: Accessibility,
ATMServices: ATMServices,
Access24HoursIndicator: Access24HoursIndicator,
Latitude: Latitude,
Longitude: Longitude,
Radius: Radius,
},
headers: {
"Content-Type": "application/json",
},
};

const filteredAtms = await axios(filterATMsConfig);

res.json(filteredAtms.data);
});

// Route for getting filtered branches
router.post("/branches/filter", async (req, res) => {
try {
// Sending a filtered request to the API and getting the response
const filteredBranches = await fetchFromAPI("branches/filter", req.body, 'POST');
res.json(filteredBranches);
} catch (error) {
console.error("Error in /branches/filter route:", error.message);
res.status(500).send("Error processing request");
}
});
const { Accessibility, ServiceAndFacility, Latitude, Longitude, Radius } =
req.body;

const filterBranchesConfig = {
method: "post",
url: "https://wombo-412213.nw.r.appspot.com/api/branches/filter",
data: {
Accessibility: Accessibility,
ServiceAndFacility: ServiceAndFacility,
Latitude: Latitude,
Longitude: Longitude,
Radius: Radius,
},
headers: {
"Content-Type": "application/json",
},
};

const filteredBranches = await axios(filterBranchesConfig);

res.json(filteredBranches.data);
});

module.exports = router;
20 changes: 9 additions & 11 deletions be/server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
require('dotenv').config();
require("dotenv").config();

const express = require('express');
const cors = require('cors');
const connectDB = require('./database'); // Assuming you have this module for DB connection
const routes = require('./routes');
const express = require("express");
const cors = require("cors");
const routes = require("./routes");
const bodyParser = require("body-parser");

const app = express();
app.use(cors());

// Connect to MongoDB
connectDB();
app.use(bodyParser.json());

// Use the routes defined in routes.js
app.use('/', routes);
app.use("/", routes);

const PORT = process.env.PORT || 3000;
if (process.env.NODE_ENV !== 'test') {
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
if (process.env.NODE_ENV !== "test") {
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
}

module.exports = app;

0 comments on commit a65f529

Please sign in to comment.