module_shop_currencies_act_modify.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Mount: "shop-currencies-modify",
  10. WantAdmin: true,
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_id := utils.Trim(wrap.R.FormValue("id"))
  13. pf_name := utils.Trim(wrap.R.FormValue("name"))
  14. pf_coefficient := utils.Trim(wrap.R.FormValue("coefficient"))
  15. pf_code := utils.Trim(wrap.R.FormValue("code"))
  16. pf_symbol := utils.Trim(wrap.R.FormValue("symbol"))
  17. if !utils.IsNumeric(pf_id) {
  18. wrap.MsgError(`Inner system error`)
  19. return
  20. }
  21. if pf_name == "" {
  22. wrap.MsgError(`Please specify currency name`)
  23. return
  24. }
  25. if !utils.IsFloat(pf_coefficient) {
  26. wrap.MsgError(`Inner system error`)
  27. return
  28. }
  29. if pf_code == "" {
  30. wrap.MsgError(`Please specify currency code`)
  31. return
  32. }
  33. if pf_symbol == "" {
  34. wrap.MsgError(`Please specify currency symbol`)
  35. return
  36. }
  37. if pf_id == "0" {
  38. var lastID int64 = 0
  39. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  40. // Insert row
  41. res, err := tx.Exec(
  42. ctx,
  43. `INSERT INTO fave_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(ctx, "SELECT id FROM fave_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. ctx,
  79. `UPDATE fave_shop_currencies SET
  80. name = ?,
  81. coefficient = ?,
  82. code = ?,
  83. symbol = ?
  84. WHERE
  85. id = ?
  86. ;`,
  87. pf_name,
  88. pf_coefficient,
  89. pf_code,
  90. pf_symbol,
  91. utils.StrToInt(pf_id),
  92. ); err != nil {
  93. return err
  94. }
  95. return nil
  96. }); err != nil {
  97. wrap.MsgError(err.Error())
  98. return
  99. }
  100. wrap.RecreateProductXmlFile()
  101. wrap.ResetCacheBlocks()
  102. wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
  103. }
  104. })
  105. }