module_blog_act_delete.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. func (this *Modules) RegisterAction_BlogDelete() *Action {
  8. return this.newAction(AInfo{
  9. Mount: "blog-delete",
  10. WantAdmin: true,
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_id := utils.Trim(wrap.R.FormValue("id"))
  13. if !utils.IsNumeric(pf_id) {
  14. wrap.MsgError(`Inner system error`)
  15. return
  16. }
  17. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  18. // Block rows
  19. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_posts WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec(ctx, "SELECT post_id FROM fave_blog_cat_post_rel WHERE post_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  23. return err
  24. }
  25. // Delete target post with category connection data
  26. if _, err := tx.Exec(ctx, "DELETE FROM fave_blog_cat_post_rel WHERE post_id = ?;", utils.StrToInt(pf_id)); err != nil {
  27. return err
  28. }
  29. if _, err := tx.Exec(ctx, "DELETE FROM fave_blog_posts WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  30. return err
  31. }
  32. return nil
  33. }); err != nil {
  34. wrap.MsgError(err.Error())
  35. return
  36. }
  37. wrap.ResetCacheBlocks()
  38. // Reload current page
  39. wrap.Write(`window.location.reload(false);`)
  40. })
  41. }