dashboards.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 UserContext struct {
  21. UserId string
  22. AccountId string
  23. }
  24. type SearchResult struct {
  25. Id string `json:"id"`
  26. Title string `json:"title"`
  27. Slug string `json:"slug"`
  28. }
  29. func NewDashboard(title string) *Dashboard {
  30. dash := &Dashboard{}
  31. dash.Id = ""
  32. dash.LastModifiedByDate = time.Now()
  33. dash.CreatedDate = time.Now()
  34. dash.LastModifiedByUserId = "123"
  35. dash.Data = make(map[string]interface{})
  36. dash.Data["title"] = title
  37. dash.Title = title
  38. dash.UpdateSlug()
  39. return dash
  40. }
  41. func NewFromJson(reader io.Reader) (*Dashboard, error) {
  42. dash := NewDashboard("temp")
  43. jsonParser := json.NewDecoder(reader)
  44. if err := jsonParser.Decode(&dash.Data); err != nil {
  45. return nil, err
  46. }
  47. return dash, nil
  48. }
  49. func (dash *Dashboard) GetString(prop string) string {
  50. return dash.Data[prop].(string)
  51. }
  52. func (dash *Dashboard) UpdateSlug() {
  53. title := strings.ToLower(dash.Data["title"].(string))
  54. re := regexp.MustCompile("[^\\w ]+")
  55. re2 := regexp.MustCompile("\\s")
  56. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  57. }