dashboards.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package models
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "time"
  7. )
  8. // Typed errors
  9. var (
  10. ErrDashboardNotFound = errors.New("Account not found")
  11. ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
  12. )
  13. type Dashboard struct {
  14. Id int64
  15. Slug string `xorm:"index(IX_AccountIdSlug)"`
  16. AccountId int64 `xorm:"index(IX_AccountIdSlug)"`
  17. Created time.Time
  18. Updated time.Time
  19. Title string
  20. Data map[string]interface{}
  21. }
  22. func NewDashboard(title string) *Dashboard {
  23. dash := &Dashboard{}
  24. dash.Data = make(map[string]interface{})
  25. dash.Data["title"] = title
  26. dash.Title = title
  27. dash.UpdateSlug()
  28. return dash
  29. }
  30. func (dash *Dashboard) GetTags() []string {
  31. jsonTags := dash.Data["tags"]
  32. if jsonTags == nil {
  33. return []string{}
  34. }
  35. arr := jsonTags.([]interface{})
  36. b := make([]string, len(arr))
  37. for i := range arr {
  38. b[i] = arr[i].(string)
  39. }
  40. return b
  41. }
  42. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  43. dash := &Dashboard{}
  44. dash.Data = cmd.Dashboard
  45. dash.Title = dash.Data["title"].(string)
  46. dash.AccountId = cmd.AccountId
  47. dash.UpdateSlug()
  48. if dash.Data["id"] != nil {
  49. dash.Id = int64(dash.Data["id"].(float64))
  50. }
  51. return dash
  52. }
  53. func (dash *Dashboard) GetString(prop string) string {
  54. return dash.Data[prop].(string)
  55. }
  56. func (dash *Dashboard) UpdateSlug() {
  57. title := strings.ToLower(dash.Data["title"].(string))
  58. re := regexp.MustCompile("[^\\w ]+")
  59. re2 := regexp.MustCompile("\\s")
  60. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  61. }
  62. //
  63. // COMMANDS
  64. //
  65. type SaveDashboardCommand struct {
  66. Dashboard map[string]interface{} `json:"dashboard"`
  67. AccountId int64 `json:"-"`
  68. Result *Dashboard
  69. }
  70. type DeleteDashboardCommand struct {
  71. Slug string
  72. AccountId int64
  73. }
  74. //
  75. // QUERIES
  76. //
  77. type GetDashboardQuery struct {
  78. Slug string
  79. AccountId int64
  80. Result *Dashboard
  81. }