module_users_act_delete.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. WantDB: true,
  10. Mount: "users-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. 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_cats WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  21. return err
  22. }
  23. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_posts WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  24. return err
  25. }
  26. if _, err := tx.Exec(ctx, "SELECT id FROM fave_pages WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  27. return err
  28. }
  29. if _, err := tx.Exec(ctx, "SELECT id FROM fave_users WHERE id = ? and id > 1 FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  30. return err
  31. }
  32. // Process
  33. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  34. return err
  35. }
  36. if _, err := tx.Exec(ctx, "UPDATE fave_blog_posts SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  37. return err
  38. }
  39. if _, err := tx.Exec(ctx, "UPDATE fave_pages SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  40. return err
  41. }
  42. if _, err := tx.Exec(ctx, "DELETE FROM fave_users WHERE id = ? and id > 1;", utils.StrToInt(pf_id)); err != nil {
  43. return err
  44. }
  45. return nil
  46. })
  47. if err != nil {
  48. wrap.MsgError(err.Error())
  49. return
  50. }
  51. wrap.ResetCacheBlocks()
  52. // Reload current page
  53. wrap.Write(`window.location.reload(false);`)
  54. })
  55. }