module_shop_act_delete.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. func (this *Modules) RegisterAction_ShopDelete() *Action {
  8. return this.newAction(AInfo{
  9. Mount: "shop-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. if 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_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_product_images WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  29. return err
  30. }
  31. // Delete product attached images
  32. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_product_images WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  33. return err
  34. }
  35. // Delete target product with category connection data
  36. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_filter_product_values WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  37. return err
  38. }
  39. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  40. return err
  41. }
  42. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_products WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  43. return err
  44. }
  45. return nil
  46. }); err != nil {
  47. wrap.MsgError(err.Error())
  48. return
  49. }
  50. wrap.RemoveProductImageThumbnails(pf_id, "*")
  51. wrap.RecreateProductXmlFile()
  52. wrap.ResetCacheBlocks()
  53. // Reload current page
  54. wrap.Write(`window.location.reload(false);`)
  55. })
  56. }