From 9bafbf6f3b8691f56fd0a374912444394cfb0d2a Mon Sep 17 00:00:00 2001 From: weiyu Date: Fri, 15 Sep 2023 14:32:15 -0500 Subject: [PATCH] fix: do not display inactive trees, and throw 404 error --- server/routers/treesRouter.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server/routers/treesRouter.ts b/server/routers/treesRouter.ts index 9de1939..efb09be 100644 --- a/server/routers/treesRouter.ts +++ b/server/routers/treesRouter.ts @@ -1,11 +1,11 @@ import express from 'express'; import Joi from 'joi'; import FilterOptions from 'interfaces/FilterOptions'; -import Tree from 'interfaces/Tree'; import { handlerWrapper } from './utils'; import Session from '../infra/database/Session'; import TreeRepository from '../infra/database/TreeRepository'; import TreeModel from '../models/Tree'; +import HttpError from '../utils/HttpError'; const router = express.Router(); type Filter = Partial<{ @@ -34,22 +34,26 @@ router.get( '/:val', handlerWrapper(async (req, res) => { const repo = new TreeRepository(new Session()); - let result - if(isNaN(Number(req.params.val))){ + let result; + if (isNaN(Number(req.params.val))) { Joi.assert(req.params.val, Joi.string().guid().required()); const exe = TreeModel.getByUUID(repo); - result = await exe(req.params.val); - } else{ + result = await exe(req.params.val); + } else { Joi.assert(req.params.val, Joi.number().required()); const exe = TreeModel.getById(repo); result = await exe(req.params.val); } + + if (result.active === false) { + throw new HttpError(404, `Can not find trees by id:${result.id}`); + } + res.send(result); res.end(); }), ); - router.get( '/', handlerWrapper(async (req, res) => {