actions.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package actions
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "golang-fave/engine/wrapper"
  10. utils "golang-fave/engine/wrapper/utils"
  11. )
  12. type Action struct {
  13. wrapper *wrapper.Wrapper
  14. db *sql.DB
  15. user *utils.MySql_user
  16. }
  17. func (this *Action) write(data string) {
  18. (*this.wrapper.W).Write([]byte(data))
  19. }
  20. func (this *Action) msg_show(title string, msg string) {
  21. this.write(fmt.Sprintf(
  22. `ModalShowMsg('%s', '%s');`,
  23. strings.Replace(strings.Replace(title, `'`, `’`, -1), `"`, `”`, -1),
  24. strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
  25. }
  26. func (this *Action) msg_success(msg string) {
  27. this.msg_show("Success", msg)
  28. }
  29. func (this *Action) msg_error(msg string) {
  30. this.msg_show("Error", msg)
  31. }
  32. func (this *Action) use_database() error {
  33. if this.db != nil {
  34. return errors.New("already connected to database")
  35. }
  36. if !utils.IsMySqlConfigExists(this.wrapper.DirVHostHome) {
  37. return errors.New("can't read database configuration file")
  38. }
  39. mc, err := utils.MySqlConfigRead(this.wrapper.DirVHostHome)
  40. if err != nil {
  41. return err
  42. }
  43. this.db, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  44. if err != nil {
  45. return err
  46. }
  47. err = this.db.Ping()
  48. if err != nil {
  49. this.db.Close()
  50. return err
  51. }
  52. return nil
  53. }
  54. func (this *Action) load_session_user() error {
  55. if this.db == nil {
  56. return errors.New("not connected to database")
  57. }
  58. if this.user != nil {
  59. return errors.New("user already loaded")
  60. }
  61. if this.wrapper.Session.GetIntDef("UserId", 0) <= 0 {
  62. return errors.New("session user id is not defined")
  63. }
  64. this.user = &utils.MySql_user{}
  65. err := this.db.QueryRow("SELECT `id`, `first_name`, `last_name`, `email`, `password` FROM `users` WHERE `id` = ? LIMIT 1;", this.wrapper.Session.GetIntDef("UserId", 0)).Scan(
  66. &this.user.A_id, &this.user.A_first_name, &this.user.A_last_name, &this.user.A_email, &this.user.A_password)
  67. if err != nil {
  68. return err
  69. }
  70. if this.user.A_id != this.wrapper.Session.GetIntDef("UserId", 0) {
  71. return errors.New("can't load user from session user id")
  72. }
  73. return nil
  74. }
  75. func New(wrapper *wrapper.Wrapper) *Action {
  76. return &Action{wrapper, nil, nil}
  77. }
  78. func (this *Action) Run() bool {
  79. if this.wrapper.R.Method != "POST" {
  80. return false
  81. }
  82. if err := this.wrapper.R.ParseForm(); err == nil {
  83. action := this.wrapper.R.FormValue("action")
  84. if action != "" {
  85. if _, ok := reflect.TypeOf(this).MethodByName("Action_" + action); ok {
  86. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  87. (*this.wrapper.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  88. reflect.ValueOf(this).MethodByName("Action_" + action).Call([]reflect.Value{})
  89. return true
  90. }
  91. }
  92. }
  93. return false
  94. }