From 10cf6548a417a063d0c05e468f7654d95415df27 Mon Sep 17 00:00:00 2001 From: Nikolay Martyanov Date: Wed, 27 Dec 2023 02:29:30 +0100 Subject: [PATCH] back: Add getSiblings method in Region Controller. This commit introduces a new method getSiblings in regionController.js. This method seeks to return sibling regions for a given region ID and hierarchy ID. It also modifies the Hierarchy model to include a getSiblings method and updates the regionRoutes.js to add a new route for this functionality. Issue: #159 Signed-off-by: Nikolay Martyanov --- backend/src/controllers/regionController.js | 26 +++++++++++++++++++++ backend/src/models/Hierarchy.js | 9 +++++++ backend/src/routes/regionRoutes.js | 16 +++++++++++++ 3 files changed, 51 insertions(+) diff --git a/backend/src/controllers/regionController.js b/backend/src/controllers/regionController.js index c7148ac..009a1bd 100644 --- a/backend/src/controllers/regionController.js +++ b/backend/src/controllers/regionController.js @@ -400,3 +400,29 @@ exports.getSubregions = async (req, res) => { ) : { message: subregions.message }; return res.status(subregions.status).json(result); }; + +exports.getSiblings = async (req, res) => { + const { regionId } = req.params; + const hierarchyId = req.query.hierarchyId || 1; + + console.log('regionId: ', regionId); + + // Check if the region exists + const region = await Hierarchy.findOne({ + where: { + regionId, + hierarchyId, + }, + }); + if (!region) { + return res.status(404).json({ message: 'Region not found' }); + } + + try { + const siblings = await region.getSiblings(); + return res.status(200).json(siblings.map((sibling) => sibling.toApiFormat())); + } catch (err) { + console.error(err); + return res.status(500).json({ message: 'Internal Server Error' }); + } +}; diff --git a/backend/src/models/Hierarchy.js b/backend/src/models/Hierarchy.js index 0ce0dd4..1a0eaef 100644 --- a/backend/src/models/Hierarchy.js +++ b/backend/src/models/Hierarchy.js @@ -11,6 +11,15 @@ class Hierarchy extends Model { }; } + async getSiblings() { + return Hierarchy.findAll({ + where: { + parentId: this.parentId, + hierarchyId: this.hierarchyId, + }, + }); + } + static async getAncestors(regionId, hierarchyId) { const ancestors = await sequelize.query(` WITH RECURSIVE Ancestors AS ( diff --git a/backend/src/routes/regionRoutes.js b/backend/src/routes/regionRoutes.js index 845aa30..a90055d 100644 --- a/backend/src/routes/regionRoutes.js +++ b/backend/src/routes/regionRoutes.js @@ -104,4 +104,20 @@ router.get( }, ); +router.get( + '/:regionId/siblings', + ...[ + check('regionId').isInt().withMessage('Region ID must be an integer'), + check('hierarchyId').optional().isInt().withMessage('Hierarchy ID must be an integer'), + ], + async (req, res) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + const errorMessages = errors.array().map((error) => error.msg); + return res.status(400).json({ errors: errorMessages }); + } + return regionController.getSiblings(req, res); + }, +); + module.exports = router;