/
put.go
211 lines (180 loc) · 4.93 KB
/
put.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (c) 2022-2023. Author: Ruslan Bikchentaev. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
* Перший приватний програміст.
*/
package crud
import (
"bytes"
"fmt"
"go/types"
"io"
"mime/multipart"
"strings"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"github.com/ruslanBik4/dbEngine/dbEngine"
"github.com/ruslanBik4/httpgo/apis"
"github.com/ruslanBik4/logs"
)
// TableInsert insert data of {params} into {table}
func TableInsert(preRoute string, table dbEngine.Table, params []string) apis.ApiRouteHandler {
return func(ctx *fasthttp.RequestCtx) (any, error) {
args := make([]any, 0, len(params))
colSel := make([]string, 0, len(params))
badParams := make(map[string]string, 0)
buf := bytes.NewBufferString("")
for _, name := range params {
arg := ctx.UserValue(name)
if arg == nil {
continue
}
n, a := AddColumnAndValue(name, table, arg, buf, badParams)
colSel = append(colSel, n)
args = append(args, a)
}
if len(badParams) > 0 {
return badParams, apis.ErrWrongParamsList
}
id, err := table.Insert(ctx,
dbEngine.ColumnsForSelect(colSel...),
dbEngine.ArgsForSelect(args...),
)
if err != nil {
return CreateErrResult(err)
}
return RenderCreatedResult(ctx, id, buf, colSel, preRoute+table.Name())
}
}
// TableUpdate update data in {table}
func TableUpdate(preRoute string, table dbEngine.Table, columns, priColumns []string) apis.ApiRouteHandler {
return func(ctx *fasthttp.RequestCtx) (any, error) {
badParams := make(map[string]string, 0)
for _, key := range priColumns {
if ctx.UserValue(key) == nil {
badParams[key] = "required params"
}
}
args := make([]any, 0, len(columns))
colSel := make([]string, 0, len(columns))
buf := bytes.NewBufferString("")
for _, name := range columns {
isPrimary := false
for _, priName := range priColumns {
if name == priName {
arg := ctx.UserValue("new." + priName)
if arg != nil {
colSel = append(colSel, priName)
args = append(args, arg)
_, err := fmt.Fprintf(buf, " %v", arg)
if err != nil {
return nil, err
}
}
isPrimary = true
break
}
}
if isPrimary {
continue
}
arg := ctx.UserValue(name)
if arg == nil {
continue
}
n, a := AddColumnAndValue(name, table, arg, buf, badParams)
colSel = append(colSel, n)
args = append(args, a)
}
if len(badParams) > 0 {
return badParams, apis.ErrWrongParamsList
}
for _, name := range priColumns {
args = append(args, ctx.UserValue(name))
}
i, err := table.Update(ctx,
dbEngine.ColumnsForSelect(colSel...),
dbEngine.WhereForSelect(priColumns...),
dbEngine.ArgsForSelect(args...),
)
if err != nil {
return nil, err
}
if i <= 0 {
logs.DebugLog(colSel, priColumns)
logs.DebugLog(args)
return map[string]string{"update": fmt.Sprintf("%d", i)}, apis.ErrWrongParamsList
}
return RenderAcceptedResult(ctx, colSel, buf, preRoute+table.Name())
}
}
func AddColumnAndValue(name string, table dbEngine.Table, arg any, buf io.Writer, badParams map[string]string) (string, any) {
colName := strings.TrimSuffix(name, "[]")
col := table.FindColumn(colName)
if col == nil {
badParams[colName] = dbEngine.ErrNotFoundColumn{Table: table.Name(), Column: colName}.Error()
return "", nil
}
switch col.BasicType() {
case types.UnsafePointer:
switch val := arg.(type) {
case nil, string:
badParams[colName] = "wrong type of file"
case []*multipart.FileHeader:
names, bytea, err := ReadByteA(val)
if err != nil {
logs.DebugLog(names)
badParams[colName] = err.Error()
return "", nil
}
switch len(bytea) {
case 0:
badParams[colName] = "empty file"
case 1:
_, err := buf.Write([]byte("[file]"))
if err != nil {
logs.ErrorLog(err)
}
return colName, bytea[0]
default:
_, err := buf.Write([]byte("[files]"))
if err != nil {
logs.ErrorLog(err)
}
return colName, bytea
}
default:
badParams[colName] = fmt.Sprintf("unknown type of value: %T", val)
}
default:
_, err := fmt.Fprintf(buf, " %v", arg)
logs.ErrorLog(err)
return colName, arg
}
return "", nil
}
type insertResult struct {
FormActions []FormActions `json:"formActions"`
Id int64 `json:"id,omitempty"`
Msg string `json:"message"`
}
func ReadByteA(fHeaders []*multipart.FileHeader) ([]string, [][]byte, error) {
bytea := make([][]byte, len(fHeaders))
names := make([]string, len(fHeaders))
for i, fHeader := range fHeaders {
f, err := fHeader.Open()
if err != nil {
logs.DebugLog(err, fHeader)
return nil, nil, errors.Wrap(err, fHeader.Filename)
}
b, err := io.ReadAll(f)
if err != nil {
logs.DebugLog(err, fHeader)
return nil, nil, errors.Wrap(err, "read "+fHeader.Filename)
}
bytea[i] = b
names[i] = fHeader.Filename
}
return names, bytea, nil
}