module_index_act_update_profile.go 1.5 KB

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