mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Progress on account and dashboard save/load
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/Unknwon/macaron"
|
||||
"github.com/macaron-contrib/session"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/routes/apimodel"
|
||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||
)
|
||||
|
||||
func GetDashboard(c *middleware.Context) {
|
||||
@@ -88,5 +87,5 @@ func PostDashboard(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"status": "success", "slug": dashboard.Slug})
|
||||
c.JSON(200, utils.DynMap{"status": "success", "slug": dashboard.Slug})
|
||||
}
|
||||
|
38
pkg/routes/api/api_register.go
Normal file
38
pkg/routes/api/api_register.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/torkelo/grafana-pro/pkg/log"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||
)
|
||||
|
||||
type registerAccountJsonModel struct {
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Password2 bool `json:"remember2"`
|
||||
}
|
||||
|
||||
func CreateAccount(c *middleware.Context) {
|
||||
var registerModel registerAccountJsonModel
|
||||
|
||||
if !c.JsonBody(®isterModel) {
|
||||
c.JSON(400, utils.DynMap{"status": "bad request"})
|
||||
return
|
||||
}
|
||||
|
||||
account := models.Account{
|
||||
Login: registerModel.Email,
|
||||
Email: registerModel.Email,
|
||||
Password: registerModel.Password,
|
||||
}
|
||||
|
||||
err := models.CreateAccount(&account)
|
||||
if err != nil {
|
||||
log.Error(2, "Failed to create user account, email: %v, error: %v", registerModel.Email, err)
|
||||
c.JSON(500, utils.DynMap{"status": "failed to create account"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, utils.DynMap{"status": "ok"})
|
||||
}
|
@@ -20,6 +20,10 @@ func Register(m *macaron.Macaron) {
|
||||
m.Get("/login", Index)
|
||||
m.Get("/login/:name", login.OAuthLogin)
|
||||
|
||||
// user register
|
||||
m.Get("/register/*_", Index)
|
||||
m.Post("/api/account", api.CreateAccount)
|
||||
|
||||
// dashboards
|
||||
m.Get("/dashboard/*", auth, Index)
|
||||
m.Get("/api/dashboards/:slug", auth, api.GetDashboard)
|
||||
|
@@ -53,7 +53,7 @@ func OAuthLogin(ctx *middleware.Context) {
|
||||
// create account if missing
|
||||
if err == models.ErrAccountNotFound {
|
||||
account = &models.Account{
|
||||
Login: userInfo.Login,
|
||||
Login: userInfo.Email,
|
||||
Email: userInfo.Email,
|
||||
Name: userInfo.Name,
|
||||
Company: userInfo.Company,
|
||||
|
@@ -35,6 +35,7 @@ func Init() {
|
||||
models.GetDashboard = GetDashboard
|
||||
models.SaveDashboard = SaveDashboard
|
||||
models.SearchQuery = SearchQuery
|
||||
models.DeleteDashboard = DeleteDashboard
|
||||
}
|
||||
|
||||
func LoadModelsConfig() {
|
||||
|
@@ -27,8 +27,8 @@ func CreateAccount(account *models.Account) error {
|
||||
func GetAccount(id int64) (*models.Account, error) {
|
||||
var err error
|
||||
|
||||
account := &models.Account{Id: id}
|
||||
has, err := x.Get(account)
|
||||
var account models.Account
|
||||
has, err := x.Id(id).Get(&account)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -36,7 +36,11 @@ func GetAccount(id int64) (*models.Account, error) {
|
||||
return nil, models.ErrAccountNotFound
|
||||
}
|
||||
|
||||
return account, nil
|
||||
if account.UsingAccountId == 0 {
|
||||
account.UsingAccountId = account.Id
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func GetAccountByLogin(emailOrLogin string) (*models.Account, error) {
|
||||
|
@@ -47,8 +47,18 @@ func SearchQuery(query string, accountId int64) ([]*models.SearchResult, error)
|
||||
sess := x.Limit(100, 0).Where("account_id=?", accountId)
|
||||
sess.Table("Dashboard")
|
||||
|
||||
var results []*models.SearchResult
|
||||
results := make([]*models.SearchResult, 0)
|
||||
err := sess.Find(&results)
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
func DeleteDashboard(slug string, accountId int64) error {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
|
||||
_, err := sess.Exec(rawSql, accountId, slug)
|
||||
|
||||
return err
|
||||
}
|
||||
|
3
pkg/utils/json.go
Normal file
3
pkg/utils/json.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package utils
|
||||
|
||||
type DynMap map[string]interface{}
|
Reference in New Issue
Block a user