diff --git a/backend/apis/BlinkApiCollection/Experience/GetExperience.bru b/backend/apis/BlinkApiCollection/Experience/GetExperience.bru index 6d51e9e..fc4746d 100644 --- a/backend/apis/BlinkApiCollection/Experience/GetExperience.bru +++ b/backend/apis/BlinkApiCollection/Experience/GetExperience.bru @@ -5,7 +5,7 @@ meta { } get { - url: http://localhost:3000/api/experiences/1 + url: http://localhost:3000/api/experiences/5 body: none auth: inherit } diff --git a/backend/apis/BlinkApiCollection/Experience/UpdateExperience.bru b/backend/apis/BlinkApiCollection/Experience/UpdateExperience.bru new file mode 100644 index 0000000..e57d202 --- /dev/null +++ b/backend/apis/BlinkApiCollection/Experience/UpdateExperience.bru @@ -0,0 +1,18 @@ +meta { + name: UpdateExperience + type: http + seq: 4 +} + +patch { + url: http://localhost:3000/api/experiences/5 + body: json + auth: inherit +} + +body:json { + { + "title": "Senior Software Engineer", + "description": "I worked as a SENIOR software engineer in this company." + } +} diff --git a/backend/apis/BlinkApiCollection/collection.bru b/backend/apis/BlinkApiCollection/collection.bru index 532163e..d1bfba2 100644 --- a/backend/apis/BlinkApiCollection/collection.bru +++ b/backend/apis/BlinkApiCollection/collection.bru @@ -3,5 +3,5 @@ auth { } auth:bearer { - token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEsImlhdCI6MTc0OTU1MDM1OCwiZXhwIjoxNzQ5NTc5MTU4fQ.2oZk-agnonMEZw3cdf8bKjerTGpjLdxoglCiqfLVbyc + token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEsImlhdCI6MTc0OTcxNTI1MCwiZXhwIjoxNzQ5NzQ0MDUwfQ.s1Fn9-Ju5XayFq89MBaQ9AGVm7CIrVvUaDWbnfiBA54 } diff --git a/backend/apis/nodejs/src/models/experience_model.js b/backend/apis/nodejs/src/models/experience_model.js index a617672..35c8d48 100644 --- a/backend/apis/nodejs/src/models/experience_model.js +++ b/backend/apis/nodejs/src/models/experience_model.js @@ -14,6 +14,9 @@ const knex = require('../utils/knex_config'); function createExperience(title, description, organizationId, organizationName, date, person_id, type) { + if (organizationId !== undefined && organizationName !== undefined) { + throw new ValidationError("Either organization_id or organization_name must be populated, but not both."); // If they were both populated, what organization are they working for? + } const experience = { title, description, @@ -50,7 +53,10 @@ async function remove(experienceId, personId) { async function update(experience) { await knex('Experience') - .where('id', experience.id) + .where({ + id: experience.id, + person_id: experience.person_id + }) .update(experience); } diff --git a/backend/apis/nodejs/src/routes/experience_routes.js b/backend/apis/nodejs/src/routes/experience_routes.js index ebe5b4f..f15e57c 100644 --- a/backend/apis/nodejs/src/routes/experience_routes.js +++ b/backend/apis/nodejs/src/routes/experience_routes.js @@ -63,10 +63,58 @@ async function remove(req, res) { } } +async function update(req, res) { + try { + const updatedExperience = {}; + updatedExperience.person_id = req.jwt.person_id; + updatedExperience.id = req.params.experienceId; + + if (req.body.title !== undefined) { + updatedExperience.title = req.body.title; + } + + if (req.body.description !== undefined) { + updatedExperience.description = req.body.description; + } + + if (req.body.organizationId !== undefined ^ req.body.organizationName !== undefined) { + if (req.body.organizationId !== undefined) { + updatedExperience.organizationId = req.body.organizationId; + } else { + updatedExperience.organizationName = req.body.organizationName; + } + } + + if (req.body.date !== undefined) { + updatedExperience.date = req.body.date; + } + + if (req.body.type !== undefined) { + updatedExperience.type = req.body.type; + } + + if (Object.keys(updatedExperience).length === 2) { // 2 because at least person_id and id are set + return res.status(400).json({ + error: 'Bad request. No data to update' + }); + } + + Experience.update(updatedExperience); + return res.status(204).send(); + + } catch (error) { + console.error(`Error in function ${update.name}: ${error}`); + res.status(500).json({ + error: 'Internal server error' + }); + } +} + const routes = express.Router(); routes.post('/', jwtUtils.extractToken, insert); routes.get('/:experienceId', jwtUtils.extractToken, find); routes.delete('/:experienceId', jwtUtils.extractToken, remove); +routes.patch('/:experienceId', jwtUtils.extractToken, update); module.exports = { routes