Skip to content

Commit

Permalink
fix: do not display inactive trees, and throw 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiyuSun committed Sep 15, 2023
1 parent 3f22b2f commit 9bafbf6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions server/routers/treesRouter.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 9bafbf6

Please sign in to comment.