HomeAccounting

Форк
0
112 строк · 2.8 Кб
1
import WebSql from '../WebSql';
2
import axios from 'axios';
3

4
export default class Currency {
5

6
    constructor ()
7
    {
8

9
        this.dataProvider = {};
10

11
        this.tableName = 'currency';
12

13
        this.db = new WebSql(this.tableName, '1.0', 'currency information table', 20000000);
14

15
        let columns = [
16
            {
17
                name: 'num_code',
18
                type: 'INTEGER',
19
                unique: true
20
            },
21
            {
22
                name: 'char_code',
23
                type: 'VARCHAR(25)',
24
                unique: true
25
            },
26
            {
27
                name: 'nominal',
28
                type: 'INTEGER'
29
            },
30
            {
31
                name: 'name',
32
                type: 'TEXT'
33
            },
34
            {
35
                name: 'value',
36
                type: 'DECIMAL(10,4)'
37
            }
38
        ];
39

40
        this.db.createTable(this.tableName, columns);
41
    }
42

43
    fillTable()
44
    {
45

46
        const columns = ['num_code', 'char_code', 'nominal', 'name', 'value'];
47

48
        this.dataProvider = '';
49

50
        this.getCurrenciesFromServer();
51

52
        let interval = setInterval(() => {
53

54
            if(this.dataProvider !== '') {
55

56
                clearInterval(interval);
57

58
                for (let key in this.dataProvider) {
59
                    let values = [
60
                        this.dataProvider[key].num_code,
61
                        this.dataProvider[key].char_code,
62
                        this.dataProvider[key].nominal,
63
                        this.dataProvider[key].name,
64
                        this.dataProvider[key].value
65
                    ];
66

67
                    this.db.insertRecordToTable(this.tableName, columns, values);
68
                }
69
            }
70

71
        }, 2000);
72
    }
73

74
    getCurrencies()
75
    {
76
        return new Promise((resolve, reject) => {
77
            this.db.db.transaction((tx) => {
78
                tx.executeSql("SELECT * FROM " + this.tableName, [], (trans, result) => {
79
                    resolve(result.rows);
80
                }, (error) => {
81
                    reject(error);
82
                })
83
            });
84
        });
85
    }
86

87
    getCurrency(currencyCode)
88
    {
89
        let query = "SELECT * FROM " + this.tableName + " WHERE num_code=?";
90

91
        return new Promise((resolve, reject) => {
92
            this.db.db.transaction((tx) => {
93
                tx.executeSql(query, [currencyCode], (trans, result) => {
94
                    resolve(result.rows[0]);
95
                }, (error) => {
96
                    reject(error);
97
                })
98
            });
99
        });
100
    }
101

102
    getCurrenciesFromServer()
103
    {
104
        axios.get('/pa/get-currencies')
105
            .then(response=> {
106
                this.dataProvider = response.data.data;
107
            })
108
            .catch(error => {
109
                console.log(error);
110
            });
111
    }
112
}

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

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

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

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