module_shop_currencies_act_modify.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // Delete products XML cache
  67. wrap.RemoveProductXmlCacheFile()
  68. wrap.ResetCacheBlocks()
  69. wrap.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
  70. } else {
  71. if err := wrap.DB.Transaction(func(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. // Delete products XML cache
  100. wrap.RemoveProductXmlCacheFile()
  101. wrap.ResetCacheBlocks()
  102. wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
  103. }
  104. })
  105. }