-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text_CRUDs.go
373 lines (319 loc) · 9.42 KB
/
Text_CRUDs.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// generated code - do not edit
package controllers
import (
"log"
"net/http"
"sync"
"time"
"github.com/fullstack-lang/gongsvg/go/models"
"github.com/fullstack-lang/gongsvg/go/orm"
"github.com/gin-gonic/gin"
)
// declaration in order to justify use of the models import
var __Text__dummysDeclaration__ models.Text
var __Text_time__dummyDeclaration time.Duration
var mutexText sync.Mutex
// An TextID parameter model.
//
// This is used for operations that want the ID of an order in the path
// swagger:parameters getText updateText deleteText
type TextID struct {
// The ID of the order
//
// in: path
// required: true
ID int64
}
// TextInput is a schema that can validate the user’s
// input to prevent us from getting invalid data
// swagger:parameters postText updateText
type TextInput struct {
// The Text to submit or modify
// in: body
Text *orm.TextAPI
}
// GetTexts
//
// swagger:route GET /texts texts getTexts
//
// # Get all texts
//
// Responses:
// default: genericError
//
// 200: textDBResponse
func (controller *Controller) GetTexts(c *gin.Context) {
// source slice
var textDBs []orm.TextDB
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetTexts", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack github.com/fullstack-lang/gongsvg/go/models, Unkown stack", stackPath)
}
db := backRepo.BackRepoText.GetDB()
query := db.Find(&textDBs)
if query.Error != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = query.Error.Error()
log.Println(query.Error.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// slice that will be transmitted to the front
textAPIs := make([]orm.TextAPI, 0)
// for each text, update fields from the database nullable fields
for idx := range textDBs {
textDB := &textDBs[idx]
_ = textDB
var textAPI orm.TextAPI
// insertion point for updating fields
textAPI.ID = textDB.ID
textDB.CopyBasicFieldsToText_WOP(&textAPI.Text_WOP)
textAPI.TextPointersEncoding = textDB.TextPointersEncoding
textAPIs = append(textAPIs, textAPI)
}
c.JSON(http.StatusOK, textAPIs)
}
// PostText
//
// swagger:route POST /texts texts postText
//
// Creates a text
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 200: nodeDBResponse
func (controller *Controller) PostText(c *gin.Context) {
mutexText.Lock()
defer mutexText.Unlock()
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("PostTexts", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack github.com/fullstack-lang/gongsvg/go/models, Unkown stack", stackPath)
}
db := backRepo.BackRepoText.GetDB()
// Validate input
var input orm.TextAPI
err := c.ShouldBindJSON(&input)
if err != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = err.Error()
log.Println(err.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// Create text
textDB := orm.TextDB{}
textDB.TextPointersEncoding = input.TextPointersEncoding
textDB.CopyBasicFieldsFromText_WOP(&input.Text_WOP)
query := db.Create(&textDB)
if query.Error != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = query.Error.Error()
log.Println(query.Error.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// get an instance (not staged) from DB instance, and call callback function
backRepo.BackRepoText.CheckoutPhaseOneInstance(&textDB)
text := backRepo.BackRepoText.Map_TextDBID_TextPtr[textDB.ID]
if text != nil {
models.AfterCreateFromFront(backRepo.GetStage(), text)
}
// a POST is equivalent to a back repo commit increase
// (this will be improved with implementation of unit of work design pattern)
backRepo.IncrementPushFromFrontNb()
c.JSON(http.StatusOK, textDB)
}
// GetText
//
// swagger:route GET /texts/{ID} texts getText
//
// Gets the details for a text.
//
// Responses:
// default: genericError
//
// 200: textDBResponse
func (controller *Controller) GetText(c *gin.Context) {
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetText", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack github.com/fullstack-lang/gongsvg/go/models, Unkown stack", stackPath)
}
db := backRepo.BackRepoText.GetDB()
// Get textDB in DB
var textDB orm.TextDB
if err := db.First(&textDB, c.Param("id")).Error; err != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = err.Error()
log.Println(err.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
var textAPI orm.TextAPI
textAPI.ID = textDB.ID
textAPI.TextPointersEncoding = textDB.TextPointersEncoding
textDB.CopyBasicFieldsToText_WOP(&textAPI.Text_WOP)
c.JSON(http.StatusOK, textAPI)
}
// UpdateText
//
// swagger:route PATCH /texts/{ID} texts updateText
//
// # Update a text
//
// Responses:
// default: genericError
//
// 200: textDBResponse
func (controller *Controller) UpdateText(c *gin.Context) {
mutexText.Lock()
defer mutexText.Unlock()
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("UpdateText", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack github.com/fullstack-lang/gongsvg/go/models, Unkown stack", stackPath)
}
db := backRepo.BackRepoText.GetDB()
// Validate input
var input orm.TextAPI
if err := c.ShouldBindJSON(&input); err != nil {
log.Println(err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Get model if exist
var textDB orm.TextDB
// fetch the text
query := db.First(&textDB, c.Param("id"))
if query.Error != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = query.Error.Error()
log.Println(query.Error.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// update
textDB.CopyBasicFieldsFromText_WOP(&input.Text_WOP)
textDB.TextPointersEncoding = input.TextPointersEncoding
query = db.Model(&textDB).Updates(textDB)
if query.Error != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = query.Error.Error()
log.Println(query.Error.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// get an instance (not staged) from DB instance, and call callback function
textNew := new(models.Text)
textDB.CopyBasicFieldsToText(textNew)
// redeem pointers
textDB.DecodePointers(backRepo, textNew)
// get stage instance from DB instance, and call callback function
textOld := backRepo.BackRepoText.Map_TextDBID_TextPtr[textDB.ID]
if textOld != nil {
models.AfterUpdateFromFront(backRepo.GetStage(), textOld, textNew)
}
// an UPDATE generates a back repo commit increase
// (this will be improved with implementation of unit of work design pattern)
// in some cases, with the marshalling of the stage, this operation might
// generates a checkout
backRepo.IncrementPushFromFrontNb()
// return status OK with the marshalling of the the textDB
c.JSON(http.StatusOK, textDB)
}
// DeleteText
//
// swagger:route DELETE /texts/{ID} texts deleteText
//
// # Delete a text
//
// default: genericError
//
// 200: textDBResponse
func (controller *Controller) DeleteText(c *gin.Context) {
mutexText.Lock()
defer mutexText.Unlock()
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("DeleteText", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack github.com/fullstack-lang/gongsvg/go/models, Unkown stack", stackPath)
}
db := backRepo.BackRepoText.GetDB()
// Get model if exist
var textDB orm.TextDB
if err := db.First(&textDB, c.Param("id")).Error; err != nil {
var returnError GenericError
returnError.Body.Code = http.StatusBadRequest
returnError.Body.Message = err.Error()
log.Println(err.Error())
c.JSON(http.StatusBadRequest, returnError.Body)
return
}
// with gorm.Model field, default delete is a soft delete. Unscoped() force delete
db.Unscoped().Delete(&textDB)
// get an instance (not staged) from DB instance, and call callback function
textDeleted := new(models.Text)
textDB.CopyBasicFieldsToText(textDeleted)
// get stage instance from DB instance, and call callback function
textStaged := backRepo.BackRepoText.Map_TextDBID_TextPtr[textDB.ID]
if textStaged != nil {
models.AfterDeleteFromFront(backRepo.GetStage(), textStaged, textDeleted)
}
// a DELETE generates a back repo commit increase
// (this will be improved with implementation of unit of work design pattern)
backRepo.IncrementPushFromFrontNb()
c.JSON(http.StatusOK, gin.H{"data": true})
}