module_index_act_update_profile.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package modules
  2. import (
  3. "golang-fave/engine/utils"
  4. "golang-fave/engine/wrapper"
  5. )
  6. func (this *Modules) RegisterAction_IndexUserUpdateProfile() *Action {
  7. return this.newAction(AInfo{
  8. Mount: "index-user-update-profile",
  9. WantUser: true,
  10. }, func(wrap *wrapper.Wrapper) {
  11. pf_first_name := utils.Trim(wrap.R.FormValue("first_name"))
  12. pf_last_name := utils.Trim(wrap.R.FormValue("last_name"))
  13. pf_email := utils.Trim(wrap.R.FormValue("email"))
  14. pf_password := utils.Trim(wrap.R.FormValue("password"))
  15. if pf_email == "" {
  16. wrap.MsgError(`Please specify user email`)
  17. return
  18. }
  19. if !utils.IsValidEmail(pf_email) {
  20. wrap.MsgError(`Please specify correct user email`)
  21. return
  22. }
  23. if pf_password != "" {
  24. // Update with password if set
  25. _, err := wrap.DB.Exec(
  26. wrap.R.Context(),
  27. `UPDATE fave_users SET
  28. first_name = ?,
  29. last_name = ?,
  30. email = ?,
  31. password = MD5(?)
  32. WHERE
  33. id = ?
  34. ;`,
  35. pf_first_name,
  36. pf_last_name,
  37. pf_email,
  38. pf_password,
  39. wrap.User.A_id,
  40. )
  41. if err != nil {
  42. wrap.MsgError(err.Error())
  43. return
  44. }
  45. } else {
  46. // Update without password if not set
  47. _, err := wrap.DB.Exec(
  48. wrap.R.Context(),
  49. `UPDATE fave_users SET
  50. first_name = ?,
  51. last_name = ?,
  52. email = ?
  53. WHERE
  54. id = ?
  55. ;`,
  56. pf_first_name,
  57. pf_last_name,
  58. pf_email,
  59. wrap.User.A_id,
  60. )
  61. if err != nil {
  62. wrap.MsgError(err.Error())
  63. return
  64. }
  65. }
  66. // Reload current page
  67. wrap.Write(`window.location.reload(false);`)
  68. })
  69. }