Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anoushka application search backend #1188

Merged
merged 5 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading