github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/lngcodes/lngcodes.go (about) 1 package lngcodes 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 "sync" 8 "time" 9 10 //"log" 11 "net/http" 12 13 "github.com/gin-gonic/gin" 14 15 config "github.com/mdaxf/iac/config" 16 "github.com/mdaxf/iac/controllers/common" 17 dbconn "github.com/mdaxf/iac/databases" 18 "github.com/mdaxf/iac/framework/auth" 19 "github.com/mdaxf/iac/logger" 20 ) 21 22 type LCController struct { 23 } 24 25 type LCData struct { 26 IDs []int `json:"ids"` 27 Lngcodes []string `json:"lngcodes"` 28 Texts []string `json:"texts"` 29 Shorts []string `json:"shorts"` 30 Languages []string `json:"languages"` 31 Language string `json:"language"` 32 } 33 34 func (f *LCController) GetLngCodes(c *gin.Context) { 35 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "LngCodes"} 36 startTime := time.Now() 37 defer func() { 38 elapsed := time.Since(startTime) 39 iLog.PerformanceWithDuration("controllers.lngcodes.GetLngCodes", elapsed) 40 }() 41 42 defer func() { 43 if err := recover(); err != nil { 44 iLog.Error(fmt.Sprintf("GetLngCodes error: %s", err)) 45 c.JSON(http.StatusBadRequest, gin.H{"error": err}) 46 } 47 }() 48 49 _, LoginName, _, err := auth.GetUserInformation(c) 50 if err != nil { 51 iLog.Error(fmt.Sprintf("Get LngCode error: %s", err.Error())) 52 } 53 if LoginName == "" { 54 LoginName = "System" 55 } 56 57 iLog.User = LoginName 58 59 iLog.Debug(fmt.Sprintf("Get LngCodes")) 60 61 body, err := common.GetRequestBody(c) 62 63 if err != nil { 64 iLog.Error(fmt.Sprintf("Get LngCodes error: %s", err.Error())) 65 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 66 return 67 } 68 69 var lcdata LCData 70 err = json.Unmarshal(body, &lcdata) 71 if err != nil { 72 iLog.Error(fmt.Sprintf("Get LngCodes get the message body error: %s", err.Error())) 73 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 74 return 75 } 76 77 iLog.Debug(fmt.Sprintf("Get LngCodes lcdata: %s", lcdata)) 78 79 language := "en" 80 if lcdata.Language == "" { 81 language = "en" 82 } else { 83 language = lcdata.Language 84 } 85 86 if len(lcdata.Lngcodes) == 0 { 87 iLog.Error(fmt.Sprintf("Get LngCodes error: %s", "lngcodes is empty")) 88 c.JSON(http.StatusBadRequest, gin.H{"error": "lngcodes is empty"}) 89 return 90 } 91 92 var wg sync.WaitGroup 93 iLog.Debug(fmt.Sprintf("Get LngCodes autopopulate: %s, %d, %d", config.GlobalConfiguration.TranslationConfig["autopopulate"].(bool), len(lcdata.Texts), len(lcdata.Lngcodes))) 94 if config.GlobalConfiguration.TranslationConfig["autopopulate"].(bool) && len(lcdata.Texts) > 0 && len(lcdata.Lngcodes) > 0 { 95 wg.Add(1) // Increment the wait group counter 96 go func() { 97 defer wg.Done() // Decrement the wait group counter when the goroutine exits 98 // Your task code goes here 99 f.populatelngcodes(lcdata.Lngcodes, lcdata.Texts, language, LoginName) 100 }() 101 } 102 wg.Add(1) 103 go func() { 104 defer wg.Done() 105 // Your second task code goes here 106 107 query := fmt.Sprintf("SELECT lngcode, text FROM language_codes WHERE lngcode IN ('%s') AND language = '%s'", strings.Join(lcdata.Lngcodes, "','"), language) 108 //query := fmt.Sprintf("SELECT lngcode, text FROM language_codes Where language = '%s'", language) 109 110 iLog.Debug(fmt.Sprintf("Get LngCodes query: %s", query)) 111 idbtx, err := dbconn.DB.Begin() 112 if err != nil { 113 iLog.Error(fmt.Sprintf("Get LngCodes error: %s", err.Error())) 114 return 115 } 116 defer idbtx.Rollback() 117 db := dbconn.NewDBOperation(LoginName, idbtx, logger.Framework) 118 119 result, err := db.Query_Json(query) 120 121 if err != nil { 122 iLog.Error(fmt.Sprintf("Get LngCodes error: %s", err.Error())) 123 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 124 return 125 } 126 iLog.Debug(fmt.Sprintf("Get LngCodes rows: %s", result)) 127 idbtx.Commit() 128 c.JSON(http.StatusOK, gin.H{"data": result}) 129 }() 130 131 wg.Wait() 132 } 133 134 func (f *LCController) UpdateLngCode(c *gin.Context) { 135 iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "LngCodes"} 136 startTime := time.Now() 137 defer func() { 138 elapsed := time.Since(startTime) 139 iLog.PerformanceWithDuration("controllers.lngcodes.UpdateLngCode", elapsed) 140 }() 141 142 defer func() { 143 if err := recover(); err != nil { 144 iLog.Error(fmt.Sprintf("UpdateLngCode error: %s", err)) 145 c.JSON(http.StatusBadRequest, gin.H{"error": err}) 146 } 147 }() 148 149 _, LoginName, _, err := auth.GetUserInformation(c) 150 151 if err != nil { 152 iLog.Error(fmt.Sprintf("Update LngCode error: %s", err.Error())) 153 } 154 if LoginName == "" { 155 LoginName = "System" 156 } 157 iLog.User = LoginName 158 159 iLog.Debug(fmt.Sprintf("Update LngCode")) 160 body, err := common.GetRequestBody(c) 161 162 if err != nil { 163 iLog.Error(fmt.Sprintf("Insert LngCode error: %s", err.Error())) 164 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 165 return 166 } 167 168 var lcdata LCData 169 err = json.Unmarshal(body, &lcdata) 170 if err != nil { 171 iLog.Error(fmt.Sprintf("Insert LngCode get the message body error: %s", err.Error())) 172 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 173 return 174 } 175 176 iLog.Debug(fmt.Sprintf("update LngCode lcdata: %s, %s", body, lcdata)) 177 idbtx, err := dbconn.DB.Begin() 178 if err != nil { 179 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", err.Error())) 180 return 181 } 182 defer idbtx.Rollback() 183 db := dbconn.NewDBOperation(LoginName, idbtx, logger.Framework) 184 for index, id := range lcdata.IDs { 185 if id == 0 { 186 err = f.insertlngcode(db, lcdata.Lngcodes[index], lcdata.Texts[index], lcdata.Languages[index], LoginName) 187 if err != nil { 188 iLog.Error(fmt.Sprintf("Insert LngCode error: %s", err.Error())) 189 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 190 return 191 } 192 } else { 193 err = f.updatelngcodbyid(db, id, lcdata.Lngcodes[index], lcdata.Texts[index], lcdata.Languages[index], LoginName) 194 if err != nil { 195 iLog.Error(fmt.Sprintf("Insert LngCode error: %s", err.Error())) 196 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 197 return 198 } 199 } 200 } 201 idbtx.Commit() 202 c.JSON(http.StatusOK, gin.H{"data": "success"}) 203 204 } 205 func (f *LCController) insertlngcode(db *dbconn.DBOperation, lngcode string, text string, language string, User string) error { 206 iLog := logger.Log{ModuleName: logger.API, User: User, ControllerName: "LngCodes"} 207 startTime := time.Now() 208 defer func() { 209 elapsed := time.Since(startTime) 210 iLog.PerformanceWithDuration("controllers.lngcodes.insertlngcode", elapsed) 211 }() 212 213 defer func() { 214 if err := recover(); err != nil { 215 iLog.Error(fmt.Sprintf("insertlngcode error: %s", err)) 216 } 217 }() 218 currentTimeUTC := time.Now().UTC() 219 220 // Format the time as a string in the MySQL date and time format 221 formattedTime := currentTimeUTC.Format("2006-01-02 15:04:05") 222 Columns := make([]string, 7) 223 Values := make([]string, 7) 224 225 n := 0 226 Columns[n] = "text" 227 Values[n] = text 228 229 n += 1 230 Columns[n] = "lngcode" 231 Values[n] = lngcode 232 233 n += 1 234 Columns[n] = "language" 235 Values[n] = language 236 237 n += 1 238 Columns[n] = "UpdatedOn" 239 Values[n] = formattedTime 240 241 n += 1 242 Columns[n] = "UpdatedBy" 243 Values[n] = User 244 245 n += 1 246 Columns[n] = "CreatedOn" 247 Values[n] = formattedTime 248 249 n += 1 250 Columns[n] = "CreatedBy" 251 Values[n] = User 252 253 iLog.Debug(fmt.Sprintf("insertlngcode: %s , %s", Columns, Values)) 254 _, err := db.TableInsert("language_codes", Columns, Values) 255 if err != nil { 256 iLog.Error(fmt.Sprintf("inert a new lngcode record error: %s", err.Error())) 257 return err 258 } 259 return nil 260 } 261 262 func (f *LCController) updatelngcodbyid(db *dbconn.DBOperation, id int, lngcode string, text string, language string, User string) error { 263 iLog := logger.Log{ModuleName: logger.API, User: User, ControllerName: "LngCodes"} 264 startTime := time.Now() 265 defer func() { 266 elapsed := time.Since(startTime) 267 iLog.PerformanceWithDuration("controllers.lngcodes.updatelngcodbyid", elapsed) 268 }() 269 270 defer func() { 271 if err := recover(); err != nil { 272 iLog.Error(fmt.Sprintf("insertlngcode error: %s", err)) 273 } 274 }() 275 276 currentTimeUTC := time.Now().UTC() 277 278 // Format the time as a string in the MySQL date and time format 279 formattedTime := currentTimeUTC.Format("2006-01-02 15:04:05") 280 Columns := make([]string, 5) 281 Values := make([]string, 5) 282 datatypes := make([]int, 5) 283 Where := "" 284 n := 0 285 Columns[n] = "text" 286 Values[n] = text 287 datatypes[n] = 1 288 n += 1 289 Columns[n] = "lngcode" 290 Values[n] = lngcode 291 datatypes[n] = 1 292 n += 1 293 Columns[n] = "language" 294 Values[n] = language 295 datatypes[n] = 1 296 n += 1 297 Columns[n] = "UpdatedOn" 298 Values[n] = formattedTime 299 datatypes[n] = 2 300 n += 1 301 Columns[n] = "UpdatedBy" 302 Values[n] = User 303 datatypes[n] = 1 304 305 Where = fmt.Sprintf("ID = '%d' ", id) 306 307 _, err := db.TableUpdate("language_codes", Columns, Values, datatypes, Where) 308 if err != nil { 309 iLog.Error(fmt.Sprintf("update the lngcode error: %s", err.Error())) 310 return err 311 } 312 return nil 313 } 314 315 func (f *LCController) populatesinglelngcodes(db *dbconn.DBOperation, lngcode string, text string, language string, User string) { 316 iLog := logger.Log{ModuleName: logger.API, User: User, ControllerName: "LngCodes"} 317 startTime := time.Now() 318 defer func() { 319 elapsed := time.Since(startTime) 320 iLog.PerformanceWithDuration("controllers.lngcodes.populatesinglelngcodes", elapsed) 321 }() 322 323 defer func() { 324 if err := recover(); err != nil { 325 iLog.Error(fmt.Sprintf("populatesinglelngcodes error: %s", err)) 326 } 327 }() 328 iLog.Debug(fmt.Sprintf("populatesinglelngcodes")) 329 330 if lngcode == "" { 331 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "lngcodes is empty")) 332 return 333 } 334 335 if language == "" { 336 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "language is empty")) 337 return 338 } 339 340 query := fmt.Sprintf("SELECT lngcode, text FROM language_codes WHERE lngcode = '%s' AND language = '%s'", lngcode, language) 341 iLog.Debug(fmt.Sprintf("populatesinglelngcodes query: %s", query)) 342 343 result, err := db.Query_Json(query) 344 if err != nil { 345 iLog.Error(fmt.Sprintf("populatesinglelngcodes error: %s", err.Error())) 346 return 347 } 348 iLog.Debug(fmt.Sprintf("populatesinglelngcodes rows: %s", result)) 349 350 var existlngcode string 351 var existtext string 352 353 update := false 354 insert := true 355 if result != nil { 356 357 if len(result) > 0 { 358 insert = false 359 existlngcode = result[0]["lngcode"].(string) 360 existtext = result[0]["text"].(string) 361 if existlngcode == lngcode && existtext == text { 362 update = false 363 } else { 364 update = true 365 } 366 } 367 } else { 368 insert = true 369 } 370 371 iLog.Debug(fmt.Sprintf("populatesinglelngcodes update: %s insert: %s", update, insert)) 372 currentTimeUTC := time.Now().UTC() 373 374 // Format the time as a string in the MySQL date and time format 375 formattedTime := currentTimeUTC.Format("2006-01-02 15:04:05") 376 if update { 377 return 378 /* Columns := make([]string, 3) 379 Values := make([]string, 3) 380 datatypes := make([]int, 3) 381 Where := "" 382 n := 0 383 Columns[n] = "text" 384 Values[n] = text 385 datatypes[n] = 1 386 387 n += 1 388 Columns[n] = "UpdatedOn" 389 Values[n] = formattedTime 390 datatypes[n] = 2 391 n += 1 392 Columns[n] = "UpdatedBy" 393 Values[n] = User 394 datatypes[n] = 1 395 396 Where = fmt.Sprintf("lngcode = '%s' ANG language ='%s'", lngcode, language) 397 398 _, err := db.TableUpdate("language_codes", Columns, Values, datatypes, Where) 399 if err != nil { 400 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", err.Error())) 401 return 402 } */ 403 } else if insert { 404 405 Columns := make([]string, 5) 406 Values := make([]string, 5) 407 datatypes := make([]int, 5) 408 n := 0 409 Columns[n] = "lngcode" 410 Values[n] = lngcode 411 datatypes[n] = 1 412 n += 1 413 Columns[n] = "text" 414 Values[n] = text 415 datatypes[n] = 1 416 /* n += 1 417 Columns[n] = "short" 418 Values[n] = short 419 datatypes[n] = 1 */ 420 n += 1 421 Columns[n] = "language" 422 Values[n] = language 423 datatypes[n] = 1 424 n += 1 425 Columns[n] = "CreatedOn" 426 Values[n] = formattedTime 427 datatypes[n] = 2 428 n += 1 429 Columns[n] = "CreatedBy" 430 Values[n] = User 431 datatypes[n] = 1 432 n += 1 433 434 _, err := db.TableInsert("language_codes", Columns, Values) 435 if err != nil { 436 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", err.Error())) 437 return 438 } 439 440 } 441 442 } 443 444 func (f *LCController) populatelngcodes(lngcodes []string, text []string, language string, User string) { 445 iLog := logger.Log{ModuleName: logger.API, User: User, ControllerName: "LngCodes"} 446 startTime := time.Now() 447 defer func() { 448 elapsed := time.Since(startTime) 449 iLog.PerformanceWithDuration("controllers.lngcodes.populatelngcodes", elapsed) 450 }() 451 452 defer func() { 453 if err := recover(); err != nil { 454 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", err)) 455 } 456 }() 457 iLog.Debug(fmt.Sprintf("populatelngcodes")) 458 459 if len(lngcodes) == 0 { 460 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "lngcodes is empty")) 461 return 462 } 463 464 if len(text) == 0 { 465 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "text is empty")) 466 return 467 } 468 /* 469 if len(short) == 0 { 470 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "short is empty")) 471 return 472 } */ 473 474 if language == "" { 475 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", "language is empty")) 476 return 477 } 478 479 idbtx, err := dbconn.DB.Begin() 480 if err != nil { 481 iLog.Error(fmt.Sprintf("populatelngcodes error: %s", err.Error())) 482 return 483 } 484 defer idbtx.Rollback() 485 db := dbconn.NewDBOperation(User, idbtx, logger.Framework) 486 for i := 0; i < len(lngcodes); i++ { 487 iLog.Debug(fmt.Sprintf("populatelngcodes lngcode: %s %s %s %s", lngcodes[i], text[i], language, User)) 488 f.populatesinglelngcodes(db, lngcodes[i], text[i], language, User) 489 490 } 491 idbtx.Commit() 492 }