Update Experience

This commit is contained in:
xfarrow
2025-06-12 10:07:29 +02:00
parent 602f3f7794
commit 0733415397
5 changed files with 75 additions and 3 deletions

View File

@ -5,7 +5,7 @@ meta {
} }
get { get {
url: http://localhost:3000/api/experiences/1 url: http://localhost:3000/api/experiences/5
body: none body: none
auth: inherit auth: inherit
} }

View File

@ -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."
}
}

View File

@ -3,5 +3,5 @@ auth {
} }
auth:bearer { auth:bearer {
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEsImlhdCI6MTc0OTU1MDM1OCwiZXhwIjoxNzQ5NTc5MTU4fQ.2oZk-agnonMEZw3cdf8bKjerTGpjLdxoglCiqfLVbyc token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEsImlhdCI6MTc0OTcxNTI1MCwiZXhwIjoxNzQ5NzQ0MDUwfQ.s1Fn9-Ju5XayFq89MBaQ9AGVm7CIrVvUaDWbnfiBA54
} }

View File

@ -14,6 +14,9 @@
const knex = require('../utils/knex_config'); const knex = require('../utils/knex_config');
function createExperience(title, description, organizationId, organizationName, date, person_id, type) { 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 = { const experience = {
title, title,
description, description,
@ -50,7 +53,10 @@ async function remove(experienceId, personId) {
async function update(experience) { async function update(experience) {
await knex('Experience') await knex('Experience')
.where('id', experience.id) .where({
id: experience.id,
person_id: experience.person_id
})
.update(experience); .update(experience);
} }

View File

@ -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(); const routes = express.Router();
routes.post('/', jwtUtils.extractToken, insert); routes.post('/', jwtUtils.extractToken, insert);
routes.get('/:experienceId', jwtUtils.extractToken, find); routes.get('/:experienceId', jwtUtils.extractToken, find);
routes.delete('/:experienceId', jwtUtils.extractToken, remove); routes.delete('/:experienceId', jwtUtils.extractToken, remove);
routes.patch('/:experienceId', jwtUtils.extractToken, update);
module.exports = { module.exports = {
routes routes