module_shop_act_delete.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package modules
  2. import (
  3. "golang-fave/engine/wrapper"
  4. "golang-fave/utils"
  5. )
  6. func (this *Modules) RegisterAction_ShopDelete() *Action {
  7. return this.newAction(AInfo{
  8. WantDB: true,
  9. Mount: "shop-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. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  18. // Block rows
  19. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", pf_id); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec("SELECT id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", pf_id); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec("SELECT id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", pf_id); err != nil {
  26. return err
  27. }
  28. // Delete target post with category connection data
  29. if _, err := tx.Exec("DELETE FROM shop_filter_product_values WHERE product_id = ?;", pf_id); err != nil {
  30. return err
  31. }
  32. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", pf_id); err != nil {
  33. return err
  34. }
  35. if _, err := tx.Exec("DELETE FROM shop_products WHERE id = ?;", pf_id); err != nil {
  36. return err
  37. }
  38. return nil
  39. }); err != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. // Reload current page
  44. wrap.Write(`window.location.reload(false);`)
  45. })
  46. }