module_index_act_update_profile.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package modules
  2. import (
  3. "golang-fave/engine/wrapper"
  4. "golang-fave/utils"
  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. `UPDATE 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. `UPDATE users SET
  49. first_name = ?,
  50. last_name = ?,
  51. email = ?
  52. WHERE
  53. id = ?
  54. ;`,
  55. pf_first_name,
  56. pf_last_name,
  57. pf_email,
  58. wrap.User.A_id,
  59. )
  60. if err != nil {
  61. wrap.MsgError(err.Error())
  62. return
  63. }
  64. }
  65. // Reload current page
  66. wrap.Write(`window.location.reload(false);`)
  67. })
  68. }