module_shop_currencies_act_modify.go 2.4 KB

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