-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_mapping.go
301 lines (249 loc) · 7.92 KB
/
record_mapping.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"github.com/diapco/votecube-crud/deserialize"
"github.com/diapco/votecube-crud/models"
"github.com/diapco/votecube-crud/sequence"
"github.com/diapco/votecube-crud/serialize"
"time"
)
type RecordArrays struct {
Polls []*models.Poll
PollContinents []*models.PollsContinent
PollCountries []*models.PollsCountry
PollStates []*models.PollsState
PollTowns []*models.PollsTown
PollFactorPositions []*models.PollsFactorsPosition
FactorPositions []*models.FactorPosition
Factors []*models.Factor
Positions []*models.Position
PollLabels []*models.PollsLabel
Labels []*models.Label
}
func MoveRecordsToArrays(
batch *RequestBatch,
) (RecordArrays, []*deserialize.CreatePollRequest) {
var validRequests []*deserialize.CreatePollRequest
var polls []*models.Poll
var pollContinents []*models.PollsContinent
var pollCountries []*models.PollsCountry
var pollStates []*models.PollsState
var pollTowns []*models.PollsTown
var pollFactorPositions []*models.PollsFactorsPosition
var factorPositions []*models.FactorPosition
var factors []*models.Factor
var positions []*models.Position
var pollLabels []*models.PollsLabel
var labels []*models.Label
for _, request := range batch.Data {
if request == nil {
continue
}
validRequests = append(validRequests, request)
//i = i + 1
//request.Index = i
poll := request.Poll
polls = append(polls, &poll)
for _, pollContinent := range poll.R.PollsContinents {
pollContinents = append(pollContinents, pollContinent)
}
pollsCountries := poll.R.PollsCountries
if pollsCountries != nil {
for _, pollCountry := range pollsCountries {
pollCountries = append(pollCountries, pollCountry)
}
}
pollsStates := poll.R.PollsStates
if pollsStates != nil {
for _, pollState := range pollsStates {
pollStates = append(pollStates, pollState)
}
}
pollsTowns := poll.R.PollsTowns
if pollsTowns != nil {
for _, pollTown := range pollsTowns {
pollTowns = append(pollTowns, pollTown)
}
}
for _, pollFactorPosition := range poll.R.PollsFactorsPositions {
pollFactorPositions = append(pollFactorPositions, pollFactorPosition)
if pollFactorPosition.FactorPositionID == 0 {
factorPosition := pollFactorPosition.R.FactorPosition
factorPositions = append(factorPositions, factorPosition)
if factorPosition.FactorID == 0 {
factors = append(factors, factorPosition.R.Factor)
}
if factorPosition.PositionID == 0 {
positions = append(positions, factorPosition.R.Position)
}
}
}
pollsLabels := poll.R.PollsLabels
if pollsLabels != nil {
for _, pollLabel := range pollsLabels {
pollLabels = append(pollLabels, pollLabel)
if pollLabel.LabelID == 0 {
labels = append(labels, pollLabel.R.Label)
}
}
}
}
return RecordArrays{
Polls: polls,
PollContinents: pollContinents,
PollCountries: pollCountries,
PollStates: pollStates,
PollTowns: pollTowns,
PollFactorPositions: pollFactorPositions,
FactorPositions: factorPositions,
Factors: factors,
Positions: positions,
PollLabels: pollLabels,
Labels: labels,
}, validRequests
}
func AssignSeqValues(
recArrs *RecordArrays,
) error {
pollSeqCursor, err := sequence.PollId.GetCursor(len(recArrs.Polls))
if err != nil {
return err
}
pollContinentSeqCursor, err := sequence.PollContinentId.GetCursor(len(recArrs.PollContinents))
if err != nil {
return err
}
pollCountrySeqCursor, err := sequence.PollCountryId.GetCursor(len(recArrs.PollCountries))
if err != nil {
return err
}
pollStateSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.PollStates))
if err != nil {
return err
}
pollTownSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.PollTowns))
if err != nil {
return err
}
pollFactorPositionSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.PollFactorPositions))
if err != nil {
return err
}
factorPositionSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.FactorPositions))
if err != nil {
return err
}
factorSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.Factors))
if err != nil {
return err
}
positionSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.Positions))
if err != nil {
return err
}
pollLabelSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.PollLabels))
if err != nil {
return err
}
labelSeqCursor, err := sequence.PollStateId.GetCursor(len(recArrs.Labels))
if err != nil {
return err
}
for _, poll := range recArrs.Polls {
poll.PollID = pollSeqCursor.Next()
}
for _, pollContinent := range recArrs.PollContinents {
pollContinent.PollContinentID = pollContinentSeqCursor.Next()
}
for _, pollCountry := range recArrs.PollCountries {
pollCountry.PollCountryID = pollCountrySeqCursor.Next()
}
for _, pollState := range recArrs.PollStates {
pollState.PollStateID = pollStateSeqCursor.Next()
}
for _, pollTown := range recArrs.PollTowns {
pollTown.PollTownID = pollTownSeqCursor.Next()
}
for _, pollFactorPosition := range recArrs.PollFactorPositions {
pollFactorPosition.PollFactorPositionID = pollFactorPositionSeqCursor.Next()
}
for _, factorPosition := range recArrs.FactorPositions {
factorPosition.FactorPositionID = factorPositionSeqCursor.Next()
}
for _, factor := range recArrs.Factors {
factor.FactorID = factorSeqCursor.Next()
}
for _, position := range recArrs.Positions {
position.PositionID = positionSeqCursor.Next()
}
for _, pollLabel := range recArrs.PollLabels {
pollLabel.PollLabelID = pollLabelSeqCursor.Next()
}
for _, label := range recArrs.Labels {
label.LabelID = labelSeqCursor.Next()
}
return nil
}
func PopulateResponseAndCrossReferences(
validRequests *[]*deserialize.CreatePollRequest,
) {
now := time.Now()
for _, request := range *validRequests {
if request == nil {
continue
}
responseData := make([]byte, 0)
request.ResponseData = &responseData
poll := request.Poll
pollId := poll.PollID
poll.CreatedAt = now
serialize.WNum(poll.PollID, request.ResponseData)
for _, pollContinent := range poll.R.PollsContinents {
pollContinent.PollID = pollId
serialize.WNum(pollContinent.PollContinentID, request.ResponseData)
}
for _, pollCountry := range poll.R.PollsCountries {
pollCountry.PollID = pollId
serialize.WNum(pollCountry.PollCountryID, request.ResponseData)
}
for _, pollState := range poll.R.PollsStates {
pollState.PollID = pollId
serialize.WNum(pollState.PollStateID, request.ResponseData)
}
for _, pollTown := range poll.R.PollsTowns {
pollTown.PollID = pollId
serialize.WNum(pollTown.PollTownID, request.ResponseData)
}
for _, pollFactorPosition := range poll.R.PollsFactorsPositions {
pollFactorPosition.PollID = pollId
serialize.WNum(pollFactorPosition.PollFactorPositionID, request.ResponseData)
factorPosition := pollFactorPosition.R.FactorPosition
if factorPosition != nil {
factorPosition.CreatedAt = now
pollFactorPosition.FactorPositionID = factorPosition.FactorPositionID
serialize.WNum(factorPosition.FactorPositionID, request.ResponseData)
factor := factorPosition.R.Factor
if factor != nil {
factor.CreatedAt = now
factorPosition.FactorID = factor.FactorID
serialize.WNum(factor.FactorID, request.ResponseData)
}
position := factorPosition.R.Position
if position != nil {
position.CreatedAt = now
factorPosition.PositionID = position.PositionID
serialize.WNum(position.PositionID, request.ResponseData)
}
}
}
for _, pollLabel := range poll.R.PollsLabels {
pollLabel.PollID = pollId
serialize.WNum(pollLabel.PollLabelID, request.ResponseData)
label := pollLabel.R.Label
if label != nil {
label.CreatedAt = now
pollLabel.LabelID = label.LabelID
serialize.WNum(label.LabelID, request.ResponseData)
}
}
}
}