diff --git a/backend/src/controllers/jobControllers.js b/backend/src/controllers/jobControllers.js index 352a442..d6cf582 100644 --- a/backend/src/controllers/jobControllers.js +++ b/backend/src/controllers/jobControllers.js @@ -115,9 +115,25 @@ const browseLatest = async (req, res, next) => { next(err); } }; + // The E of BREAD - Edit (Update) operation -// This operation is not yet implemented +const edit = async (req, res, next) => { + // Extract the job data from the request body + const job = req.body; + try { + const result = await tables.job.update(req.params.id, job); + // If the job is not found, respond with HTTP 404 (Not Found) + if (result.affectedRows === 1) { + res.sendStatus(204); + } else { + res.sendStatus(404); + } + } catch (err) { + // Pass any errors to the error-handling middleware + next(err); + } +}; // The A of BREAD - Add (Create) operation const add = async (req, res, next) => { // Extract the job data from the request body @@ -157,7 +173,7 @@ module.exports = { readByCompany, readByCompanyJob, browseLatest, - // edit, + edit, add, destroy, }; diff --git a/backend/src/models/JobManager.js b/backend/src/models/JobManager.js index cddd1bc..cd6d3ff 100644 --- a/backend/src/models/JobManager.js +++ b/backend/src/models/JobManager.js @@ -172,9 +172,18 @@ class JobManager extends AbstractManager { // The U of CRUD - Update operation // TODO: Implement the update operation to modify an existing job - // async update(job) { - // ... - // } + async update(id, job) { + // Execute the SQL SELECT query to retrieve a specific job by its ID + // eslint-disable-next-line no-param-reassign + delete job.created_at; + const [result] = await this.database.query( + `UPDATE ${this.table} set ? WHERE id = ?`, + [job, id] + ); + + // Return the first row of the result, which represents the item + return result; + } // The D of CRUD - Delete operation // TODO: Implement the delete operation to remove an job by its ID diff --git a/backend/src/router.js b/backend/src/router.js index 766720e..f479526 100644 --- a/backend/src/router.js +++ b/backend/src/router.js @@ -23,6 +23,7 @@ const validateUser = require("./validators/validateUser"); const validateAccount = require("./validators/validateAccount"); const validateCompany = require("./validators/validateCompany"); const validateCV = require("./validators/validateCV"); +const validateJob = require("./validators/validateJob"); // ROUTES GET router.get("/jobs", jobControllers.browse); @@ -32,7 +33,7 @@ router.get("/companies", companyControllers.browse); router.get( "/consultants", checkCredentials, - checkAdmin, + checkConsultant, userControllers.getConsultant ); router.get("/roles", checkCredentials, checkAdmin, roleControllers.browse); @@ -85,8 +86,13 @@ router.get( router.get("/roles/:id", checkCredentials, checkAdmin, roleControllers.read); router.get("/users/:id", checkCredentials, userControllers.read); -// ROUTES POST -router.post("/jobs", checkCredentials, checkConsultant, jobControllers.add); +router.post( + "/jobs", + validateJob, + checkCredentials, + checkConsultant, + jobControllers.add +); router.post("/login", validateUser, userControllers.login); router.post("/register", validateUser, userControllers.add); router.post( @@ -145,15 +151,7 @@ router.put( validateCV, userControllers.updateProfileCV ); -router.put( - "/application/:id", - checkCredentials, - checkConsultant, - applicationControllers.edit -); - -router.post("/logout", userControllers.logout); - +router.put("/jobs/:id", checkCredentials, checkConsultant, jobControllers.edit); /* ************************************************************************* */ module.exports = router; diff --git a/backend/src/validators/validateJob.js b/backend/src/validators/validateJob.js new file mode 100644 index 0000000..f8ab56c --- /dev/null +++ b/backend/src/validators/validateJob.js @@ -0,0 +1,35 @@ +const Joi = require("joi"); + +const schema = Joi.object({ + id: Joi.number(), + company_id: Joi.number(), + consultant_id: Joi.number(), + title: Joi.string().required(), + description_mission: Joi.string().required(), + description_about_candidate: Joi.string().required(), + description_position: Joi.string().required(), + description_advantages: Joi.string().required(), + description_process: Joi.string().required(), + language: Joi.string().required(), + salary: Joi.string().required(), + location: Joi.string().required(), + working_type: Joi.string().required(), + starting_date: Joi.date().required(), + position_category: Joi.string().required(), + contract_type: Joi.string().required(), + position_requirements: Joi.string().required(), + created_at: Joi.string(), +}); + +const validateJob = (req, res, next) => { + delete req.body.id; + const { error } = schema.validate(req.body); + + if (error) { + res.status(422).json(error); + } else { + next(); + } +}; + +module.exports = validateJob; diff --git a/frontend/src/components/JobCard.jsx b/frontend/src/components/JobCard.jsx index df5a216..6911078 100644 --- a/frontend/src/components/JobCard.jsx +++ b/frontend/src/components/JobCard.jsx @@ -43,6 +43,15 @@ function JobCard({ job, cardStyle, refresh, isUserPage }) { >

{job.title}

+ {!isUserPage ? ( + + + + ) : ( + "" + )} {!isUserPage ? (