diff --git a/src/controllers/jobsController.js b/src/controllers/jobsController.js index 65cfeafdc..81e7c3cb3 100644 --- a/src/controllers/jobsController.js +++ b/src/controllers/jobsController.js @@ -39,6 +39,115 @@ const getJobs = async (req, res) => { } }; +// Controller to fetch job summaries with pagination, search, filtering, and sorting +const getJobSummaries = async (req, res) => { + const { search = '', page = 1, limit = 18, category = '' } = req.query; + + try { + const pageNumber = Math.max(1, parseInt(page, 10)); + const limitNumber = Math.max(1, parseInt(limit, 10)); + + // Construct the query object + const query = {}; + if (search) query.title = { $regex: search, $options: 'i' }; + if (category) query.category = category; + + // Sorting logic + const sortCriteria = { + title: 1, + datePosted: -1, + featured: -1 + }; + + // Fetch the total number of jobs matching the query for pagination + const totalJobs = await Job.countDocuments(query); + const jobs = await Job.find(query) + .select('title category location description datePosted featured') + .sort(sortCriteria) + .skip((pageNumber - 1) * limitNumber) + .limit(limitNumber); + + res.json({ + jobs, + pagination: { + totalJobs, + totalPages: Math.ceil(totalJobs / limitNumber), // Calculate total number of pages + currentPage: pageNumber, + limit: limitNumber, + hasNextPage: pageNumber < Math.ceil(totalJobs / limitNumber), + hasPreviousPage: pageNumber > 1, + }, + }); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch job summaries', details: error.message }); + } +}; + +// Controller to fetch job title suggestions for a dropdown +const getJobTitleSuggestions = async (req, res) => { + const { query = '' } = req.query; + + try { + const suggestions = await Job.find({ title: { $regex: query, $options: 'i' } }) + .distinct('title'); + + res.json({ suggestions }); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch job title suggestions', details: error.message }); + } +}; + +const resetJobsFilters = async (req, res) => { + const { page = 1, limit = 18 } = req.query; + + try { + // Validate pagination parameters + const pageNumber = Math.max(1, parseInt(page, 10)); + const limitNumber = Math.max(1, parseInt(limit, 10)); + + // Sorting logic + const sortCriteria = { + title: 1, + datePosted: -1, + featured: -1 + }; + // Fetch all jobs without filtering + const totalJobs = await Job.countDocuments({}); + const jobs = await Job.find({}) + .sort(sortCriteria) + .skip((pageNumber - 1) * limitNumber) + .limit(limitNumber); + + // Respond with all jobs and pagination metadata + res.json({ + jobs, + pagination: { + totalJobs, + totalPages: Math.ceil(totalJobs / limitNumber), + currentPage: pageNumber, + limit: limitNumber, + hasNextPage: pageNumber < Math.ceil(totalJobs / limitNumber), + hasPreviousPage: pageNumber > 1, + }, + }); + } catch (error) { + res.status(500).json({ error: 'Failed to reset filters or reload jobs', details: error.message }); + } +}; + +const getCategories = async (req, res) => { + try { + const categories = await Job.distinct('category', {}); + + // Sort categories alphabetically + categories.sort((a, b) => a.localeCompare(b)); + + res.status(200).json({ categories }); + } catch (error) { + console.error('Error fetching categories:', error); + res.status(500).json({ message: 'Failed to fetch categories' }); + } +}; // Controller to fetch job details by ID const getJobById = async (req, res) => { const { id } = req.params; @@ -106,26 +215,17 @@ const deleteJob = async (req, res) => { } }; -const getCategories = async (req, res) => { - try { - const categories = await Job.distinct('category', {}); - - // Sort categories alphabetically - categories.sort((a, b) => a.localeCompare(b)); - res.status(200).json({ categories }); - } catch (error) { - console.error('Error fetching categories:', error); - res.status(500).json({ message: 'Failed to fetch categories' }); - } -}; // Export controllers as a plain object module.exports = { getJobs, + getJobTitleSuggestions, getJobById, createJob, updateJob, deleteJob, - getCategories, + getJobSummaries, + resetJobsFilters, + getCategories }; diff --git a/src/routes/jobsRouter.js b/src/routes/jobsRouter.js index 5b66735a6..6587ebb92 100644 --- a/src/routes/jobsRouter.js +++ b/src/routes/jobsRouter.js @@ -4,11 +4,13 @@ const jobsController = require('../controllers/jobsController'); // Adjust the p const router = express.Router(); // Define routes +router.get('/suggestions', jobsController.getJobTitleSuggestions); +router.get('/reset-filters', jobsController.resetJobsFilters); +router.get('/summaries', jobsController.getJobSummaries); router.get('/', jobsController.getJobs); router.get('/categories', jobsController.getCategories); router.get('/:id', jobsController.getJobById); router.post('/', jobsController.createJob); router.put('/:id', jobsController.updateJob); router.delete('/:id', jobsController.deleteJob); - module.exports = router;