Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue # 17] Job Posting CRUD API for Backend Service #19

Merged
merged 16 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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);
}
}
Loading
Loading