module_shop_attributes_act_delete.go 2.2 KB

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