mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package stores
|
|
|
|
import (
|
|
"errors"
|
|
|
|
r "github.com/dancannon/gorethink"
|
|
"github.com/torkelo/grafana-pro/pkg/models"
|
|
)
|
|
|
|
func (self *rethinkStore) getNextAccountId() (int, error) {
|
|
resp, err := r.Table("master").Get("ids").Update(map[string]interface{}{
|
|
"NextAccountId": r.Row.Field("NextAccountId").Add(1),
|
|
}, r.UpdateOpts{ReturnVals: true}).RunWrite(self.session)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if resp.NewValue == nil {
|
|
return 0, errors.New("Failed to get new value after incrementing account id")
|
|
}
|
|
|
|
return int(resp.NewValue.(map[string]interface{})["NextAccountId"].(float64)), nil
|
|
}
|
|
|
|
func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error {
|
|
accountId, err := self.getNextAccountId()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
account.DatabaseId = accountId
|
|
|
|
resp, err := r.Table("accounts").Insert(account).RunWrite(self.session)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.Inserted == 0 {
|
|
return errors.New("Failed to insert acccount")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserAccount, error) {
|
|
resp, err := r.Table("accounts").GetAllByIndex("AccountLogin", []interface{}{emailOrName}).Run(self.session)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var account models.UserAccount
|
|
err = resp.One(&account)
|
|
if err != nil {
|
|
return nil, errors.New("Not found")
|
|
}
|
|
|
|
return &account, nil
|
|
}
|
|
|
|
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
|
|
resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
|
|
"NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
|
|
}, r.UpdateOpts{ReturnVals: true}).RunWrite(self.session)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if resp.NewValue == nil {
|
|
return 0, errors.New("Failed to get next dashboard id, no new value after update")
|
|
}
|
|
|
|
return int(resp.NewValue.(map[string]interface{})["NextDashboardId"].(float64)), nil
|
|
}
|