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

@ -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);
}

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();
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