dashboards.go 1.3 KB

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