Skip to content

Commit

Permalink
Merge pull request #39 from Vero-Ventures/35-adds-sorting-back-end-ap…
Browse files Browse the repository at this point in the history
…i-by-job-posted-date-for-the-job-postings

Implemented sorting back-end APIs for the front-end job posting and admin panel pages
  • Loading branch information
Soohyeun authored May 15, 2024
2 parents 0c1ee31 + 9e54447 commit 22d1bc8
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 27 deletions.
21 changes: 19 additions & 2 deletions src/app/api/job-posting/asylum-refugees/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPaginationParams,
fetchJobPostings,
handleError,
checkFieldExist,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -12,9 +13,25 @@ export async function GET(req) {

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);
// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
Expand Down
21 changes: 19 additions & 2 deletions src/app/api/job-posting/disabled/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPaginationParams,
fetchJobPostings,
handleError,
checkFieldExist,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -12,9 +13,25 @@ export async function GET(req) {

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);
// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
Expand Down
21 changes: 19 additions & 2 deletions src/app/api/job-posting/indigenous/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPaginationParams,
fetchJobPostings,
handleError,
checkFieldExist,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -12,9 +13,25 @@ export async function GET(req) {

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);
// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
Expand Down
21 changes: 19 additions & 2 deletions src/app/api/job-posting/newcomers/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPaginationParams,
fetchJobPostings,
handleError,
checkFieldExist,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -12,9 +13,25 @@ export async function GET(req) {

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);
// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
Expand Down
22 changes: 20 additions & 2 deletions src/app/api/job-posting/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { NextResponse } from 'next/server';
import { connectMongoDB } from '@/libs/mongodb';
import posting from '@/app/api/posting';
import mongoose from 'mongoose';
import { checkFieldExist } from '@/app/api/job-posting/siteRequestUtils';

export async function GET(req) {
try {
const email = req.nextUrl.searchParams.get('email');
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

if (!email) {
return NextResponse.json(
Expand All @@ -14,12 +17,21 @@ export async function GET(req) {
);
}

// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

await connectMongoDB();

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

const jobPostings = await Posting.find({ email });
const jobPostings = await Posting.find({ email }).sort(sortCriteria);

// Check if job postings were found
if (jobPostings.length === 0) {
Expand Down Expand Up @@ -86,8 +98,14 @@ export async function POST(req) {
createdJobPostings.push(newJobPosting);
}

// Extract the IDs of the created job postings
const createdJobPostingIds = createdJobPostings.map(posting => posting._id);

return NextResponse.json(
{ message: 'Job postings created successfully' },
{
message: 'Job postings created successfully',
createdJobPostingIds: createdJobPostingIds,
},
{ status: 200 }
);
} catch (error) {
Expand Down
45 changes: 30 additions & 15 deletions src/app/api/job-posting/siteRequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@ export function getPaginationParams(req) {
return { skip, pageSize };
}

// Function to fetch job postings with pagination
export async function fetchJobPostings(siteCriteria, skip, pageSize) {
// Connect to MongoDB
await connectMongoDB();

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

// Query job postings with pagination
const jobPostings = await Posting.find(siteCriteria)
.skip(skip)
.limit(pageSize);

return jobPostings;
}

// Function to handle errors
export function handleError(error) {
console.error('Error fetching job postings:', error);
Expand All @@ -55,3 +40,33 @@ export function handleError(error) {
);
}
}

// Function to fetch and sort job postings by date
export async function fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
) {
await connectMongoDB();

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

// Query job postings with pagination
const jobPostings = await Posting.find(siteCriteria)
.sort(sortCriteria)
.skip(skip)
.limit(pageSize);

return jobPostings;
}

// Function to check if the requested field exists in the schema
export async function checkFieldExist(requestedObject) {
await connectMongoDB();

const Posting = mongoose.models.posting || mongoose.model('posting', posting);
const field = Object.keys(requestedObject)[0];

return Posting.schema.path(field) !== undefined;
}
21 changes: 19 additions & 2 deletions src/app/api/job-posting/students/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPaginationParams,
fetchJobPostings,
handleError,
checkFieldExist,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -12,9 +13,25 @@ export async function GET(req) {

// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

// Query job postings with pagination
const jobPostings = await fetchJobPostings(siteCriteria, skip, pageSize);
// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
console.log('no field');
return NextResponse.json(
{ message: 'Not Found - Requested sort field does not exist' },
{ status: 404 }
);
}

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
Expand Down

0 comments on commit 22d1bc8

Please sign in to comment.