Commit 607b0c0c by Torkel Ödegaard

More refactoring and aligning code to the command query model

parent d5a59ac6
......@@ -4,9 +4,10 @@ import (
"errors"
"fmt"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/models"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
"github.com/torkelo/grafana-pro/pkg/social"
)
......@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) {
log.Info("login.OAuthLogin(social login): %s", userInfo)
account, err := models.GetAccountByLogin(userInfo.Email)
account, err := m.GetAccountByLogin(userInfo.Email)
// create account if missing
if err == models.ErrAccountNotFound {
account = &models.Account{
if err == m.ErrAccountNotFound {
cmd := &m.CreateAccountCommand{
Login: userInfo.Email,
Email: userInfo.Email,
Name: userInfo.Name,
Company: userInfo.Company,
}
if err = models.SaveAccount(account); err != nil {
if err = bus.Dispatch(&cmd); err != nil {
ctx.Handle(500, "Failed to create account", err)
return
}
account = &cmd.Result
} else if err != nil {
ctx.Handle(500, "Unexpected error", err)
}
......
package api
import (
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/utils"
m "github.com/torkelo/grafana-pro/pkg/models"
)
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
var cmd m.CreateAccountCommand
if !c.JsonBody(&registerModel) {
c.JSON(400, utils.DynMap{"status": "bad request"})
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation error", nil)
return
}
account := models.Account{
Login: registerModel.Email,
Email: registerModel.Email,
Password: registerModel.Password,
}
cmd.Login = cmd.Email
err := bus.Dispatch(&cmd)
err := models.SaveAccount(&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"})
c.JsonApiErr(500, "failed to create account", err)
return
}
c.JSON(200, utils.DynMap{"status": "ok"})
c.JsonOK("Account created")
}
......@@ -6,7 +6,6 @@ import (
)
var (
SaveAccount func(account *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error)
)
......@@ -55,6 +54,16 @@ type AccountDTO struct {
Collaborators []*CollaboratorDTO `json:"collaborators"`
}
type CreateAccountCommand struct {
Email string `json:"email" binding:"required"`
Login string `json:"login"`
Password string `json:"password" binding:"required"`
Name string `json:"name"`
Company string `json:"company"`
Result Account `json:"-"`
}
// returns a view projection
type GetAccountInfoQuery struct {
Id int64
......
......@@ -13,6 +13,24 @@ func init() {
bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", AddCollaborator)
bus.AddHandler("sql", GetOtherAccounts)
bus.AddHandler("sql", CreateAccount)
}
func CreateAccount(cmd *m.CreateAccountCommand) error {
return inTransaction(func(sess *xorm.Session) error {
account := m.Account{
Email: cmd.Email,
Login: cmd.Login,
Password: cmd.Password,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(&account)
cmd.Result = account
return err
})
}
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
......@@ -55,32 +73,6 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
})
}
func SaveAccount(account *m.Account) error {
var err error
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if account.Id == 0 {
_, err = sess.Insert(account)
} else {
_, err = sess.Id(account.Id).Update(account)
}
if err != nil {
sess.Rollback()
return err
} else if err = sess.Commit(); err != nil {
return err
}
return nil
}
func GetAccount(id int64) (*m.Account, error) {
var err error
......
......@@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) {
InitTestDB(t)
Convey("Given two saved accounts", func() {
ac1 := m.Account{
Login: "ac1",
Email: "ac1@test.com",
Name: "ac1_name",
}
ac2 := m.Account{
Login: "ac2",
Email: "ac2@test.com",
Name: "ac2_name",
}
ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com"}
ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com"}
err := SaveAccount(&ac1)
err = SaveAccount(&ac2)
err := CreateAccount(&ac1cmd)
err = CreateAccount(&ac2cmd)
So(err, ShouldBeNil)
ac1 := ac1cmd.Result
ac2 := ac2cmd.Result
Convey("Should be able to read account info projection", func() {
query := m.GetAccountInfoQuery{Id: ac1.Id}
err = GetAccountInfo(&query)
So(err, ShouldBeNil)
So(query.Result.Name, ShouldEqual, "ac1_name")
So(query.Result.Email, ShouldEqual, "ac1@test.com")
})
Convey("Can add collaborator", func() {
......
......@@ -35,7 +35,6 @@ func init() {
}
func Init() {
m.SaveAccount = SaveAccount
m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin
m.GetDashboard = GetDashboard
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment