-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hariscs/develop
Develop
- Loading branch information
Showing
5 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,110 @@ | ||
import { Request, Response } from 'express' | ||
import { JOB_SCHEMA } from '../models/job_model' | ||
import { JOB_SCHEMA } from '@models/job_model' | ||
import { isValidObjectId } from 'mongoose' | ||
|
||
//* @desc Get all jobs | ||
//* route GET /api/jobs | ||
//* @access Public | ||
export async function get_jobs(req: Request, res: Response): Promise<void> { | ||
//? @access Public | ||
export async function get_jobs(_: Request, res: Response): Promise<void> { | ||
try { | ||
const jobs = await JOB_SCHEMA.find() | ||
// fetch jobs | ||
const jobs = await JOB_SCHEMA.find({}).sort({ createdAt: -1 }) | ||
res.status(200).json({ jobs }) | ||
} catch (error) { | ||
console.error('Error fetching jobs:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Post a job | ||
//* @desc Get job | ||
//* route GET /api/jobs/:id | ||
//? @access Public | ||
export async function get_job(req: Request, res: Response): Promise<void> { | ||
try { | ||
const { id } = req.params | ||
//* check if id is valid | ||
if (!isValidObjectId(id)) { | ||
res.status(400).json({ error: 'Invalid id' }) | ||
return | ||
} | ||
//* check if job exists | ||
const job = await JOB_SCHEMA.findById(id) | ||
if (!job) { | ||
res.status(404).json({ error: 'Job not found' }) | ||
return | ||
} | ||
res.status(200).json(job) | ||
} catch (error) { | ||
console.error('Error fetching job:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Post job | ||
//* route POST /api/jobs/job | ||
//* @access Private | ||
//! @access Private | ||
export async function post_job(req: Request, res: Response): Promise<void> { | ||
try { | ||
//* post job | ||
const jobData = req.body | ||
const newJob = await JOB_SCHEMA.create(jobData) | ||
res.status(201).json({ message: 'Job posted', newJob }) | ||
res.status(201).json(newJob) | ||
} catch (error) { | ||
console.error('Error fetching jobs:', error) | ||
console.error('Error posting job:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Delete job | ||
//* route DELETE /api/jobs/:id | ||
//! @access Private | ||
export async function delete_job(req: Request, res: Response): Promise<void> { | ||
try { | ||
const { id } = req.params | ||
//* check if id is valid | ||
if (!isValidObjectId(id)) { | ||
res.status(400).json({ error: 'Invalid id' }) | ||
return | ||
} | ||
//* check if job exists | ||
const job = await JOB_SCHEMA.findById(id) | ||
if (!job) { | ||
res.status(404).json({ error: 'Job not found' }) | ||
return | ||
} | ||
//* delete job | ||
const deleted_job = await JOB_SCHEMA.findByIdAndDelete(id) | ||
res.status(200).json({ message: 'Job deleted successfully' }) | ||
} catch (error) { | ||
console.error('Error deleting job:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Update job | ||
//* route PATCH /api/jobs/:id | ||
//! @access Private | ||
export async function update_job(req: Request, res: Response): Promise<void> { | ||
try { | ||
const { id } = req.params | ||
//* check if id is valid | ||
if (!isValidObjectId(id)) { | ||
res.status(400).json({ error: 'Invalid id' }) | ||
return | ||
} | ||
//* check if job exists | ||
const job = await JOB_SCHEMA.findById(id) | ||
if (!job) { | ||
res.status(404).json({ error: 'Job not found' }) | ||
return | ||
} | ||
//* update job | ||
const updated_job = await JOB_SCHEMA.findByIdAndUpdate(id, req.body, { | ||
new: true, | ||
}) | ||
res.status(200).json(updated_job) | ||
} catch (error) { | ||
console.error('Error updating job:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
import express from 'express' | ||
import { get_jobs, post_job } from '../controllers/job_controller' | ||
import { | ||
delete_job, | ||
get_job, | ||
get_jobs, | ||
post_job, | ||
update_job, | ||
} from '@controllers/job_controller' | ||
|
||
const router = express.Router() | ||
|
||
//* @desc Get all jobs | ||
//? @access Public | ||
router.get('/', get_jobs) | ||
router.post('/job', post_job) | ||
|
||
//* @desc Get job | ||
//? @access Public | ||
router.get('/:id', get_job) | ||
|
||
//* @desc Post job | ||
//! @access Private | ||
router.post('/', post_job) | ||
|
||
//* @desc Delete job | ||
//! @access Private | ||
router.delete('/:id', delete_job) | ||
|
||
//* @desc Update job | ||
//! @access Private | ||
router.patch('/:id', update_job) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters