module_shop_currencies_act_modify.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  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. ctx,
  44. `INSERT INTO fave_shop_currencies SET
  45. name = ?,
  46. coefficient = ?,
  47. code = ?,
  48. symbol = ?
  49. ;`,
  50. pf_name,
  51. pf_coefficient,
  52. pf_code,
  53. pf_symbol,
  54. )
  55. if err != nil {
  56. return err
  57. }
  58. // Get inserted id
  59. lastID, err = res.LastInsertId()
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }); err != nil {
  65. wrap.MsgError(err.Error())
  66. return
  67. }
  68. wrap.RecreateProductXmlFile()
  69. wrap.ResetCacheBlocks()
  70. wrap.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
  71. } else {
  72. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  73. // Block rows
  74. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  75. return err
  76. }
  77. // Update row
  78. if _, err := tx.Exec(
  79. ctx,
  80. `UPDATE fave_shop_currencies SET
  81. name = ?,
  82. coefficient = ?,
  83. code = ?,
  84. symbol = ?
  85. WHERE
  86. id = ?
  87. ;`,
  88. pf_name,
  89. pf_coefficient,
  90. pf_code,
  91. pf_symbol,
  92. utils.StrToInt(pf_id),
  93. ); err != nil {
  94. return err
  95. }
  96. return nil
  97. }); err != nil {
  98. wrap.MsgError(err.Error())
  99. return
  100. }
  101. wrap.RecreateProductXmlFile()
  102. wrap.ResetCacheBlocks()
  103. wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
  104. }
  105. })
  106. }