Flowise

Форк
0
72 строки · 2.5 Кб
1
import { Request, Response, NextFunction } from 'express'
2
import toolsService from '../../services/tools'
3
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
4
import { StatusCodes } from 'http-status-codes'
5

6
const createTool = async (req: Request, res: Response, next: NextFunction) => {
7
    try {
8
        if (!req.body) {
9
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: toolsController.createTool - body not provided!`)
10
        }
11
        const apiResponse = await toolsService.createTool(req.body)
12
        return res.json(apiResponse)
13
    } catch (error) {
14
        next(error)
15
    }
16
}
17

18
const deleteTool = async (req: Request, res: Response, next: NextFunction) => {
19
    try {
20
        if (typeof req.params === 'undefined' || !req.params.id) {
21
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: toolsController.deleteTool - id not provided!`)
22
        }
23
        const apiResponse = await toolsService.deleteTool(req.params.id)
24
        return res.json(apiResponse)
25
    } catch (error) {
26
        next(error)
27
    }
28
}
29

30
const getAllTools = async (req: Request, res: Response, next: NextFunction) => {
31
    try {
32
        const apiResponse = await toolsService.getAllTools()
33
        return res.json(apiResponse)
34
    } catch (error) {
35
        next(error)
36
    }
37
}
38

39
const getToolById = async (req: Request, res: Response, next: NextFunction) => {
40
    try {
41
        if (typeof req.params === 'undefined' || !req.params.id) {
42
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: toolsController.getToolById - id not provided!`)
43
        }
44
        const apiResponse = await toolsService.getToolById(req.params.id)
45
        return res.json(apiResponse)
46
    } catch (error) {
47
        next(error)
48
    }
49
}
50

51
const updateTool = async (req: Request, res: Response, next: NextFunction) => {
52
    try {
53
        if (typeof req.params === 'undefined' || !req.params.id) {
54
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: toolsController.updateTool - id not provided!`)
55
        }
56
        if (!req.body) {
57
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: toolsController.deleteTool - body not provided!`)
58
        }
59
        const apiResponse = await toolsService.updateTool(req.params.id, req.body)
60
        return res.json(apiResponse)
61
    } catch (error) {
62
        next(error)
63
    }
64
}
65

66
export default {
67
    createTool,
68
    deleteTool,
69
    getAllTools,
70
    getToolById,
71
    updateTool
72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.