module_shop_act_delete.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. WantDB: true,
  10. Mount: "shop-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. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  19. // Block rows
  20. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  21. return err
  22. }
  23. 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 {
  24. return err
  25. }
  26. 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 {
  27. return err
  28. }
  29. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_product_images WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  30. return err
  31. }
  32. // Delete product attached images
  33. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_product_images WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  34. return err
  35. }
  36. // Delete target product with category connection data
  37. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_filter_product_values WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  38. return err
  39. }
  40. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  41. return err
  42. }
  43. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_products WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  44. return err
  45. }
  46. return nil
  47. }); err != nil {
  48. wrap.MsgError(err.Error())
  49. return
  50. }
  51. wrap.RemoveProductImageThumbnails(pf_id, "*")
  52. wrap.RecreateProductXmlFile()
  53. wrap.ResetCacheBlocks()
  54. // Reload current page
  55. wrap.Write(`window.location.reload(false);`)
  56. })
  57. }