dashboards.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package models
  2. import (
  3. "encoding/json"
  4. "io"
  5. "regexp"
  6. "strings"
  7. "time"
  8. )
  9. var (
  10. GetDashboard func(slug string, accountId int) (*Dashboard, error)
  11. SaveDashboard func(dash *Dashboard) error
  12. DeleteDashboard func(slug string, accountId int) error
  13. SearchQuery func(query string, acccountId int) ([]*SearchResult, error)
  14. )
  15. type Dashboard struct {
  16. Id string `gorethink:"id,omitempty"`
  17. Slug string
  18. AccountId int
  19. LastModifiedByUserId string
  20. LastModifiedByDate time.Time
  21. CreatedDate time.Time
  22. Title string
  23. Tags []string
  24. Data map[string]interface{}
  25. }
  26. type SearchResult struct {
  27. Id string `json:"id"`
  28. Title string `json:"title"`
  29. Slug string `json:"slug"`
  30. }
  31. func NewDashboard(title string) *Dashboard {
  32. dash := &Dashboard{}
  33. dash.Id = ""
  34. dash.LastModifiedByDate = time.Now()
  35. dash.CreatedDate = time.Now()
  36. dash.LastModifiedByUserId = "123"
  37. dash.Data = make(map[string]interface{})
  38. dash.Data["title"] = title
  39. dash.Title = title
  40. dash.UpdateSlug()
  41. return dash
  42. }
  43. func NewFromJson(reader io.Reader) (*Dashboard, error) {
  44. dash := NewDashboard("temp")
  45. jsonParser := json.NewDecoder(reader)
  46. if err := jsonParser.Decode(&dash.Data); err != nil {
  47. return nil, err
  48. }
  49. return dash, nil
  50. }
  51. func (dash *Dashboard) GetString(prop string) string {
  52. return dash.Data[prop].(string)
  53. }
  54. func (dash *Dashboard) UpdateSlug() {
  55. title := strings.ToLower(dash.Data["title"].(string))
  56. re := regexp.MustCompile("[^\\w ]+")
  57. re2 := regexp.MustCompile("\\s")
  58. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  59. }