Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
Signed-off-by: Kashish Mittal <[email protected]>
  • Loading branch information
04kash committed Jan 24, 2025
1 parent 424c1ee commit 5478656
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const logger = require("pino")();
const JobPosting = require("../../models/job_posts.model");
const { Op } = require("sequelize");

const getFilteredSortedByStatusJobPostsRequestHandler = async (req, res) => {
// check method is GET
Expand Down Expand Up @@ -40,8 +39,8 @@ const getFilteredSortedByStatusJobPostsRequestHandler = async (req, res) => {
// get pagination parameters:
const page = req?.query?.page ? parseInt(req.query.page, 10) : null;
const pageSize = req?.query?.pageSize
? parseInt(req.query.pageSize, 10)
: null;
? parseInt(req.query.pageSize, 10)
: null;

if (page != null && pageSize != null) {
searchConfig.limit = pageSize;
Expand Down Expand Up @@ -72,4 +71,4 @@ const getFilteredSortedByStatusJobPostsRequestHandler = async (req, res) => {
}
};

module.exports = getFilteredSortedByStatusJobPostsRequestHandler;
module.exports = getFilteredSortedByStatusJobPostsRequestHandler;
132 changes: 66 additions & 66 deletions server/src/controllers/job_posts/getFilteredSortedJobPosts.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
const logger = require("pino")();
const JobPosting = require("../../models/job_posts.model");
const { Op } = require("sequelize");
const JobPosting = require("../../models/job_posts.model");

const getFilteredSortedJobPostsRequestHandler = async (req, res) => {
// check method is GET
if (req.method !== "GET") {
return res
.status(405)
.json({ message: "Method not allowed, only GET methods allowed." });
}

try {
const { location, job_type, order = "descending" } = req.query;

const query = {};
// check method is GET
if (req.method !== "GET") {
return res
.status(405)
.json({ message: "Method not allowed, only GET methods allowed." });
}

if (location) {
query.location = location;
}

if (job_type) {
query.job_type = {
[Op.contains]: [job_type],
};
}
try {
const { location, job_type, order = "descending" } = req.query;

const sortOrder = order === "ascending" ? "ASC" : "DESC";
const query = {};

query.state = "Active";
if (location) {
query.location = location;
}

const searchConfig = {
order: [["close_date", sortOrder]],
where: query,
attributes: [
"id",
"title",
"employer",
"location",
"job_type",
"close_date",
"state",
],
if (job_type) {
query.job_type = {
[Op.contains]: [job_type],
};
}

// get pagination parameters:
const page = req?.query?.page ? parseInt(req.query.page, 10) : null;
const pageSize = req?.query?.pageSize
? parseInt(req.query.pageSize, 10)
: null;
const sortOrder = order === "ascending" ? "ASC" : "DESC";

if (page != null && pageSize != null) {
searchConfig.limit = pageSize;
searchConfig.offset = page * pageSize;
}
query.state = "Active";

const allJobPosts = await JobPosting.findAndCountAll(searchConfig);
const searchConfig = {
order: [["close_date", sortOrder]],
where: query,
attributes: [
"id",
"title",
"employer",
"location",
"job_type",
"close_date",
"state",
],
};

// -------- Response:
const response = {
status: "success",
message: "All job posts found successfully",
allJobPosts: {
totalPosts: allJobPosts.count,
totalPages: pageSize ? Math.ceil(allJobPosts.count / pageSize) : 1,
currentPage: page,
data: allJobPosts.rows,
},
};
// get pagination parameters:
const page = req?.query?.page ? parseInt(req.query.page, 10) : null;
const pageSize = req?.query?.pageSize
? parseInt(req.query.pageSize, 10)
: null;

if (page != null && pageSize != null) {
searchConfig.limit = pageSize;
searchConfig.offset = page * pageSize;
}

return res.status(200).json(response);
} catch (err) {
logger.error(`Unexpected server error: ${err}`);
return res.status(500).json({
status: "error",
message: "An unexpected server error occurred.",
});
}
const allJobPosts = await JobPosting.findAndCountAll(searchConfig);

// -------- Response:
const response = {
status: "success",
message: "All job posts found successfully",
allJobPosts: {
totalPosts: allJobPosts.count,
totalPages: pageSize ? Math.ceil(allJobPosts.count / pageSize) : 1,
currentPage: page,
data: allJobPosts.rows,
},
};

module.exports = getFilteredSortedJobPostsRequestHandler;

return res.status(200).json(response);
} catch (err) {
logger.error(`Unexpected server error: ${err}`);
return res.status(500).json({
status: "error",
message: "An unexpected server error occurred.",
});
}
};

module.exports = getFilteredSortedJobPostsRequestHandler;

0 comments on commit 5478656

Please sign in to comment.