From 7378c85d503bd81a58c2a339ca98896ed3f13e80 Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:16:06 +0100 Subject: [PATCH] update --- ...t_info.js => person_contact_info_model.js} | 11 +++++++-- .../src/routes/person_contact_info_routes.js | 23 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) rename backend/apis/nodejs/src/models/{person_contact_info.js => person_contact_info_model.js} (81%) diff --git a/backend/apis/nodejs/src/models/person_contact_info.js b/backend/apis/nodejs/src/models/person_contact_info_model.js similarity index 81% rename from backend/apis/nodejs/src/models/person_contact_info.js rename to backend/apis/nodejs/src/models/person_contact_info_model.js index 8894ed1..0fcda98 100644 --- a/backend/apis/nodejs/src/models/person_contact_info.js +++ b/backend/apis/nodejs/src/models/person_contact_info_model.js @@ -14,15 +14,22 @@ const knex = require('../utils/knex_config'); async function insert(personId, infoContent, infoType) { - return await knex('PersonContactInfo') + const contactInfo = await knex('PersonContactInfo') .insert({ person_id: personId, info: infoContent, info_type: infoType }) .returning("*"); + return contactInfo[0]; +} + +async function getInfoByPerson(personId) { + return await knex('PersonContactInfo') + .where('person_id', personId); } module.exports = { - insert + insert, + 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 c09f249..99ba575 100644 --- a/backend/apis/nodejs/src/routes/person_contact_info_routes.js +++ b/backend/apis/nodejs/src/routes/person_contact_info_routes.js @@ -13,13 +13,13 @@ const express = require('express'); const jwtUtils = require('../utils/jwt_utils'); -const PersonContactInfo = require('../models/person_contact_info'); +const PersonContactInfo = require('../models/person_contact_info_model'); async function insert(req, res) { try { - const contactInfo = PersonContactInfo.insert(req.jwt.person_id, req.body.content, req.body.info_type); + const contactInfo = await PersonContactInfo.insert(req.jwt.person_id, req.body.content, req.body.info_type); res.set('Location', `/api/persons/${req.jwt.person_id}/contactinfos/${contactInfo.id}`); - return res.status(201).json(application); + return res.status(201).json(contactInfo); } catch (error) { console.error(`Error in function ${insert.name}: ${error}`); res.status(500).json({ @@ -28,8 +28,25 @@ async function insert(req, res) { } } +async function findByPerson(req, res) { + try { + const infos = await PersonContactInfo.getInfoByPerson(req.params.personId); + return res.status(200).json(infos); + } catch (error) { + console.error(`Error in function ${insert.name}: ${error}`); + res.status(500).json({ + error: 'Internal server error' + }); + } +} + +async function remove(req, res){ + +} + const routes = express.Router(); routes.post('/myself/contactinfos', jwtUtils.extractToken, insert); +routes.get('/:personId/contactinfos', jwtUtils.extractToken, findByPerson) module.exports = { routes }; \ No newline at end of file