From 338e314613395637a5c59ba54b8b17f41602d890 Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:35:41 +0100 Subject: [PATCH] add delete method in contactinfo --- .../src/models/person_contact_info_model.js | 8 ++++++++ .../src/routes/person_contact_info_routes.js | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/backend/apis/nodejs/src/models/person_contact_info_model.js b/backend/apis/nodejs/src/models/person_contact_info_model.js index 0fcda98..219fe25 100644 --- a/backend/apis/nodejs/src/models/person_contact_info_model.js +++ b/backend/apis/nodejs/src/models/person_contact_info_model.js @@ -29,7 +29,15 @@ async function getInfoByPerson(personId) { .where('person_id', personId); } +async function remove(contactInfoId, personId) { + return (await knex('PersonContactInfo') + .where('id', contactInfoId) + .where('person_id', personId) + .del() === 1) +} + module.exports = { insert, + remove, getInfoByPerson } \ No newline at end of file diff --git a/backend/apis/nodejs/src/routes/person_contact_info_routes.js b/backend/apis/nodejs/src/routes/person_contact_info_routes.js index 99ba575..3036da7 100644 --- a/backend/apis/nodejs/src/routes/person_contact_info_routes.js +++ b/backend/apis/nodejs/src/routes/person_contact_info_routes.js @@ -40,13 +40,26 @@ async function findByPerson(req, res) { } } -async function remove(req, res){ - +async function remove(req, res) { + try { + const success = await PersonContactInfo.remove(req.params.contactInfoId, req.jwt.person_id); + if (success) { + return res.status(200).send(); + } else { + return res.status(400).send(); + } + } catch (error) { + console.error(`Error in function ${insert.name}: ${error}`); + res.status(500).json({ + error: 'Internal server error' + }); + } } const routes = express.Router(); routes.post('/myself/contactinfos', jwtUtils.extractToken, insert); routes.get('/:personId/contactinfos', jwtUtils.extractToken, findByPerson) +routes.delete('/contactinfos/:contactInfoId', jwtUtils.extractToken, remove) module.exports = { routes }; \ No newline at end of file