Skip to content

Commit

Permalink
[Issue # 17] Job Posting CRUD API for Backend Service (#19)
Browse files Browse the repository at this point in the history
* Added mongoDB connection configuration

* Added a post request for job posting

* Added get request to search job postings by email in route.js

* Added delete request that handles deleting a job posting and removed unnecessary comment and console.log codes

* Added Patch request which updates any desired fields in the job posting colletion in route.js

* Removed unused import and removed unnecessary space

* Added get requests for site 1, 2, 3, and 4

* Added pagination feature to the get request in site1,2,3, and 4

* Created siteRequestUtils.js and moved the repeated codes in sites' route codes to it. Also, updated the site names specifically.

* Updated the database connection to handle it in singleton way

* Removed database connection closing code to fix the connection error and added missing import NextResponse in siteRequestUtils

* Removed unnecessary error handling and fixed typo

* Added get request by Id for job posting

* Apply formatting changes

---------

Co-authored-by: ama-cantabile <ama-cantabile@users.noreply.github.com>
  • Loading branch information
ama-cantabile and ama-cantabile committed May 10, 2024
1 parent 41648dd commit e40065d
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/app/api/job-posting/by-id/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NextResponse } from 'next/server';
import { connectMongoDB } from '@/libs/mongodb';
import posting from '@/app/api/posting';
import mongoose from 'mongoose';

export async function GET(req) {
try {
const jobPostingId = req.nextUrl.searchParams.get('job-posting-id');

if (!jobPostingId) {
return NextResponse.json(
{ message: 'Bad Request - ID parameter is required' },
{ status: 400 }
);
}

await connectMongoDB();

const Posting =
mongoose.models.posting || mongoose.model('posting', posting);

const jobPostings = await Posting.find({ _id: jobPostingId });

// Check if job postings were found
if (jobPostings.length === 0) {
return NextResponse.json(
{
message:
'Not Found - No job postings were found associated with the provided ID',
},
{ status: 404 }
);
}

return NextResponse.json({ jobPostings }, { status: 200 });
} catch (error) {
console.log('Error fetching job postings by ID:', error);

// Check error status and return appropriate response
if (error instanceof SyntaxError || error instanceof TypeError) {
// Malformed or invalid request
return NextResponse.json(
{ message: 'Bad Request - The request is malformed or invalid' },
{ status: 400 }
);
} else if (error.name === 'UnauthorizedError') {
// Unauthorized
return NextResponse.json(
{
message:
'Unauthorized - The client is not authorized to perform the operation',
},
{ status: 401 }
);
} else if (error.name === 'NotFoundError' || error.name === 'CastError') {
// Not Found
return NextResponse.json(
{ message: 'Not Found - The specified job ID does not exist' },
{ status: 404 }
);
} else {
// Other server error
return NextResponse.json(
{ message: 'Internal Server Error:' },
{ status: 500 }
);
}
}
}
32 changes: 32 additions & 0 deletions src/app/api/job-posting/disabled/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from 'next/server';
import {
getPaginationParams,
fetchJobPostings,
handleError,
} from '../siteRequestUtils';

export async function GET(req) {
try {
//Todo: Update the site3 name to disabled
const siteCriteria = { site3: true };

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
}

// Return success response with the paginated job postings
return NextResponse.json({ jobPostings }, { status: 200 });
} catch (error) {
// Handle errors
return handleError(error);
}
}
32 changes: 32 additions & 0 deletions src/app/api/job-posting/indigenous/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from 'next/server';
import {
getPaginationParams,
fetchJobPostings,
handleError,
} from '../siteRequestUtils';

export async function GET(req) {
try {
//Todo: Update the site1 name to indigenous
const siteCriteria = { site1: true };

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
}

// Return success response with the paginated job postings
return NextResponse.json({ jobPostings }, { status: 200 });
} catch (error) {
// Handle errors
return handleError(error);
}
}
32 changes: 32 additions & 0 deletions src/app/api/job-posting/newcomers/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from 'next/server';
import {
getPaginationParams,
fetchJobPostings,
handleError,
} from '../siteRequestUtils';

export async function GET(req) {
try {
//Todo: Update the site2 name to newcomers
const siteCriteria = { site2: true };

// Extract pagination parameters
const { ski