playground.controller.ts 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { NextFunction, Request, Response, Router } from 'express';
  2. import { dtoValidationMiddleware } from '../middleware/validation';
  3. import { PlaygroundService } from './playground.service';
  4. import { PlaygroundRequestDto } from './dto';
  5. export class PlaygroundController {
  6. private playgroundService: PlaygroundService;
  7. private router: Router;
  8. constructor() {
  9. this.playgroundService = new PlaygroundService();
  10. this.router = Router();
  11. }
  12. get playgroundServiceGetter() {
  13. return this.playgroundService;
  14. }
  15. generateRoutes() {
  16. this.router.post(
  17. '/',
  18. dtoValidationMiddleware(PlaygroundRequestDto),
  19. this.handleRequest.bind(this)
  20. );
  21. return this.router;
  22. }
  23. async handleRequest(
  24. req: Request<{}, {}, PlaygroundRequestDto>,
  25. res: Response,
  26. next: NextFunction
  27. ) {
  28. try {
  29. const result = await this.playgroundService.makeRequest(req.body);
  30. res.send(result);
  31. } catch (error) {
  32. next(error);
  33. }
  34. }
  35. }