From 861fa38812ce044e14f0af119666de2e6555f376 Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sat, 24 Feb 2024 19:12:39 +0500 Subject: [PATCH 1/3] feat: add CRUD routes and controllers for jobs --- apps/api/src/controllers/job_controller.ts | 95 ++++++++++++++++++++-- apps/api/src/routes/job_route.ts | 25 +++++- 2 files changed, 113 insertions(+), 7 deletions(-) diff --git a/apps/api/src/controllers/job_controller.ts b/apps/api/src/controllers/job_controller.ts index 210b348..f768de6 100644 --- a/apps/api/src/controllers/job_controller.ts +++ b/apps/api/src/controllers/job_controller.ts @@ -1,12 +1,14 @@ import { Request, Response } from 'express' 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 { +//? @access Public +export async function get_jobs(_: Request, res: Response): Promise { + // fetch jobs try { - const jobs = await JOB_SCHEMA.find() + const jobs = await JOB_SCHEMA.find({}).sort({ createdAt: -1 }) res.status(200).json({ jobs }) } catch (error) { console.error('Error fetching jobs:', error) @@ -14,14 +16,95 @@ export async function get_jobs(req: Request, res: Response): Promise { } } -//* @desc Post a job +//* @desc Get job +//* route GET /api/jobs/:id +//? @access Public +export async function get_job(req: Request, res: Response): Promise { + 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 + } + // fetch job + try { + const job = await JOB_SCHEMA.findById(id) + res.status(200).json(job) + } catch (error) { + console.error('Error fetching jobs:', 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 { + //* post job try { 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) + 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 { + 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 + try { + const deleted_job = await JOB_SCHEMA.findByIdAndDelete(id) + res.status(200).json(deleted_job) + } catch (error) { + console.error('Error fetching jobs:', 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 { + 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 + try { + const updated_job = await JOB_SCHEMA.findByIdAndUpdate(id, req.body, { + new: true, + }) + res.status(200).json(updated_job) } catch (error) { console.error('Error fetching jobs:', error) res.status(500).json({ error: 'Internal server error' }) diff --git a/apps/api/src/routes/job_route.ts b/apps/api/src/routes/job_route.ts index 2df82bc..cf9f494 100644 --- a/apps/api/src/routes/job_route.ts +++ b/apps/api/src/routes/job_route.ts @@ -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) + +//* @desc Get job +//? @access Public +router.get('/:id', get_job) + +//* @desc Post job +//! @access Private router.post('/job', 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 From 24bc5e64f87053787bdb77fce473d8c10dc54367 Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sat, 24 Feb 2024 19:24:24 +0500 Subject: [PATCH 2/3] refactor: add absolute paths --- apps/api/src/controllers/job_controller.ts | 2 +- apps/api/src/index.ts | 2 +- apps/api/src/routes/job_route.ts | 4 ++-- apps/api/src/server.ts | 2 +- apps/api/tsconfig.json | 11 ++++++++++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/apps/api/src/controllers/job_controller.ts b/apps/api/src/controllers/job_controller.ts index f768de6..0d18322 100644 --- a/apps/api/src/controllers/job_controller.ts +++ b/apps/api/src/controllers/job_controller.ts @@ -1,5 +1,5 @@ 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 diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index efa32fd..95ae3af 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -1,6 +1,6 @@ import { log } from '@repo/logger' import { createServer } from './server' -import job_route from './routes/job_route' +import job_route from '@routes/job_route' const port = process.env.PORT || 5001 const server = createServer() diff --git a/apps/api/src/routes/job_route.ts b/apps/api/src/routes/job_route.ts index cf9f494..7295271 100644 --- a/apps/api/src/routes/job_route.ts +++ b/apps/api/src/routes/job_route.ts @@ -5,7 +5,7 @@ import { get_jobs, post_job, update_job, -} from '../controllers/job_controller' +} from '@controllers/job_controller' const router = express.Router() @@ -19,7 +19,7 @@ router.get('/:id', get_job) //* @desc Post job //! @access Private -router.post('/job', post_job) +router.post('/', post_job) //* @desc Delete job //! @access Private diff --git a/apps/api/src/server.ts b/apps/api/src/server.ts index c290850..62bd43b 100644 --- a/apps/api/src/server.ts +++ b/apps/api/src/server.ts @@ -3,7 +3,7 @@ import express, { type Express } from 'express' import morgan from 'morgan' import cors from 'cors' import { config } from 'dotenv' -import { connect_db } from '../config/db' +import { connect_db } from '@config/db' config() connect_db() diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 10a4f1a..84e4f60 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -4,7 +4,16 @@ "lib": ["ES2015"], "module": "CommonJS", "outDir": "./dist", + "baseUrl": ".", + "paths": { + "@/*": ["src/*"], + "@lib/*": ["src/lib/*"], + "@controllers/*": ["src/controllers/*"], + "@models/*": ["src/models/*"], + "@config/*": ["config/*"], + "@routes/*": ["src/routes/*"], + }, }, "exclude": ["node_modules"], - "include": ["."] + "include": ["."], } From 1036a3dafdd79ddc897f4835cbced9650acd7967 Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sat, 24 Feb 2024 20:37:56 +0500 Subject: [PATCH 3/3] refactor: move all actions inside the try block --- apps/api/src/controllers/job_controller.ts | 90 +++++++++++----------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/apps/api/src/controllers/job_controller.ts b/apps/api/src/controllers/job_controller.ts index 0d18322..a67a928 100644 --- a/apps/api/src/controllers/job_controller.ts +++ b/apps/api/src/controllers/job_controller.ts @@ -6,8 +6,8 @@ import { isValidObjectId } from 'mongoose' //* route GET /api/jobs //? @access Public export async function get_jobs(_: Request, res: Response): Promise { - // fetch jobs try { + // fetch jobs const jobs = await JOB_SCHEMA.find({}).sort({ createdAt: -1 }) res.status(200).json({ jobs }) } catch (error) { @@ -20,24 +20,22 @@ export async function get_jobs(_: Request, res: Response): Promise { //* route GET /api/jobs/:id //? @access Public export async function get_job(req: Request, res: Response): Promise { - 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 - } - // fetch job 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 jobs:', error) + console.error('Error fetching job:', error) res.status(500).json({ error: 'Internal server error' }) } } @@ -46,13 +44,13 @@ export async function get_job(req: Request, res: Response): Promise { //* route POST /api/jobs/job //! @access Private export async function post_job(req: Request, res: Response): Promise { - //* post job try { + //* post job const jobData = req.body const newJob = await JOB_SCHEMA.create(jobData) 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' }) } } @@ -61,24 +59,24 @@ export async function post_job(req: Request, res: Response): Promise { //* route DELETE /api/jobs/:id //! @access Private export async function delete_job(req: Request, res: Response): Promise { - 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 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(deleted_job) + res.status(200).json({ message: 'Job deleted successfully' }) } catch (error) { - console.error('Error fetching jobs:', error) + console.error('Error deleting job:', error) res.status(500).json({ error: 'Internal server error' }) } } @@ -87,26 +85,26 @@ export async function delete_job(req: Request, res: Response): Promise { //* route PATCH /api/jobs/:id //! @access Private export async function update_job(req: Request, res: Response): Promise { - 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 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 fetching jobs:', error) + console.error('Error updating job:', error) res.status(500).json({ error: 'Internal server error' }) } }