-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kashish Mittal <[email protected]>
- Loading branch information
Showing
2 changed files
with
69 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 66 additions & 66 deletions
132
server/src/controllers/job_posts/getFilteredSortedJobPosts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |