From 86bb9717d8c134647d18947b81f9601bfd382261 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Mon, 18 Mar 2024 17:54:57 +0100 Subject: [PATCH] update --- backend/apis/nodejs/src/app.js | 13 ++++++----- .../apis/nodejs/src/models/job_offer_model.js | 11 +++++++++- .../nodejs/src/routes/job_offer_routes.js | 22 +++++++++++++++++++ frontend/vanilla/html/organization.html | 14 ++++++++++-- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/backend/apis/nodejs/src/app.js b/backend/apis/nodejs/src/app.js index 2aaaab8..7c103cc 100644 --- a/backend/apis/nodejs/src/app.js +++ b/backend/apis/nodejs/src/app.js @@ -54,13 +54,14 @@ app.use(rateLimit({ ===== BEGIN ROUTE HANDLING ===== */ -app.use('/api', personRoutes.publicRoutes); -app.use('/api', organizationRoutes.publicRoutes); -app.use('/api', personRoutes.protectedRoutes); -app.use('/api', organizationRoutes.protectedRoutes); -app.use('/api', organizationAdminRoutes.protectedRoutes); -app.use('/api', organizationPostRoutes.protectedRoutes); +// app.use('/api', personRoutes.publicRoutes); +// app.use('/api', personRoutes.protectedRoutes); +app.use('/api', jobOffersRoutes.publicRoutes); app.use('/api', jobOffersRoutes.protectedRoutes); +app.use('/api', organizationRoutes.publicRoutes); +app.use('/api', organizationRoutes.protectedRoutes); +// app.use('/api', organizationPostRoutes.protectedRoutes); +// app.use('/api', organizationAdminRoutes.protectedRoutes); /* ===== END ROUTE HANDLING ===== diff --git a/backend/apis/nodejs/src/models/job_offer_model.js b/backend/apis/nodejs/src/models/job_offer_model.js index e272e7d..c879e1b 100644 --- a/backend/apis/nodejs/src/models/job_offer_model.js +++ b/backend/apis/nodejs/src/models/job_offer_model.js @@ -72,6 +72,14 @@ async function findById(jobOfferId) { }).select().first(); } +async function findByOrganizationId(organizationId){ + const result = await knex('JobOffer') + .where({organization_id: organizationId}) + .select(); + console.log(result); + return result; +} + // test async function filter(title, description, requirements, salary, salaryOperator, salaryFrequency, location, tags) { let query = knex('JobOffer'); @@ -105,5 +113,6 @@ async function filter(title, description, requirements, salary, salaryOperator, module.exports = { insert, - remove + remove, + findByOrganizationId } \ No newline at end of file diff --git a/backend/apis/nodejs/src/routes/job_offer_routes.js b/backend/apis/nodejs/src/routes/job_offer_routes.js index 7444e73..e56c7f3 100644 --- a/backend/apis/nodejs/src/routes/job_offer_routes.js +++ b/backend/apis/nodejs/src/routes/job_offer_routes.js @@ -80,11 +80,33 @@ async function remove(req, res) { } } +/** + * GET Request + * @param {*} req + * @param {*} res + * @returns + */ +async function findByOrganizationId(req, res) { + try { + const result = await JobOffer.findByOrganizationId(req.params.id); + return res.status(200).send(result); + } catch (error) { + console.error(`Error in function ${insert.name}: ${error}`); + res.status(500).json({ + error: 'Internal server error' + }); + } +} + +const publicRoutes = express.Router(); +publicRoutes.get('/organizations/:id/joboffers', findByOrganizationId); + const protectedRoutes = express.Router(); protectedRoutes.use(jwtUtils.verifyToken); protectedRoutes.post('/organizations/:id/joboffers', insert); protectedRoutes.delete('/organizations/joboffers/:jobOfferId', remove); module.exports = { + publicRoutes, protectedRoutes } \ No newline at end of file diff --git a/frontend/vanilla/html/organization.html b/frontend/vanilla/html/organization.html index 7d7f2f7..d77320c 100644 --- a/frontend/vanilla/html/organization.html +++ b/frontend/vanilla/html/organization.html @@ -38,14 +38,14 @@ alert("Invalid URL."); return; } - const response = await fetch(`${API_URL}/organization/${idToDisplay}`, { + const response = await fetch(`${API_URL}/organizations/${idToDisplay}`, { headers: { "Content-type": "application/json; charset=UTF-8", } }); const data = await response.json(); if (response.ok) { - populateFields(data.name, data.location, data.description, data.is_hiring); + populateFields(data.name, data.location, data.description, isOrganizationHiring(idToDisplay)); document.body.style.display = "block"; // Show page } else { alert(data.error); @@ -67,6 +67,16 @@ } } + + async function isOrganizationHiring(organizationId) { + const response = await fetch(`${API_URL}/organizations/${organizationId}/joboffers`, { + headers: { + "Content-type": "application/json; charset=UTF-8", + } + }); + const data = await response.json(); + return data.length > 0; + }