module_shop_currencies_act_modify.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
  67. } else {
  68. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  69. // Block rows
  70. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", pf_id); err != nil {
  71. return err
  72. }
  73. // Update row
  74. if _, err := tx.Exec(
  75. `UPDATE shop_currencies SET
  76. name = ?,
  77. coefficient = ?,
  78. code = ?,
  79. symbol = ?
  80. WHERE
  81. id = ?
  82. ;`,
  83. pf_name,
  84. pf_coefficient,
  85. pf_code,
  86. pf_symbol,
  87. utils.StrToInt(pf_id),
  88. ); err != nil {
  89. return err
  90. }
  91. return nil
  92. }); err != nil {
  93. wrap.MsgError(err.Error())
  94. return
  95. }
  96. wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
  97. }
  98. })
  99. }