Skip to content

Commit

Permalink
Merge pull request #1152 from OneCommunityGlobal/Sam-Add-Jobs-Backend
Browse files Browse the repository at this point in the history
Sam add jobs backend
  • Loading branch information
one-community authored Nov 21, 2024
2 parents 5ffa5bc + a8f5cb8 commit e761275
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
117 changes: 117 additions & 0 deletions src/controllers/jobsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const Job = require('../models/jobs'); // Import the Job model

// Controller to fetch all jobs with pagination, search, and filtering
const getJobs = async (req, res) => {
const { page = 1, limit = 18, search = '', category = '' } = req.query;

try {
// Validate query parameters
const pageNumber = Math.max(1, parseInt(page, 10)); // Ensure page is at least 1
const limitNumber = Math.max(1, parseInt(limit, 10)); // Ensure limit is at least 1

// Build query object
const query = {};
if (search) query.title = { $regex: search, $options: 'i' }; // Case-insensitive search
if (category) query.category = category;

// Fetch total count for pagination metadata
const totalJobs = await Job.countDocuments(query);

// Fetch paginated results
const jobs = await Job.find(query)
.skip((pageNumber - 1) * limitNumber)
.limit(limitNumber);

// Prepare response
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 fetch jobs', details: error.message });
}
};

// Controller to fetch job details by ID
const getJobById = async (req, res) => {
const { id } = req.params;

try {
const job = await Job.findById(id);
if (!job) {
return res.status(404).json({ error: 'Job not found' });
}
res.json(job);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch job', details: error.message });
}
};

// Controller to create a new job
const createJob = async (req, res) => {
const { title, category, description, imageUrl, location, applyLink, jobDetailsLink } =
req.body;

try {
const newJob = new Job({
title,
category,
description,
imageUrl,
location,
applyLink,
jobDetailsLink,
});

const savedJob = await newJob.save();
res.status(201).json(savedJob);
} catch (error) {
res.status(500).json({ error: 'Failed to create job', details: error.message });
}
};

// Controller to update an existing job by ID
const updateJob = async (req, res) => {
const { id } = req.params;

try {
const updatedJob = await Job.findByIdAndUpdate(id, req.body, { new: true });
if (!updatedJob) {
return res.status(404).json({ error: 'Job not found' });
}
res.json(updatedJob);
} catch (error) {
res.status(500).json({ error: 'Failed to update job', details: error.message });
}
};

// Controller to delete a job by ID
const deleteJob = async (req, res) => {
const { id } = req.params;

try {
const deletedJob = await Job.findByIdAndDelete(id);
if (!deletedJob) {
return res.status(404).json({ error: 'Job not found' });
}
res.json({ message: 'Job deleted successfully' });
} catch (error) {
res.status(500).json({ error: 'Failed to delete job', details: error.message });
}
};

// Export controllers as a plain object
module.exports = {
getJobs,
getJobById,
createJob,
updateJob,
deleteJob,
};
17 changes: 17 additions & 0 deletions src/models/jobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const jobSchema = new Schema({
title: { type: String, required: true }, // Job title
category: { type: String, required: true }, // General category (e.g., Engineering, Marketing)
description: { type: String, required: true }, // Detailed job description
imageUrl: { type: String, required: true }, // URL of the job-related image
location: { type: String, required: true }, // Job location (optional for remote jobs)
applyLink: { type: String, required: true }, // URL for the application form
featured: { type: Boolean, default: false }, // Whether the job should be featured prominently
datePosted: { type: Date, default: Date.now }, // Date the job was posted
jobDetailsLink: { type: String, required: true }, // Specific job details URL
});

module.exports = mongoose.model('Job', jobSchema);
13 changes: 13 additions & 0 deletions src/routes/jobsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');
const jobsController = require('../controllers/jobsController'); // Adjust the path if needed

const router = express.Router();

// Define routes
router.get('/', jobsController.getJobs);
router.get('/:id', jobsController.getJobById);
router.post('/', jobsController.createJob);
router.put('/:id', jobsController.updateJob);
router.delete('/:id', jobsController.deleteJob);

module.exports = router;
4 changes: 4 additions & 0 deletions src/startup/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module.exports = function (app) {
next();
return;
}
if (req.originalUrl.startsWith('/api/jobs') && req.method === 'GET') {
next();
return;
}
if (!req.header('Authorization')) {
res.status(401).send({ 'error:': 'Unauthorized request' });
return;
Expand Down
3 changes: 2 additions & 1 deletion src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const project = require('../models/project');
const information = require('../models/information');
const team = require('../models/team');
// const actionItem = require('../models/actionItem');
const notification = require('../models/notification');
const wbs = require('../models/wbs');
const task = require('../models/task');
const popup = require('../models/popupEditor');
Expand Down Expand Up @@ -55,6 +54,7 @@ const timeEntryRouter = require('../routes/timeentryRouter')(timeEntry);
const projectRouter = require('../routes/projectRouter')(project);
const informationRouter = require('../routes/informationRouter')(information);
const teamRouter = require('../routes/teamRouter')(team);
const jobsRouter = require('../routes/jobsRouter');
// const actionItemRouter = require('../routes/actionItemRouter')(actionItem);
const notificationRouter = require('../routes/notificationRouter')();
const loginRouter = require('../routes/loginRouter')();
Expand Down Expand Up @@ -162,6 +162,7 @@ module.exports = function (app) {
app.use('/api', timeOffRequestRouter);
app.use('/api', followUpRouter);
app.use('/api', blueSquareEmailAssignmentRouter);
app.use('/api/jobs', jobsRouter)
// bm dashboard
app.use('/api/bm', bmLoginRouter);
app.use('/api/bm', bmMaterialsRouter);
Expand Down

0 comments on commit e761275

Please sign in to comment.