|
@@ -508,8 +508,20 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
|
|
|
}
|
|
|
|
|
|
if pf_id == "0" {
|
|
|
- // Add new post
|
|
|
- _, err := wrap.DB.Exec(
|
|
|
+ // Start transaction with table lock
|
|
|
+ _, err := wrap.DB.Exec("LOCK TABLE blog_cats WRITE, blog_cat_post_rel WRITE;")
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tx, err := wrap.DB.Begin()
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Insert row
|
|
|
+ res, err := tx.Exec(
|
|
|
`INSERT INTO blog_posts SET
|
|
|
user = ?,
|
|
|
name = ?,
|
|
@@ -526,13 +538,84 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
|
|
|
pf_active,
|
|
|
)
|
|
|
if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
wrap.MsgError(err.Error())
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ lastID, err := res.LastInsertId()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Insert post and categories relations
|
|
|
+ catids := utils.GetPostArrayInt("cats[]", wrap.R)
|
|
|
+ if len(catids) > 0 {
|
|
|
+ var catsCount int
|
|
|
+ err = tx.QueryRow(`
|
|
|
+ SELECT
|
|
|
+ COUNT(*)
|
|
|
+ FROM
|
|
|
+ blog_cats
|
|
|
+ WHERE
|
|
|
+ id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
|
|
|
+ ;`,
|
|
|
+ ).Scan(
|
|
|
+ &catsCount,
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(catids) != catsCount {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(`Inner system error`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var balkInsertArr []string
|
|
|
+ for _, el := range catids {
|
|
|
+ balkInsertArr = append(balkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
|
|
|
+ }
|
|
|
+ if _, err = tx.Exec(
|
|
|
+ `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
|
|
|
+ ); err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Commit all changes and unlock table
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ _, err = wrap.DB.Exec("UNLOCK TABLES;")
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
wrap.Write(`window.location='/cp/blog/';`)
|
|
|
} else {
|
|
|
- // Update post
|
|
|
- _, err := wrap.DB.Exec(
|
|
|
+ // Start transaction with table lock
|
|
|
+ _, err := wrap.DB.Exec("LOCK TABLE blog_cats WRITE, blog_cat_post_rel WRITE;")
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tx, err := wrap.DB.Begin()
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update row
|
|
|
+ if _, err = tx.Exec(
|
|
|
`UPDATE blog_posts SET
|
|
|
name = ?,
|
|
|
alias = ?,
|
|
@@ -546,11 +629,69 @@ func (this *Modules) RegisterAction_BlogModify() *Action {
|
|
|
pf_content,
|
|
|
pf_active,
|
|
|
utils.StrToInt(pf_id),
|
|
|
- )
|
|
|
+ ); err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Delete post and categories relations
|
|
|
+ if _, err = tx.Exec("DELETE FROM blog_cat_post_rel WHERE post_id = ?;", pf_id); err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Insert post and categories relations
|
|
|
+ catids := utils.GetPostArrayInt("cats[]", wrap.R)
|
|
|
+ if len(catids) > 0 {
|
|
|
+ var catsCount int
|
|
|
+ err = tx.QueryRow(`
|
|
|
+ SELECT
|
|
|
+ COUNT(*)
|
|
|
+ FROM
|
|
|
+ blog_cats
|
|
|
+ WHERE
|
|
|
+ id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
|
|
|
+ ;`,
|
|
|
+ ).Scan(
|
|
|
+ &catsCount,
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(catids) != catsCount {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(`Inner system error`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var balkInsertArr []string
|
|
|
+ for _, el := range catids {
|
|
|
+ balkInsertArr = append(balkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
|
|
|
+ }
|
|
|
+ if _, err = tx.Exec(
|
|
|
+ `INSERT INTO blog_cat_post_rel (id,post_id,category_id) VALUES ` + strings.Join(balkInsertArr, ",") + `;`,
|
|
|
+ ); err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Commit all changes and unlock table
|
|
|
+ err = tx.Commit()
|
|
|
if err != nil {
|
|
|
wrap.MsgError(err.Error())
|
|
|
return
|
|
|
}
|
|
|
+ _, err = wrap.DB.Exec("UNLOCK TABLES;")
|
|
|
+ if err != nil {
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
wrap.Write(`window.location='/cp/blog/modify/` + pf_id + `/';`)
|
|
|
}
|
|
|
})
|