module_shop_attributes_act_delete.go 2.0 KB

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