|
| 1 | +import { RequestHandler, Router } from 'express'; |
| 2 | + |
| 3 | +import { |
| 4 | + difyIgnoreJidSchema, |
| 5 | + difySchema, |
| 6 | + difySettingSchema, |
| 7 | + difyStatusSchema, |
| 8 | + instanceSchema, |
| 9 | +} from '../../../../validate/validate.schema'; |
| 10 | +import { RouterBroker } from '../../../abstract/abstract.router'; |
| 11 | +import { InstanceDto } from '../../../dto/instance.dto'; |
| 12 | +import { HttpStatus } from '../../../routes/index.router'; |
| 13 | +import { difyController } from '../../../server.module'; |
| 14 | +import { DifyDto, DifyIgnoreJidDto, DifySettingDto } from '../dto/dify.dto'; |
| 15 | + |
| 16 | +export class DifyRouter extends RouterBroker { |
| 17 | + constructor(...guards: RequestHandler[]) { |
| 18 | + super(); |
| 19 | + this.router |
| 20 | + .post(this.routerPath('create'), ...guards, async (req, res) => { |
| 21 | + const response = await this.dataValidate<DifyDto>({ |
| 22 | + request: req, |
| 23 | + schema: difySchema, |
| 24 | + ClassRef: DifyDto, |
| 25 | + execute: (instance, data) => difyController.createDify(instance, data), |
| 26 | + }); |
| 27 | + |
| 28 | + res.status(HttpStatus.CREATED).json(response); |
| 29 | + }) |
| 30 | + .get(this.routerPath('find'), ...guards, async (req, res) => { |
| 31 | + const response = await this.dataValidate<InstanceDto>({ |
| 32 | + request: req, |
| 33 | + schema: instanceSchema, |
| 34 | + ClassRef: InstanceDto, |
| 35 | + execute: (instance) => difyController.findDify(instance), |
| 36 | + }); |
| 37 | + |
| 38 | + res.status(HttpStatus.OK).json(response); |
| 39 | + }) |
| 40 | + .get(this.routerPath('fetch/:difyId'), ...guards, async (req, res) => { |
| 41 | + const response = await this.dataValidate<InstanceDto>({ |
| 42 | + request: req, |
| 43 | + schema: instanceSchema, |
| 44 | + ClassRef: InstanceDto, |
| 45 | + execute: (instance) => difyController.fetchDify(instance, req.params.difyId), |
| 46 | + }); |
| 47 | + |
| 48 | + res.status(HttpStatus.OK).json(response); |
| 49 | + }) |
| 50 | + .put(this.routerPath('update/:difyId'), ...guards, async (req, res) => { |
| 51 | + const response = await this.dataValidate<DifyDto>({ |
| 52 | + request: req, |
| 53 | + schema: difySchema, |
| 54 | + ClassRef: DifyDto, |
| 55 | + execute: (instance, data) => difyController.updateDify(instance, req.params.difyId, data), |
| 56 | + }); |
| 57 | + |
| 58 | + res.status(HttpStatus.OK).json(response); |
| 59 | + }) |
| 60 | + .delete(this.routerPath('delete/:difyId'), ...guards, async (req, res) => { |
| 61 | + const response = await this.dataValidate<InstanceDto>({ |
| 62 | + request: req, |
| 63 | + schema: instanceSchema, |
| 64 | + ClassRef: InstanceDto, |
| 65 | + execute: (instance) => difyController.deleteDify(instance, req.params.difyId), |
| 66 | + }); |
| 67 | + |
| 68 | + res.status(HttpStatus.OK).json(response); |
| 69 | + }) |
| 70 | + .post(this.routerPath('settings'), ...guards, async (req, res) => { |
| 71 | + const response = await this.dataValidate<DifySettingDto>({ |
| 72 | + request: req, |
| 73 | + schema: difySettingSchema, |
| 74 | + ClassRef: DifySettingDto, |
| 75 | + execute: (instance, data) => difyController.settings(instance, data), |
| 76 | + }); |
| 77 | + |
| 78 | + res.status(HttpStatus.OK).json(response); |
| 79 | + }) |
| 80 | + .get(this.routerPath('fetchSettings'), ...guards, async (req, res) => { |
| 81 | + const response = await this.dataValidate<InstanceDto>({ |
| 82 | + request: req, |
| 83 | + schema: instanceSchema, |
| 84 | + ClassRef: InstanceDto, |
| 85 | + execute: (instance) => difyController.fetchSettings(instance), |
| 86 | + }); |
| 87 | + |
| 88 | + res.status(HttpStatus.OK).json(response); |
| 89 | + }) |
| 90 | + .post(this.routerPath('changeStatus'), ...guards, async (req, res) => { |
| 91 | + const response = await this.dataValidate<InstanceDto>({ |
| 92 | + request: req, |
| 93 | + schema: difyStatusSchema, |
| 94 | + ClassRef: InstanceDto, |
| 95 | + execute: (instance, data) => difyController.changeStatus(instance, data), |
| 96 | + }); |
| 97 | + |
| 98 | + res.status(HttpStatus.OK).json(response); |
| 99 | + }) |
| 100 | + .get(this.routerPath('fetchSessions/:difyId'), ...guards, async (req, res) => { |
| 101 | + const response = await this.dataValidate<InstanceDto>({ |
| 102 | + request: req, |
| 103 | + schema: instanceSchema, |
| 104 | + ClassRef: InstanceDto, |
| 105 | + execute: (instance) => difyController.fetchSessions(instance, req.params.difyId), |
| 106 | + }); |
| 107 | + |
| 108 | + res.status(HttpStatus.OK).json(response); |
| 109 | + }) |
| 110 | + .post(this.routerPath('ignoreJid'), ...guards, async (req, res) => { |
| 111 | + const response = await this.dataValidate<DifyIgnoreJidDto>({ |
| 112 | + request: req, |
| 113 | + schema: difyIgnoreJidSchema, |
| 114 | + ClassRef: DifyIgnoreJidDto, |
| 115 | + execute: (instance, data) => difyController.ignoreJid(instance, data), |
| 116 | + }); |
| 117 | + |
| 118 | + res.status(HttpStatus.OK).json(response); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public readonly router = Router(); |
| 123 | +} |
0 commit comments