Skip to content

Commit

Permalink
Merge pull request #1188 from OneCommunityGlobal/Anoushka-application…
Browse files Browse the repository at this point in the history
…-search

Anoushka application search
  • Loading branch information
one-community authored Dec 29, 2024
2 parents 70b6b76 + b0641f5 commit 28dd3f4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 14 deletions.
126 changes: 113 additions & 13 deletions src/controllers/jobsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion src/routes/jobsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 28dd3f4

Please sign in to comment.