diff --git a/backend/controllers/busController.js b/backend/controllers/busController.js index 45cc72a..791388a 100644 --- a/backend/controllers/busController.js +++ b/backend/controllers/busController.js @@ -1,6 +1,7 @@ const asyncHandler = require("express-async-handler"); const Bus = require("../models/busModel"); const Station = require("../models/stationModel"); +const { paginatedResults } = require("../utils/paginationFunction"); const ticketPrice = parseInt(process.env.ONE_STATION_PRICE, 10); const travelTime = parseInt(process.env.ONE_STATION_TRAVEL_TIME, 10); @@ -198,6 +199,13 @@ const createStation = asyncHandler(async (req, res) => { res.status(201).json(bus); }); +//@desc get stations pagination +//@route get /api/BRT/stations?page_size=3&page_number=1 +//@access public +const getStationPagesList = asyncHandler(async (req, res) => { + res.status(200).json(res.paginatedResults); +}); + module.exports = { getBusList, getBus, @@ -207,4 +215,5 @@ module.exports = { getRouteBuses, getStationList, createStation, + getStationPagesList, }; diff --git a/backend/routes/routes.js b/backend/routes/routes.js index be87f91..db4113f 100644 --- a/backend/routes/routes.js +++ b/backend/routes/routes.js @@ -8,7 +8,10 @@ const { getRouteBuses, getStationList, createStation, + getStationPagesList, } = require("../controllers/busController"); +const { paginatedResults } = require("../utils/paginationFunction"); +const stationModel = require("../models/stationModel"); const router = express.Router(); @@ -18,6 +21,10 @@ router.route("/").post(createBus); router.route("/stations").get(getStationList).post(createStation); +router + .route("/station") + .get(paginatedResults(stationModel), getStationPagesList); + router.route("/:id").get(getBus).put(updateBus).delete(deleteBus); router.route("/:start_station/:end_station").get(getRouteBuses); diff --git a/backend/utils/paginationFunction.js b/backend/utils/paginationFunction.js new file mode 100644 index 0000000..7a6cb32 --- /dev/null +++ b/backend/utils/paginationFunction.js @@ -0,0 +1,48 @@ +function paginatedResults(model) { + return async (req, res, next) => { + const page = parseInt(req.query.page) || 1; + const limit = parseInt(req.query.limit) || 10; // Default limit is 10 + const stationName = req.query.station; // Station name filter + + const startIndex = (page - 1) * limit; + const endIndex = page * limit; + + const results = {}; + + const query = {}; // Initialize empty query object + + // Add station name filter to the query if provided + if (stationName) { + query.stationName = stationName; + } + + if (endIndex < (await model.countDocuments().exec())) { + results.next = { + page: page + 1, + limit: limit, + }; + } + + if (startIndex > 0) { + results.previous = { + page: page - 1, + limit: limit, + }; + } + try { + results.results = await model + .find(query) + .limit(limit) + .skip(startIndex) + .exec(); + res.paginatedResults = results; + next(); + } catch (e) { + res.status(500).json({ message: e.message }); + } + }; +} + +module.exports = { + paginatedResults, +}; diff --git a/frontend/ios/brt_design.xcodeproj/project.pbxproj b/frontend/ios/brt_design.xcodeproj/project.pbxproj index 9f2d8a5..8f923d6 100644 --- a/frontend/ios/brt_design.xcodeproj/project.pbxproj +++ b/frontend/ios/brt_design.xcodeproj/project.pbxproj @@ -489,9 +489,9 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = brt_design/brt_design.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = 5G6Q76UWAG; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = brt_design/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = "BRTS Search"; @@ -522,9 +522,9 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = brt_design/brt_design.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = 5G6Q76UWAG; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = brt_design/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = "BRTS Search"; LD_RUNPATH_SEARCH_PATHS = (