2014-08-08 05:35:15 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-11-20 05:11:07 -06:00
|
|
|
"errors"
|
2014-08-22 08:32:42 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-08-21 15:09:48 -05:00
|
|
|
"time"
|
2014-08-08 05:35:15 -05:00
|
|
|
)
|
|
|
|
|
2014-11-20 05:11:07 -06:00
|
|
|
// Typed errors
|
|
|
|
var (
|
2015-01-05 10:04:29 -06:00
|
|
|
ErrDashboardNotFound = errors.New("Account not found")
|
|
|
|
ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
|
2014-10-06 14:31:54 -05:00
|
|
|
)
|
|
|
|
|
2014-08-08 05:35:15 -05:00
|
|
|
type Dashboard struct {
|
2015-02-23 13:07:49 -06:00
|
|
|
Id int64
|
|
|
|
Slug string
|
|
|
|
OrgId int64
|
|
|
|
Version int
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
2014-08-21 15:09:48 -05:00
|
|
|
|
|
|
|
Title string
|
|
|
|
Data map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2014-08-08 05:35:15 -05:00
|
|
|
func NewDashboard(title string) *Dashboard {
|
|
|
|
dash := &Dashboard{}
|
|
|
|
dash.Data = make(map[string]interface{})
|
|
|
|
dash.Data["title"] = title
|
2014-08-22 08:32:42 -05:00
|
|
|
dash.Title = title
|
|
|
|
dash.UpdateSlug()
|
2014-08-08 05:35:15 -05:00
|
|
|
return dash
|
|
|
|
}
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
func (dash *Dashboard) GetTags() []string {
|
|
|
|
jsonTags := dash.Data["tags"]
|
|
|
|
if jsonTags == nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := jsonTags.([]interface{})
|
|
|
|
b := make([]string, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
b[i] = arr[i].(string)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
|
|
|
dash := &Dashboard{}
|
|
|
|
dash.Data = cmd.Dashboard
|
|
|
|
dash.Title = dash.Data["title"].(string)
|
2015-02-23 13:07:49 -06:00
|
|
|
dash.OrgId = cmd.OrgId
|
2014-12-22 05:25:08 -06:00
|
|
|
dash.UpdateSlug()
|
2014-08-08 05:35:15 -05:00
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if dash.Data["id"] != nil {
|
|
|
|
dash.Id = int64(dash.Data["id"].(float64))
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
return dash
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dash *Dashboard) GetString(prop string) string {
|
|
|
|
return dash.Data[prop].(string)
|
|
|
|
}
|
2014-08-22 08:32:42 -05:00
|
|
|
|
|
|
|
func (dash *Dashboard) UpdateSlug() {
|
|
|
|
title := strings.ToLower(dash.Data["title"].(string))
|
|
|
|
re := regexp.MustCompile("[^\\w ]+")
|
|
|
|
re2 := regexp.MustCompile("\\s")
|
|
|
|
dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
|
|
|
|
}
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// COMMANDS
|
|
|
|
//
|
|
|
|
|
|
|
|
type SaveDashboardCommand struct {
|
|
|
|
Dashboard map[string]interface{} `json:"dashboard"`
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64 `json:"-"`
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteDashboardCommand struct {
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug string
|
|
|
|
OrgId int64
|
2015-01-09 04:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// QUERIES
|
|
|
|
//
|
|
|
|
|
|
|
|
type GetDashboardQuery struct {
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug string
|
|
|
|
OrgId int64
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
}
|