-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxi_fields.go
151 lines (128 loc) · 4.09 KB
/
oxi_fields.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package oxilib
import (
"github.com/oxipass/oxilib/consts"
"github.com/oxipass/oxilib/internal/pkg/oxierr"
"github.com/oxipass/oxilib/internal/pkg/validators"
"github.com/oxipass/oxilib/models"
)
// DeleteField - delete existing field
func (storage *StorageSingleton) DeleteField(deleteFieldForm models.UpdateFieldForm) (response models.CommonResponse, err error) {
err = storage.checkReadiness()
if err != nil {
return response, err
}
err = storage.dbObject.StartTX()
if err != nil {
return response, err
}
err = storage.dbObject.DbDeleteField(deleteFieldForm.ID)
if err != nil {
errEndTX := storage.dbObject.RollbackTX()
if errEndTX != nil {
return response, oxierr.FormError(oxierr.BSERR00016DbDeleteFailed, err.Error(), errEndTX.Error())
}
return response, err
}
err = storage.dbObject.CommitTX()
if err != nil {
return response, err
}
response.Status = consts.CSuccessResponse
return response, nil
}
// AddNewField - adds new field
func (storage *StorageSingleton) AddNewField(addFieldForm models.UpdateFieldForm) (response models.FieldAddedResponse, err error) {
var field models.OxiField
err = storage.checkReadiness()
if err != nil {
return response, err
}
if err := validators.ValidateField(addFieldForm.OxiField); err != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error())
}
field.Name, err = storage.encObject.Encrypt(addFieldForm.Name)
if err != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error())
}
field.Icon, err = storage.encObject.Encrypt(addFieldForm.Icon)
if err != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error())
}
field.ValueType, err = storage.encObject.Encrypt(addFieldForm.ValueType)
if err != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error())
}
field.Value, err = storage.encObject.Encrypt(addFieldForm.Value)
if err != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error())
}
err = storage.dbObject.StartTX()
if err != nil {
return response, err
}
fieldId, err := storage.dbObject.DbInsertField(addFieldForm.ItemID, field)
if err != nil {
errEndTX := storage.dbObject.RollbackTX()
if errEndTX != nil {
return response, oxierr.FormError(oxierr.BSERR00006DbInsertFailed, err.Error(), errEndTX.Error())
}
return response, err
}
err = storage.dbObject.CommitTX()
if err != nil {
return response, err
}
response.Status = consts.CSuccessResponse
response.FieldID = fieldId
return response, nil
}
// ReadFieldsByItemID - real all the fields by ItemId
func (storage *StorageSingleton) ReadFieldsByItemID(itemId int64) (fields []models.OxiField, err error) {
fieldsEncrypted, err := storage.dbObject.DbSelectAllItemFields(itemId)
if err != nil {
return fields, err
}
for _, field := range fieldsEncrypted {
fieldReady, err := storage.DecryptField(field)
if err != nil {
return fields, err
}
fields = append(fields, fieldReady)
}
return fields, nil
}
// ReadFieldsByFieldID - real all the fields by FieldId
func (storage *StorageSingleton) ReadFieldsByFieldID(fieldId int64) (field models.OxiField, err error) {
fieldEncrypted, err := storage.dbObject.DbGetFieldById(fieldId)
if err != nil {
return field, err
}
fieldReady, err := storage.DecryptField(fieldEncrypted)
if err != nil {
return field, err
}
return fieldReady, nil
}
func (storage *StorageSingleton) DecryptField(fieldEncrypted models.OxiField) (field models.OxiField, err error) {
field.Value, err = storage.encObject.Decrypt(fieldEncrypted.Value)
if err != nil {
return field, err
}
field.Name, err = storage.encObject.Decrypt(fieldEncrypted.Name)
if err != nil {
return field, err
}
field.ValueType, err = storage.encObject.Decrypt(fieldEncrypted.ValueType)
if err != nil {
return field, err
}
field.Icon, err = storage.encObject.Decrypt(fieldEncrypted.Icon)
if err != nil {
return field, err
}
field.ID = fieldEncrypted.ID
field.Created = fieldEncrypted.Created
field.Updated = fieldEncrypted.Updated
field.Deleted = fieldEncrypted.Deleted
return field, nil
}