qinglong/back/api/scenario.ts
copilot-swe-agent[bot] af88062219 Add Scenario Mode with workflow editor - backend and frontend implementation
Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-23 05:32:56 +00:00

145 lines
3.8 KiB
TypeScript

import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import ScenarioService from '../services/scenario';
import { celebrate, Joi } from 'celebrate';
const route = Router();
export default (app: Router) => {
app.use('/scenarios', route);
route.get(
'/',
celebrate({
query: Joi.object({
searchValue: Joi.string().optional().allow(''),
page: Joi.number().optional(),
size: Joi.number().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
const { searchValue, page, size } = req.query as any;
const result = await scenarioService.list(
searchValue,
page ? parseInt(page) : undefined,
size ? parseInt(size) : undefined,
);
return res.send({ code: 200, data: result });
} catch (e) {
return next(e);
}
},
);
route.post(
'/',
celebrate({
body: Joi.object({
name: Joi.string().required(),
description: Joi.string().optional().allow(''),
workflowGraph: Joi.object().optional(),
status: Joi.number().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
const data = await scenarioService.create(req.body);
return res.send({ code: 200, data });
} catch (e) {
return next(e);
}
},
);
route.put(
'/',
celebrate({
body: Joi.object({
id: Joi.number().required(),
name: Joi.string().required(),
description: Joi.string().optional().allow(''),
workflowGraph: Joi.object().optional(),
status: Joi.number().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
const data = await scenarioService.update(req.body);
return res.send({ code: 200, data });
} catch (e) {
return next(e);
}
},
);
route.delete(
'/',
celebrate({
body: Joi.array().items(Joi.number().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
await scenarioService.remove(req.body);
return res.send({ code: 200 });
} catch (e) {
return next(e);
}
},
);
route.put(
'/disable',
celebrate({
body: Joi.array().items(Joi.number().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
await scenarioService.disabled(req.body);
return res.send({ code: 200 });
} catch (e) {
return next(e);
}
},
);
route.put(
'/enable',
celebrate({
body: Joi.array().items(Joi.number().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
await scenarioService.enabled(req.body);
return res.send({ code: 200 });
} catch (e) {
return next(e);
}
},
);
route.get(
'/:id',
celebrate({
params: Joi.object({
id: Joi.number().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
try {
const scenarioService = Container.get(ScenarioService);
const data = await scenarioService.getDb({ id: parseInt(req.params.id) });
return res.send({ code: 200, data });
} catch (e) {
return next(e);
}
},
);
};