module_shop_currencies_act_modify.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "shop-currencies-modify",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. pf_name := utils.Trim(wrap.R.FormValue("name"))
  15. pf_coefficient := utils.Trim(wrap.R.FormValue("coefficient"))
  16. pf_code := utils.Trim(wrap.R.FormValue("code"))
  17. pf_symbol := utils.Trim(wrap.R.FormValue("symbol"))
  18. if !utils.IsNumeric(pf_id) {
  19. wrap.MsgError(`Inner system error`)
  20. return
  21. }
  22. if pf_name == "" {
  23. wrap.MsgError(`Please specify currency name`)
  24. return
  25. }
  26. if !utils.IsFloat(pf_coefficient) {
  27. wrap.MsgError(`Inner system error`)
  28. return
  29. }
  30. if pf_code == "" {
  31. wrap.MsgError(`Please specify currency code`)
  32. return
  33. }
  34. if pf_symbol == "" {
  35. wrap.MsgError(`Please specify currency symbol`)
  36. return
  37. }
  38. if pf_id == "0" {
  39. var lastID int64 = 0
  40. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  41. // Insert row
  42. res, err := tx.Exec(
  43. `INSERT INTO shop_currencies SET
  44. name = ?,
  45. coefficient = ?,
  46. code = ?,
  47. symbol = ?
  48. ;`,
  49. pf_name,
  50. pf_coefficient,
  51. pf_code,
  52. pf_symbol,
  53. )
  54. if err != nil {
  55. return err
  56. }
  57. // Get inserted id
  58. lastID, err = res.LastInsertId()
  59. if err != nil {
  60. return err
  61. }
  62. return nil
  63. }); err != nil {
  64. wrap.MsgError(err.Error())
  65. return
  66. }
  67. wrap.RecreateProductXmlFile()
  68. wrap.ResetCacheBlocks()
  69. wrap.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
  70. } else {
  71. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  72. // Block rows
  73. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  74. return err
  75. }
  76. // Update row
  77. if _, err := tx.Exec(
  78. `UPDATE shop_currencies SET
  79. name = ?,
  80. coefficient = ?,
  81. code = ?,
  82. symbol = ?
  83. WHERE
  84. id = ?
  85. ;`,
  86. pf_name,
  87. pf_coefficient,
  88. pf_code,
  89. pf_symbol,
  90. utils.StrToInt(pf_id),
  91. ); err != nil {
  92. return err
  93. }
  94. return nil
  95. }); err != nil {
  96. wrap.MsgError(err.Error())
  97. return
  98. }
  99. wrap.RecreateProductXmlFile()
  100. wrap.ResetCacheBlocks()
  101. wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
  102. }
  103. })
  104. }