module_blog_categories_act_delete.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_BlogCategoriesDelete() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "blog-categories-delete",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. if !utils.IsNumeric(pf_id) || utils.StrToInt(pf_id) <= 1 {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  19. // Block rows
  20. if _, err := tx.Exec("SELECT id FROM blog_cats FOR UPDATE;"); err != nil {
  21. return err
  22. }
  23. if _, err := tx.Exec("SELECT category_id FROM blog_cat_post_rel WHERE category_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  24. return err
  25. }
  26. if _, err := tx.Exec("SELECT id FROM blog_posts WHERE category = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  27. return err
  28. }
  29. // Set root category
  30. if _, err := tx.Exec("UPDATE blog_posts SET category = 1 WHERE category = ?;", utils.StrToInt(pf_id)); err != nil {
  31. return err
  32. }
  33. // Process
  34. if _, err := tx.Exec("DELETE FROM blog_cat_post_rel WHERE category_id = ?;", utils.StrToInt(pf_id)); err != nil {
  35. return err
  36. }
  37. if _, err := tx.Exec("SELECT @ml := lft, @mr := rgt FROM blog_cats WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  38. return err
  39. }
  40. if _, err := tx.Exec("DELETE FROM blog_cats WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  41. return err
  42. }
  43. if _, err := tx.Exec("UPDATE blog_cats SET lft = lft - 1, rgt = rgt - 1 WHERE lft > @ml AND rgt < @mr;"); err != nil {
  44. return err
  45. }
  46. if _, err := tx.Exec("UPDATE blog_cats SET lft = lft - 2 WHERE lft > @mr;"); err != nil {
  47. return err
  48. }
  49. if _, err := tx.Exec("UPDATE blog_cats SET rgt = rgt - 2 WHERE rgt > @mr;"); err != nil {
  50. return err
  51. }
  52. return nil
  53. })
  54. if err != nil {
  55. wrap.MsgError(err.Error())
  56. return
  57. }
  58. wrap.ResetCacheBlocks()
  59. // Reload current page
  60. wrap.Write(`window.location.reload(false);`)
  61. })
  62. }