-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from Justin-Quinn51/create-jobs-controller
Create jobs controller
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { type Request, type Response } from "express"; | ||
import { | ||
getAllJobs, | ||
getJobById, | ||
createNewJob, | ||
updateJob, | ||
closeJob, | ||
deleteJob, | ||
} from "../models/jobsModel.ts"; | ||
|
||
export async function getJobs(req: Request, res: Response) { | ||
try { | ||
const jobs = await getAllJobs(); | ||
res.status(200).json(jobs); | ||
} catch (error) { | ||
console.error("Error fetching jobs:", error); | ||
res.status(500).json({ error: "Failed to retrieve jobs." }); | ||
} | ||
} | ||
|
||
export async function getJob(req: Request, res: Response) { | ||
const jobId = req.params.id; | ||
try { | ||
const job = await getJobById(jobId); | ||
if (!job) { | ||
res.status(404).json({ error: "Job not found." }); | ||
} | ||
res.status(200).json(job); | ||
} catch (error) { | ||
console.error("Error fetching job:", error); | ||
res.status(500).json({ error: "Failed to retrieve job." }); | ||
} | ||
} | ||
|
||
export async function createJob(req: Request, res: Response) { | ||
const { id, job_title, description, date_added, expires, closed, employer } = | ||
req.body; | ||
try { | ||
const newJob = await createNewJob( | ||
id, | ||
job_title, | ||
description, | ||
new Date(date_added), | ||
new Date(expires), | ||
closed, | ||
employer, | ||
); | ||
res.status(201).json(newJob); | ||
} catch (error) { | ||
console.error("Error adding a new job:", error); | ||
res.status(500).json({ error: "Failed to create job." }); | ||
} | ||
} | ||
|
||
export async function updateJobDetails(req: Request, res: Response) { | ||
const jobId = req.params.id; | ||
const updates = req.body; | ||
|
||
try { | ||
const updatedJob = await updateJob(jobId, updates); | ||
if (!updatedJob) { | ||
return res.status(404).json({ error: "Job not found." }); | ||
} | ||
res.status(200).json(updatedJob); | ||
} catch (error) { | ||
console.error("Error updating a job", error); | ||
res.status(500).json({ error: "Failed to update job" }); | ||
} | ||
} | ||
|
||
export async function closeJobById(req: Request, res: Response) { | ||
const jobId = req.params.id; | ||
try { | ||
const closedJob = await closeJob(jobId, true); | ||
res.status(200).json(closedJob); | ||
} catch (error) { | ||
res.status(500).json({ error: "Failed to close job." }); | ||
} | ||
} | ||
|
||
export async function deleteJobById(req: Request, res: Response) { | ||
const jobId = req.params.id; | ||
try { | ||
await deleteJob(jobId); | ||
res.status(204).send(); | ||
} catch (error) { | ||
res.status(500).json({ error: "Failed to delete job" }); | ||
} | ||
} |