module_shop_attributes_act_delete.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. func (this *Modules) RegisterAction_ShopAttributesDelete() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "shop-attributes-delete",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. if !utils.IsNumeric(pf_id) {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  19. // Block rows
  20. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  21. return err
  22. }
  23. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_filters_values WHERE filter_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  24. return err
  25. }
  26. if _, err := tx.Exec(
  27. ctx,
  28. `SELECT
  29. fave_shop_filter_product_values.product_id
  30. FROM
  31. fave_shop_filter_product_values
  32. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  33. WHERE
  34. fave_shop_filters_values.id IS NOT NULL AND
  35. fave_shop_filters_values.filter_id = ?
  36. FOR UPDATE;`,
  37. utils.StrToInt(pf_id),
  38. ); err != nil {
  39. return err
  40. }
  41. // Process
  42. if _, err := tx.Exec(
  43. ctx,
  44. `DELETE
  45. fave_shop_filter_product_values
  46. FROM
  47. fave_shop_filter_product_values
  48. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  49. WHERE
  50. fave_shop_filters_values.id IS NOT NULL AND
  51. fave_shop_filters_values.filter_id = ?
  52. ;`,
  53. utils.StrToInt(pf_id),
  54. ); err != nil {
  55. return err
  56. }
  57. if _, err := tx.Exec(
  58. ctx,
  59. `DELETE FROM fave_shop_filters_values WHERE filter_id = ?;`,
  60. utils.StrToInt(pf_id),
  61. ); err != nil {
  62. return err
  63. }
  64. if _, err := tx.Exec(
  65. ctx,
  66. `DELETE FROM fave_shop_filters WHERE id = ?;`,
  67. utils.StrToInt(pf_id),
  68. ); err != nil {
  69. return err
  70. }
  71. return nil
  72. })
  73. if err != nil {
  74. wrap.MsgError(err.Error())
  75. return
  76. }
  77. wrap.RecreateProductXmlFile()
  78. wrap.ResetCacheBlocks()
  79. // Reload current page
  80. wrap.Write(`window.location.reload(false);`)
  81. })
  82. }