module_blog_act_delete.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. WantDB: true,
  10. Mount: "blog-delete",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. if !utils.IsNumeric(pf_id) {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  19. // Block rows
  20. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_posts WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  21. return err
  22. }
  23. 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 {
  24. return err
  25. }
  26. // Delete target post with category connection data
  27. if _, err := tx.Exec(ctx, "DELETE FROM fave_blog_cat_post_rel WHERE post_id = ?;", utils.StrToInt(pf_id)); err != nil {
  28. return err
  29. }
  30. if _, err := tx.Exec(ctx, "DELETE FROM fave_blog_posts WHERE id = ?;", utils.StrToInt(pf_id)); err != nil {
  31. return err
  32. }
  33. return nil
  34. }); err != nil {
  35. wrap.MsgError(err.Error())
  36. return
  37. }
  38. wrap.ResetCacheBlocks()
  39. // Reload current page
  40. wrap.Write(`window.location.reload(false);`)
  41. })
  42. }