Browse Source

Shop, product attach searched product action

Vova Tkach 5 years ago
parent
commit
622bdc503c
2 changed files with 76 additions and 2 deletions
  1. 74 0
      modules/module_shop_act_attach_product_to.go
  2. 2 2
      modules/module_shop_act_detach.go

+ 74 - 0
modules/module_shop_act_attach_product_to.go

@@ -0,0 +1,74 @@
+package modules
+
+import (
+	"errors"
+
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+func (this *Modules) RegisterAction_ShopAttachProductTo() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "shop-attach-product-to",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_parent_id := wrap.R.FormValue("parent_id")
+		pf_product_id := wrap.R.FormValue("product_id")
+
+		if !utils.IsNumeric(pf_parent_id) || !utils.IsNumeric(pf_product_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+			// Check parent
+			var count int
+			if err := tx.QueryRow(
+				"SELECT COUNT(*) FROM `shop_products` WHERE `id` = ? AND `parent_id` IS NULL;",
+				utils.StrToInt(pf_parent_id),
+			).Scan(&count); err != nil {
+				return err
+			}
+			if count <= 0 {
+				return errors.New("Parent product can't be used for attaching")
+			}
+
+			// Check child
+			if err := tx.QueryRow(
+				"SELECT COUNT(*) FROM `shop_products` WHERE `parent_id` = ?;",
+				utils.StrToInt(pf_product_id),
+			).Scan(&count); err != nil {
+				return err
+			}
+			if count >= 1 {
+				return errors.New("Parent can't be attached to parent")
+			}
+
+			// Attach
+			if _, err := tx.Exec(`
+				UPDATE shop_products SET
+					parent_id = ?
+				WHERE
+					id = ? AND
+					parent_id IS NULL
+				;`,
+				utils.StrToInt(pf_parent_id),
+				utils.StrToInt(pf_product_id),
+			); err != nil {
+				return err
+			}
+			return nil
+		}); err != nil {
+			wrap.MsgError(err.Error())
+			return
+		}
+
+		wrap.RecreateProductXmlFile()
+
+		wrap.ResetCacheBlocks()
+
+		// Reload current page
+		wrap.Write(`window.location.reload(false);`)
+	})
+}

+ 2 - 2
modules/module_shop_act_detach.go

@@ -24,8 +24,8 @@ func (this *Modules) RegisterAction_ShopDetach() *Action {
 					parent_id = NULL,
 					active = 0
 				WHERE
-					id = ?;
-				`,
+					id = ?
+				;`,
 				utils.StrToInt(pf_id),
 			); err != nil {
 				return err