From 224d0f7722a82962785a2c917e9ab14a5f9d2e09 Mon Sep 17 00:00:00 2001 From: mustafahincal Date: Sat, 31 Aug 2024 16:41:34 +0300 Subject: [PATCH] feat: get rp by id --- v1/src/controllers/rps.controller.js | 12 ++++++++++++ v1/src/routes/index.js | 4 ++-- v1/src/routes/rps.routes.js | 2 ++ v1/src/services/rp.service.js | 11 +++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/v1/src/controllers/rps.controller.js b/v1/src/controllers/rps.controller.js index 03eebd5..332e271 100644 --- a/v1/src/controllers/rps.controller.js +++ b/v1/src/controllers/rps.controller.js @@ -11,6 +11,7 @@ const { exportBySiteBoundToExcel: exportBySiteBound, getExcelTemplate: getRPsExcelTemplate, importFromXlsx: importRPsFromXlsx, + getRpById, } = require('../services/rp.service'); const { calculateDistributionCurves, @@ -126,6 +127,16 @@ const getBySiteBoundId = async (req, res) => { }); }; +const getById = async (req, res) => { + const { rpId } = req.params; + const rp = await getRpById(rpId); + res.send({ + rp, + success: true, + message: 'Rp listed successfully', + }); +}; + const distributionCurves = async (req, res) => { const { rpIdList, sourceList, chartList } = req.body; const result = []; @@ -186,4 +197,5 @@ module.exports = { getExcelTemplate, importFromXlsx, copyPaste, + getById, }; diff --git a/v1/src/routes/index.js b/v1/src/routes/index.js index c7f9b4a..c799bf5 100644 --- a/v1/src/routes/index.js +++ b/v1/src/routes/index.js @@ -6,9 +6,9 @@ const authenticateToken = require('../middlewares/authenticate'); router.use('/auth', require('./auth.routes')); router.use('/users', require('./users.routes')); router.use('/siteBounds', authenticateToken, require('./siteBounds.routes')); -router.use('/fields', authenticateToken, require('./sites.routes')); +router.use('/fields', require('./sites.routes')); router.use('/projects', authenticateToken, require('./projects.routes')); -router.use('/rps', authenticateToken, require('./rps.routes')); +router.use('/rps', require('./rps.routes')); router.use('/gprs', authenticateToken, require('./gprs.routes')); router.use( '/magnetometrics', diff --git a/v1/src/routes/rps.routes.js b/v1/src/routes/rps.routes.js index 5db228d..26b69c4 100644 --- a/v1/src/routes/rps.routes.js +++ b/v1/src/routes/rps.routes.js @@ -10,6 +10,7 @@ const { exportBySiteBoundToExcel, getExcelTemplate, importFromXlsx, + getById, copyPaste, } = require('../controllers/rps.controller'); const errorCatcher = require('../scripts/utils/errorCatcher'); @@ -20,6 +21,7 @@ const router = express.Router(); router.route('/').get(errorCatcher(index)); router.route('/:rpId').put(errorCatcher(update)); +router.route('/:rpId').get(errorCatcher(getById)); router.route(`/copy-paste/:siteBoundId`).post(errorCatcher(copyPaste)); router.route('/:siteBoundId').get(errorCatcher(getBySiteBoundId)); router.route('/').post(errorCatcher(create)); diff --git a/v1/src/services/rp.service.js b/v1/src/services/rp.service.js index 9fb9a17..75bc11b 100644 --- a/v1/src/services/rp.service.js +++ b/v1/src/services/rp.service.js @@ -58,6 +58,16 @@ const getRpsBySiteBoundId = async (siteBoundId) => { return rps; }; +const getRpById = async (id) => { + const rp = await Rp.findById(id); + + if (!rp) { + throw new Error('rp not found'); + } + + return rp; +}; + const getLastRpBySiteBoundId = async (siteBoundId) => { const rp = await Rp.findOne({ siteBound: siteBoundId, @@ -178,4 +188,5 @@ module.exports = { exportBySiteBoundToExcel, getExcelTemplate, importFromXlsx, + getRpById, };