Browse Source

Always redirect to modify page after add/modify action

Vova Tkach 5 years ago
parent
commit
9597e27942

+ 3 - 4
modules/module_blog_act_modify.go

@@ -45,6 +45,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 		}
 
 		if pf_id == "0" {
+			var lastID int64 = 0
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
@@ -70,7 +71,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 				}
 
 				// Get inserted post id
-				lastID, err := res.LastInsertId()
+				lastID, err = res.LastInsertId()
 				if err != nil {
 					return err
 				}
@@ -116,8 +117,7 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
-			wrap.Write(`window.location='/cp/blog/';`)
+			wrap.Write(`window.location='/cp/blog/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Block rows
@@ -190,7 +190,6 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
 			wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
 		}
 	})

+ 13 - 5
modules/module_blog_categories_act_modify.go

@@ -7,7 +7,8 @@ import (
 	"golang-fave/utils"
 )
 
-func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
+func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) (error, int64) {
+	var lastID int64 = 0
 	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
@@ -27,11 +28,16 @@ func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_nam
 		if _, err := tx.Exec("UPDATE blog_cats SET rgt = rgt + 2 WHERE id = ?;", pf_parent); err != nil {
 			return err
 		}
-		if _, err := tx.Exec("INSERT INTO blog_cats (id, user, name, alias, lft, rgt) VALUES (NULL, ?, ?, ?, @mr, @mr + 1);", wrap.User.A_id, pf_name, pf_alias); err != nil {
+		res, err := tx.Exec("INSERT INTO blog_cats (id, user, name, alias, lft, rgt) VALUES (NULL, ?, ?, ?, @mr, @mr + 1);", wrap.User.A_id, pf_name, pf_alias)
+		if err != nil {
+			return err
+		}
+		lastID, err = res.LastInsertId()
+		if err != nil {
 			return err
 		}
 		return nil
-	})
+	}), lastID
 }
 
 func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
@@ -211,11 +217,13 @@ func (this *Modules) RegisterAction_BlogCategoriesModify() *Action {
 		}
 
 		if pf_id == "0" {
-			if err := this.blog_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
+			var err error = nil
+			var lastID int64 = 0
+			if err, lastID = this.blog_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
 				wrap.MsgError(err.Error())
 				return
 			}
-			wrap.Write(`window.location='/cp/blog/categories/';`)
+			wrap.Write(`window.location='/cp/blog/categories-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := this.blog_ActionCategoryUpdate(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
 				wrap.MsgError(err.Error())

+ 62 - 46
modules/module_index_act_modify.go

@@ -45,57 +45,73 @@ func (this *Modules) RegisterAction_IndexModify() *Action {
 
 		if pf_id == "0" {
 			// Add new page
-			_, err := wrap.DB.Exec(
-				`INSERT INTO pages SET
-					user = ?,
-					name = ?,
-					alias = ?,
-					content = ?,
-					meta_title = ?,
-					meta_keywords = ?,
-					meta_description = ?,
-					datetime = ?,
-					active = ?
-				;`,
-				wrap.User.A_id,
-				pf_name,
-				pf_alias,
-				pf_content,
-				pf_meta_title,
-				pf_meta_keywords,
-				pf_meta_description,
-				utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
-				pf_active,
-			)
-			if err != nil {
+			var lastID int64 = 0
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				res, err := tx.Exec(
+					`INSERT INTO pages SET
+						user = ?,
+						name = ?,
+						alias = ?,
+						content = ?,
+						meta_title = ?,
+						meta_keywords = ?,
+						meta_description = ?,
+						datetime = ?,
+						active = ?
+					;`,
+					wrap.User.A_id,
+					pf_name,
+					pf_alias,
+					pf_content,
+					pf_meta_title,
+					pf_meta_keywords,
+					pf_meta_description,
+					utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
+					pf_active,
+				)
+				if err != nil {
+					return err
+				}
+				// Get inserted post id
+				lastID, err = res.LastInsertId()
+				if err != nil {
+					return err
+				}
+				return nil
+			}); err != nil {
 				wrap.MsgError(err.Error())
 				return
 			}
-			wrap.Write(`window.location='/cp/';`)
+			wrap.Write(`window.location='/cp/index/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			// Update page
-			_, err := wrap.DB.Exec(
-				`UPDATE pages SET
-					name = ?,
-					alias = ?,
-					content = ?,
-					meta_title = ?,
-					meta_keywords = ?,
-					meta_description = ?,
-					active = ?
-				WHERE
-					id = ?
-				;`,
-				pf_name,
-				pf_alias,
-				pf_content,
-				pf_meta_title,
-				pf_meta_keywords,
-				pf_meta_description,
-				pf_active,
-				utils.StrToInt(pf_id),
-			)
-			if err != nil {
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				_, err := tx.Exec(
+					`UPDATE pages SET
+						name = ?,
+						alias = ?,
+						content = ?,
+						meta_title = ?,
+						meta_keywords = ?,
+						meta_description = ?,
+						active = ?
+					WHERE
+						id = ?
+					;`,
+					pf_name,
+					pf_alias,
+					pf_content,
+					pf_meta_title,
+					pf_meta_keywords,
+					pf_meta_description,
+					pf_active,
+					utils.StrToInt(pf_id),
+				)
+				if err != nil {
+					return err
+				}
+				return nil
+			}); err != nil {
 				wrap.MsgError(err.Error())
 				return
 			}

+ 3 - 4
modules/module_shop_act_modify.go

@@ -69,6 +69,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 		}
 
 		if pf_id == "0" {
+			var lastID int64 = 0
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
@@ -98,7 +99,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 				}
 
 				// Get inserted product id
-				lastID, err := res.LastInsertId()
+				lastID, err = res.LastInsertId()
 				if err != nil {
 					return err
 				}
@@ -158,8 +159,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
-			wrap.Write(`window.location='/cp/shop/';`)
+			wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Block rows
@@ -264,7 +264,6 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
 			wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
 		}
 	})

+ 3 - 4
modules/module_shop_attributes_act_modify.go

@@ -45,6 +45,7 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 		}
 
 		if pf_id == "0" {
+			var lastID int64 = 0
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Insert row
 				res, err := tx.Exec(
@@ -60,7 +61,7 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 				}
 
 				// Get inserted id
-				lastID, err := res.LastInsertId()
+				lastID, err = res.LastInsertId()
 				if err != nil {
 					return err
 				}
@@ -88,8 +89,7 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
-			wrap.Write(`window.location='/cp/shop/attributes/';`)
+			wrap.Write(`window.location='/cp/shop/attributes-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Block rows
@@ -194,7 +194,6 @@ func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
 			wrap.Write(`window.location='/cp/shop/attributes-modify/` + pf_id + `/';`)
 		}
 	})

+ 13 - 5
modules/module_shop_categories_act_modify.go

@@ -7,7 +7,8 @@ import (
 	"golang-fave/utils"
 )
 
-func (this *Modules) shop_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
+func (this *Modules) shop_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) (error, int64) {
+	var lastID int64 = 0
 	return wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 		// Block rows
 		if _, err := tx.Exec("SELECT id FROM shop_cats FOR UPDATE;"); err != nil {
@@ -27,11 +28,16 @@ func (this *Modules) shop_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_nam
 		if _, err := tx.Exec("UPDATE shop_cats SET rgt = rgt + 2 WHERE id = ?;", pf_parent); err != nil {
 			return err
 		}
-		if _, err := tx.Exec("INSERT INTO shop_cats (id, user, name, alias, lft, rgt) VALUES (NULL, ?, ?, ?, @mr, @mr + 1);", wrap.User.A_id, pf_name, pf_alias); err != nil {
+		res, err := tx.Exec("INSERT INTO shop_cats (id, user, name, alias, lft, rgt) VALUES (NULL, ?, ?, ?, @mr, @mr + 1);", wrap.User.A_id, pf_name, pf_alias)
+		if err != nil {
+			return err
+		}
+		lastID, err = res.LastInsertId()
+		if err != nil {
 			return err
 		}
 		return nil
-	})
+	}), lastID
 }
 
 func (this *Modules) shop_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
@@ -211,11 +217,13 @@ func (this *Modules) RegisterAction_ShopCategoriesModify() *Action {
 		}
 
 		if pf_id == "0" {
-			if err := this.shop_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
+			var err error = nil
+			var lastID int64 = 0
+			if err, lastID = this.shop_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
 				wrap.MsgError(err.Error())
 				return
 			}
-			wrap.Write(`window.location='/cp/shop/categories/';`)
+			wrap.Write(`window.location='/cp/shop/categories-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := this.shop_ActionCategoryUpdate(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
 				wrap.MsgError(err.Error())

+ 10 - 4
modules/module_shop_currencies_act_modify.go

@@ -43,9 +43,10 @@ func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
 		}
 
 		if pf_id == "0" {
+			var lastID int64 = 0
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Insert row
-				_, err := tx.Exec(
+				res, err := tx.Exec(
 					`INSERT INTO shop_currencies SET
 						name = ?,
 						coefficient = ?,
@@ -60,13 +61,19 @@ func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
 				if err != nil {
 					return err
 				}
+
+				// Get inserted id
+				lastID, err = res.LastInsertId()
+				if err != nil {
+					return err
+				}
+
 				return nil
 			}); err != nil {
 				wrap.MsgError(err.Error())
 				return
 			}
-
-			wrap.Write(`window.location='/cp/shop/currencies/';`)
+			wrap.Write(`window.location='/cp/shop/currencies-modify/` + utils.Int64ToStr(lastID) + `/';`)
 		} else {
 			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
 				// Block rows
@@ -97,7 +104,6 @@ func (this *Modules) RegisterAction_ShopCurrenciesModify() *Action {
 				wrap.MsgError(err.Error())
 				return
 			}
-
 			wrap.Write(`window.location='/cp/shop/currencies-modify/` + pf_id + `/';`)
 		}
 	})

+ 67 - 45
modules/module_users_act_modify.go

@@ -55,68 +55,90 @@ func (this *Modules) RegisterAction_UsersModify() *Action {
 				wrap.MsgError(`Please specify user password`)
 				return
 			}
-			_, err := wrap.DB.Exec(
-				`INSERT INTO users SET
-					first_name = ?,
-					last_name = ?,
-					email = ?,
-					password = MD5(?),
-					admin = ?,
-					active = ?
-				;`,
-				pf_first_name,
-				pf_last_name,
-				pf_email,
-				pf_password,
-				pf_admin,
-				pf_active,
-			)
-			if err != nil {
-				wrap.MsgError(err.Error())
-				return
-			}
-			wrap.Write(`window.location='/cp/users/';`)
-		} else {
-			// Update user
-			if pf_password == "" {
-				_, err := wrap.DB.Exec(
-					`UPDATE users SET
+
+			var lastID int64 = 0
+			if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+				res, err := tx.Exec(
+					`INSERT INTO users SET
 						first_name = ?,
 						last_name = ?,
 						email = ?,
+						password = MD5(?),
 						admin = ?,
 						active = ?
-					WHERE
-						id = ?
 					;`,
 					pf_first_name,
 					pf_last_name,
 					pf_email,
+					pf_password,
 					pf_admin,
 					pf_active,
-					utils.StrToInt(pf_id),
 				)
 				if err != nil {
+					return err
+				}
+				// Get inserted post id
+				lastID, err = res.LastInsertId()
+				if err != nil {
+					return err
+				}
+				return nil
+			}); err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+			wrap.Write(`window.location='/cp/users/modify/` + utils.Int64ToStr(lastID) + `/';`)
+		} else {
+			// Update user
+			if pf_password == "" {
+				if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+					_, err := tx.Exec(
+						`UPDATE users SET
+							first_name = ?,
+							last_name = ?,
+							email = ?,
+							admin = ?,
+							active = ?
+						WHERE
+							id = ?
+						;`,
+						pf_first_name,
+						pf_last_name,
+						pf_email,
+						pf_admin,
+						pf_active,
+						utils.StrToInt(pf_id),
+					)
+					if err != nil {
+						return err
+					}
+					return nil
+				}); err != nil {
 					wrap.MsgError(err.Error())
 					return
 				}
 			} else {
-				_, err := wrap.DB.Exec(
-					`UPDATE users SET
-						first_name = ?,
-						last_name = ?,
-						email = ?,
-						password = MD5(?)
-					WHERE
-						id = ?
-					;`,
-					pf_first_name,
-					pf_last_name,
-					pf_email,
-					pf_password,
-					utils.StrToInt(pf_id),
-				)
-				if err != nil {
+				if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
+					_, err := tx.Exec(
+						`UPDATE users SET
+							first_name = ?,
+							last_name = ?,
+							email = ?,
+							password = MD5(?)
+						WHERE
+							id = ?
+						;`,
+						pf_first_name,
+						pf_last_name,
+						pf_email,
+						pf_password,
+						utils.StrToInt(pf_id),
+					)
+					if err != nil {
+						return err
+					}
+					return nil
+				}); err != nil {
 					wrap.MsgError(err.Error())
 					return
 				}