mirror of
https://github.com/whyour/qinglong.git
synced 2025-09-11 21:33:06 +08:00
Add validation to dependencies GET endpoint and update service logic
Introduces Joi validation for the GET /dependencies route using celebrate, ensuring request bodies conform to expected structure. Updates the DependenceService.dependencies method to handle type filtering more robustly and refines the search condition logic.
This commit is contained in:
parent
213ee53347
commit
3c1dc68ac9
|
@ -8,17 +8,29 @@ const route = Router();
|
|||
export default (app: Router) => {
|
||||
app.use('/dependencies', route);
|
||||
|
||||
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const dependenceService = Container.get(DependenceService);
|
||||
const data = await dependenceService.dependencies(req.query as any);
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
return next(e);
|
||||
}
|
||||
});
|
||||
route.get(
|
||||
'/',
|
||||
celebrate({
|
||||
body: Joi.array().items(
|
||||
Joi.object({
|
||||
searchValue: Joi.string().optional().allow(''),
|
||||
type: Joi.string().optional().allow(''),
|
||||
status: Joi.string().optional().allow(''),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
const dependenceService = Container.get(DependenceService);
|
||||
const data = await dependenceService.dependencies(req.query as any);
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
logger.error('🔥 error: %o', e);
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.post(
|
||||
'/',
|
||||
|
|
|
@ -98,34 +98,32 @@ export default class DependenceService {
|
|||
searchValue,
|
||||
type,
|
||||
status,
|
||||
}: { searchValue: string; type: string; status: string },
|
||||
}: {
|
||||
searchValue: string;
|
||||
type: keyof typeof DependenceTypes;
|
||||
status: string;
|
||||
},
|
||||
sort: any = [],
|
||||
query: any = {},
|
||||
): Promise<Dependence[]> {
|
||||
let condition = {
|
||||
...query,
|
||||
type: DependenceTypes[type as any],
|
||||
};
|
||||
let condition = query;
|
||||
if (type && ['nodejs', 'python3', 'linux'].includes(type)) {
|
||||
condition.type = DependenceTypes[type];
|
||||
} else condition.type = [0, 1, 2];
|
||||
if (status) {
|
||||
condition.status = status.split(',').map(Number);
|
||||
}
|
||||
if (searchValue) {
|
||||
const encodeText = encodeURI(searchValue);
|
||||
const reg = {
|
||||
condition.name = {
|
||||
[Op.or]: [
|
||||
{ [Op.like]: `%${searchValue}%` },
|
||||
{ [Op.like]: `%${encodeText}%` },
|
||||
],
|
||||
};
|
||||
|
||||
condition = {
|
||||
...condition,
|
||||
name: reg,
|
||||
};
|
||||
}
|
||||
try {
|
||||
const result = await this.find(condition, sort);
|
||||
return result as any;
|
||||
return await this.find(condition, sort);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user