module_users_act_delete.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. func (this *Modules) RegisterAction_UsersDelete() *Action {
  8. return this.newAction(AInfo{
  9. Mount: "users-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. 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_cats WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_posts WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec(ctx, "SELECT id FROM fave_pages WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec(ctx, "SELECT id FROM fave_users WHERE id = ? and id > 1 FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  29. return err
  30. }
  31. // Process
  32. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  33. return err
  34. }
  35. if _, err := tx.Exec(ctx, "UPDATE fave_blog_posts SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  36. return err
  37. }
  38. if _, err := tx.Exec(ctx, "UPDATE fave_pages SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  39. return err
  40. }
  41. if _, err := tx.Exec(ctx, "DELETE FROM fave_users WHERE id = ? and id > 1;", utils.StrToInt(pf_id)); err != nil {
  42. return err
  43. }
  44. return nil
  45. })
  46. if err != nil {
  47. wrap.MsgError(err.Error())
  48. return
  49. }
  50. wrap.ResetCacheBlocks()
  51. // Reload current page
  52. wrap.Write(`window.location.reload(false);`)
  53. })
  54. }