module_shop_attributes_act_delete.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  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("SELECT id FROM shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  21. return err
  22. }
  23. if _, err := tx.Exec("SELECT id FROM shop_filters_values WHERE filter_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  24. return err
  25. }
  26. if _, err := tx.Exec(
  27. `SELECT
  28. shop_filter_product_values.product_id
  29. FROM
  30. shop_filter_product_values
  31. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  32. WHERE
  33. shop_filters_values.id IS NOT NULL AND
  34. 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. `DELETE
  43. shop_filter_product_values
  44. FROM
  45. shop_filter_product_values
  46. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  47. WHERE
  48. shop_filters_values.id IS NOT NULL AND
  49. shop_filters_values.filter_id = ?
  50. ;`,
  51. utils.StrToInt(pf_id),
  52. ); err != nil {
  53. return err
  54. }
  55. if _, err := tx.Exec(
  56. `DELETE FROM shop_filters_values WHERE filter_id = ?;`,
  57. utils.StrToInt(pf_id),
  58. ); err != nil {
  59. return err
  60. }
  61. if _, err := tx.Exec(
  62. `DELETE FROM shop_filters WHERE id = ?;`,
  63. utils.StrToInt(pf_id),
  64. ); err != nil {
  65. return err
  66. }
  67. return nil
  68. })
  69. if err != nil {
  70. wrap.MsgError(err.Error())
  71. return
  72. }
  73. wrap.RecreateProductXmlFile()
  74. wrap.ResetCacheBlocks()
  75. // Reload current page
  76. wrap.Write(`window.location.reload(false);`)
  77. })
  78. }