diff --git a/pkg/api/datasources.go b/pkg/api/datasources.go index b78aa5320eb..37a1cedbfae 100644 --- a/pkg/api/datasources.go +++ b/pkg/api/datasources.go @@ -64,12 +64,13 @@ func AddDataSource(c *middleware.Context) { cmd.AccountId = c.Account.Id - err := bus.Dispatch(&cmd) - if err != nil { + if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "Failed to add datasource", err) return } + //bus.Publish(&m.DataSourceCreatedEvent{Account: c.GetAccountId(), }) + c.JsonOK("Datasource added") } diff --git a/pkg/models/account.go b/pkg/models/account.go index 9c95efe8739..4d06a70d41c 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -6,49 +6,31 @@ import ( ) // Typed errors + var ( ErrAccountNotFound = errors.New("Account not found") ) type Account struct { - Id int64 - Login string `xorm:"UNIQUE NOT NULL"` - Email string `xorm:"UNIQUE NOT NULL"` - Name string - FullName string - Password string - IsAdmin bool - Rands string `xorm:"VARCHAR(10)"` - Salt string `xorm:"VARCHAR(10)"` - Company string - NextDashboardId int - UsingAccountId int64 + Id int64 + Login string `xorm:"UNIQUE NOT NULL"` + Email string `xorm:"UNIQUE NOT NULL"` + Name string + FullName string + Password string + IsAdmin bool + Salt string `xorm:"VARCHAR(10)"` + Company string + NextDashboardId int + UsingAccountId int64 + DefaultDataSourceId int64 Created time.Time Updated time.Time } -// api projection -type OtherAccountDTO struct { - Id int64 `json:"id"` - Email string `json:"email"` - Role string `json:"role"` - IsUsing bool `json:"isUsing"` -} - -// api projection model -type CollaboratorDTO struct { - AccountId int64 `json:"accountId"` - Email string `json:"email"` - Role string `json:"role"` -} - -// api view projection -type AccountDTO struct { - Email string `json:"email"` - Name string `json:"name"` - Collaborators []*CollaboratorDTO `json:"collaborators"` -} +// --------------------- +// COMMANDS type CreateAccountCommand struct { Email string `json:"email" binding:"required"` @@ -66,13 +48,19 @@ type SetUsingAccountCommand struct { UsingAccountId int64 } -// returns a view projection +type SetDefaultDataSourceCommand struct { + AccountId int64 + DataSourceId int64 +} + +// ---------------------- +// QUERIES + type GetAccountInfoQuery struct { Id int64 Result AccountDTO } -// returns a view projection type GetOtherAccountsQuery struct { AccountId int64 Result []*OtherAccountDTO @@ -87,3 +75,25 @@ type GetAccountByLoginQuery struct { Login string Result *Account } + +// ------------------------ +// DTO & Projections + +type OtherAccountDTO struct { + Id int64 `json:"id"` + Email string `json:"email"` + Role string `json:"role"` + IsUsing bool `json:"isUsing"` +} + +type CollaboratorDTO struct { + AccountId int64 `json:"accountId"` + Email string `json:"email"` + Role string `json:"role"` +} + +type AccountDTO struct { + Email string `json:"email"` + Name string `json:"name"` + Collaborators []*CollaboratorDTO `json:"collaborators"` +} diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 481df529277..f355b5dec9b 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -25,55 +25,6 @@ type Dashboard struct { Data map[string]interface{} } -type SearchResult struct { - Dashboards []*DashboardSearchHit `json:"dashboards"` - Tags []*DashboardTagCloudItem `json:"tags"` - TagsOnly bool `json:"tagsOnly"` -} - -type DashboardSearchHit struct { - Title string `json:"title"` - Slug string `json:"slug"` - Tags []string `json:"tags"` -} - -type DashboardTagCloudItem struct { - Term string `json:"term"` - Count int `json:"count"` -} - -type SearchDashboardsQuery struct { - Title string - Tag string - AccountId int64 - - Result []*DashboardSearchHit -} - -type GetDashboardTagsQuery struct { - AccountId int64 - Result []*DashboardTagCloudItem -} - -type SaveDashboardCommand struct { - Dashboard map[string]interface{} `json:"dashboard"` - AccountId int64 `json:"-"` - - Result *Dashboard -} - -type DeleteDashboardCommand struct { - Slug string - AccountId int64 -} - -type GetDashboardQuery struct { - Slug string - AccountId int64 - - Result *Dashboard -} - func NewDashboard(title string) *Dashboard { dash := &Dashboard{} dash.Data = make(map[string]interface{}) @@ -121,3 +72,30 @@ func (dash *Dashboard) UpdateSlug() { re2 := regexp.MustCompile("\\s") dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-") } + +// +// COMMANDS +// + +type SaveDashboardCommand struct { + Dashboard map[string]interface{} `json:"dashboard"` + AccountId int64 `json:"-"` + + Result *Dashboard +} + +type DeleteDashboardCommand struct { + Slug string + AccountId int64 +} + +// +// QUERIES +// + +type GetDashboardQuery struct { + Slug string + AccountId int64 + + Result *Dashboard +} diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index b81312b9b6b..12ff5bbadc0 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -38,16 +38,8 @@ type DataSource struct { Updated time.Time } -type GetDataSourcesQuery struct { - AccountId int64 - Result []*DataSource -} - -type GetDataSourceByIdQuery struct { - Id int64 - AccountId int64 - Result DataSource -} +// ---------------------- +// COMMANDS type AddDataSourceCommand struct { AccountId int64 @@ -58,6 +50,8 @@ type AddDataSourceCommand struct { Password string Database string User string + + Result *DataSource } type UpdateDataSourceCommand struct { @@ -76,3 +70,22 @@ type DeleteDataSourceCommand struct { Id int64 AccountId int64 } + +// --------------------- +// QUERIES + +type GetDataSourcesQuery struct { + AccountId int64 + Result []*DataSource +} + +type GetDataSourceByIdQuery struct { + Id int64 + AccountId int64 + Result DataSource +} + +// --------------------- +// EVENTS +type DataSourceCreatedEvent struct { +} diff --git a/pkg/models/search.go b/pkg/models/search.go new file mode 100644 index 00000000000..e717ddb73ef --- /dev/null +++ b/pkg/models/search.go @@ -0,0 +1,31 @@ +package models + +type SearchResult struct { + Dashboards []*DashboardSearchHit `json:"dashboards"` + Tags []*DashboardTagCloudItem `json:"tags"` + TagsOnly bool `json:"tagsOnly"` +} + +type DashboardSearchHit struct { + Title string `json:"title"` + Slug string `json:"slug"` + Tags []string `json:"tags"` +} + +type DashboardTagCloudItem struct { + Term string `json:"term"` + Count int `json:"count"` +} + +type SearchDashboardsQuery struct { + Title string + Tag string + AccountId int64 + + Result []*DashboardSearchHit +} + +type GetDashboardTagsQuery struct { + AccountId int64 + Result []*DashboardTagCloudItem +} diff --git a/pkg/services/account/handlers.go b/pkg/services/account/handlers.go new file mode 100644 index 00000000000..07857f5e642 --- /dev/null +++ b/pkg/services/account/handlers.go @@ -0,0 +1,9 @@ +package account + +import ( + "github.com/torkelo/grafana-pro/pkg/bus" +) + +func InitAccountService() { + bus.ListenTo() +} diff --git a/pkg/stores/sqlstore/datasource.go b/pkg/stores/sqlstore/datasource.go index 74488adece0..95986126297 100644 --- a/pkg/stores/sqlstore/datasource.go +++ b/pkg/stores/sqlstore/datasource.go @@ -60,7 +60,8 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error { Updated: time.Now(), } - _, err = sess.Insert(ds) + _, err = sess.Insert(&ds) + cmd.Result = &ds return err })