Flowise

Форк
0
76 строк · 2.7 Кб
1
import { Request, Response, NextFunction } from 'express'
2
import variablesService from '../../services/variables'
3
import { Variable } from '../../database/entities/Variable'
4
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
5
import { StatusCodes } from 'http-status-codes'
6

7
const createVariable = async (req: Request, res: Response, next: NextFunction) => {
8
    try {
9
        if (typeof req.body === 'undefined') {
10
            throw new InternalFlowiseError(
11
                StatusCodes.PRECONDITION_FAILED,
12
                `Error: variablesController.createVariable - body not provided!`
13
            )
14
        }
15
        const body = req.body
16
        const newVariable = new Variable()
17
        Object.assign(newVariable, body)
18
        const apiResponse = await variablesService.createVariable(newVariable)
19
        return res.json(apiResponse)
20
    } catch (error) {
21
        next(error)
22
    }
23
}
24

25
const deleteVariable = async (req: Request, res: Response, next: NextFunction) => {
26
    try {
27
        if (typeof req.params === 'undefined' || !req.params.id) {
28
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, 'Error: variablesController.deleteVariable - id not provided!')
29
        }
30
        const apiResponse = await variablesService.deleteVariable(req.params.id)
31
        return res.json(apiResponse)
32
    } catch (error) {
33
        next(error)
34
    }
35
}
36

37
const getAllVariables = async (req: Request, res: Response, next: NextFunction) => {
38
    try {
39
        const apiResponse = await variablesService.getAllVariables()
40
        return res.json(apiResponse)
41
    } catch (error) {
42
        next(error)
43
    }
44
}
45

46
const updateVariable = async (req: Request, res: Response, next: NextFunction) => {
47
    try {
48
        if (typeof req.params === 'undefined' || !req.params.id) {
49
            throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, 'Error: variablesController.updateVariable - id not provided!')
50
        }
51
        if (typeof req.body === 'undefined') {
52
            throw new InternalFlowiseError(
53
                StatusCodes.PRECONDITION_FAILED,
54
                'Error: variablesController.updateVariable - body not provided!'
55
            )
56
        }
57
        const variable = await variablesService.getVariableById(req.params.id)
58
        if (!variable) {
59
            return res.status(404).send(`Variable ${req.params.id} not found in the database`)
60
        }
61
        const body = req.body
62
        const updatedVariable = new Variable()
63
        Object.assign(updatedVariable, body)
64
        const apiResponse = await variablesService.updateVariable(variable, updatedVariable)
65
        return res.json(apiResponse)
66
    } catch (error) {
67
        next(error)
68
    }
69
}
70

71
export default {
72
    createVariable,
73
    deleteVariable,
74
    getAllVariables,
75
    updateVariable
76
}
77

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

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

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

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