dashboards.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package models
  2. import (
  3. "encoding/json"
  4. "io"
  5. "regexp"
  6. "strings"
  7. "time"
  8. )
  9. type Dashboard struct {
  10. Id string `gorethink:"id,omitempty"`
  11. Slug string
  12. AccountId int
  13. LastModifiedByUserId string
  14. LastModifiedByDate time.Time
  15. CreatedDate time.Time
  16. Title string
  17. Tags []string
  18. Data map[string]interface{}
  19. }
  20. type CollaboratorLink struct {
  21. AccountId int
  22. Role string
  23. ModifiedOn time.Time
  24. CreatedOn time.Time
  25. }
  26. type UserAccount struct {
  27. Id int `gorethink:"id"`
  28. UserName string
  29. Login string
  30. Email string
  31. Password string
  32. NextDashboardId int
  33. UsingAccountId int
  34. Collaborators []CollaboratorLink
  35. CreatedOn time.Time
  36. ModifiedOn time.Time
  37. }
  38. type UserContext struct {
  39. UserId string
  40. AccountId string
  41. }
  42. type SearchResult struct {
  43. Id string `json:"id"`
  44. Title string `json:"title"`
  45. Slug string `json:"slug"`
  46. }
  47. func NewDashboard(title string) *Dashboard {
  48. dash := &Dashboard{}
  49. dash.Id = ""
  50. dash.LastModifiedByDate = time.Now()
  51. dash.CreatedDate = time.Now()
  52. dash.LastModifiedByUserId = "123"
  53. dash.Data = make(map[string]interface{})
  54. dash.Data["title"] = title
  55. dash.Title = title
  56. dash.UpdateSlug()
  57. return dash
  58. }
  59. func NewFromJson(reader io.Reader) (*Dashboard, error) {
  60. dash := NewDashboard("temp")
  61. jsonParser := json.NewDecoder(reader)
  62. if err := jsonParser.Decode(&dash.Data); err != nil {
  63. return nil, err
  64. }
  65. return dash, nil
  66. }
  67. func (dash *Dashboard) GetString(prop string) string {
  68. return dash.Data[prop].(string)
  69. }
  70. func (dash *Dashboard) UpdateSlug() {
  71. title := strings.ToLower(dash.Data["title"].(string))
  72. re := regexp.MustCompile("[^\\w ]+")
  73. re2 := regexp.MustCompile("\\s")
  74. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  75. }
  76. func (account *UserAccount) AddCollaborator(accountId int) {
  77. account.Collaborators = append(account.Collaborators, CollaboratorLink{
  78. AccountId: accountId,
  79. Role: "admin",
  80. CreatedOn: time.Now(),
  81. ModifiedOn: time.Now(),
  82. })
  83. }