Commit 607b0c0c by Torkel Ödegaard

More refactoring and aligning code to the command query model

parent d5a59ac6
...@@ -4,9 +4,10 @@ import ( ...@@ -4,9 +4,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/log" "github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware" "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/setting"
"github.com/torkelo/grafana-pro/pkg/social" "github.com/torkelo/grafana-pro/pkg/social"
) )
...@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) { ...@@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) {
log.Info("login.OAuthLogin(social login): %s", userInfo) log.Info("login.OAuthLogin(social login): %s", userInfo)
account, err := models.GetAccountByLogin(userInfo.Email) account, err := m.GetAccountByLogin(userInfo.Email)
// create account if missing // create account if missing
if err == models.ErrAccountNotFound { if err == m.ErrAccountNotFound {
account = &models.Account{ cmd := &m.CreateAccountCommand{
Login: userInfo.Email, Login: userInfo.Email,
Email: userInfo.Email, Email: userInfo.Email,
Name: userInfo.Name, Name: userInfo.Name,
Company: userInfo.Company, 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) ctx.Handle(500, "Failed to create account", err)
return return
} }
account = &cmd.Result
} else if err != nil { } else if err != nil {
ctx.Handle(500, "Unexpected error", err) ctx.Handle(500, "Unexpected error", err)
} }
......
package api package api
import ( 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/middleware"
"github.com/torkelo/grafana-pro/pkg/models" m "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) { func CreateAccount(c *middleware.Context) {
var registerModel registerAccountJsonModel var cmd m.CreateAccountCommand
if !c.JsonBody(&registerModel) { if !c.JsonBody(&cmd) {
c.JSON(400, utils.DynMap{"status": "bad request"}) c.JsonApiErr(400, "Validation error", nil)
return return
} }
account := models.Account{ cmd.Login = cmd.Email
Login: registerModel.Email, err := bus.Dispatch(&cmd)
Email: registerModel.Email,
Password: registerModel.Password,
}
err := models.SaveAccount(&account)
if err != nil { if err != nil {
log.Error(2, "Failed to create user account, email: %v, error: %v", registerModel.Email, err) c.JsonApiErr(500, "failed to create account", err)
c.JSON(500, utils.DynMap{"status": "failed to create account"})
return return
} }
c.JSON(200, utils.DynMap{"status": "ok"}) c.JsonOK("Account created")
} }
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
) )
var ( var (
SaveAccount func(account *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error) GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error) GetAccount func(accountId int64) (*Account, error)
) )
...@@ -55,6 +54,16 @@ type AccountDTO struct { ...@@ -55,6 +54,16 @@ type AccountDTO struct {
Collaborators []*CollaboratorDTO `json:"collaborators"` 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 // returns a view projection
type GetAccountInfoQuery struct { type GetAccountInfoQuery struct {
Id int64 Id int64
......
...@@ -13,6 +13,24 @@ func init() { ...@@ -13,6 +13,24 @@ func init() {
bus.AddHandler("sql", GetAccountInfo) bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", AddCollaborator) bus.AddHandler("sql", AddCollaborator)
bus.AddHandler("sql", GetOtherAccounts) 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 { func GetAccountInfo(query *m.GetAccountInfoQuery) error {
...@@ -55,32 +73,6 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) 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) { func GetAccount(id int64) (*m.Account, error) {
var err error var err error
......
...@@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) {
InitTestDB(t) InitTestDB(t)
Convey("Given two saved accounts", func() { Convey("Given two saved accounts", func() {
ac1 := m.Account{ ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com"}
Login: "ac1", ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com"}
Email: "ac1@test.com",
Name: "ac1_name", err := CreateAccount(&ac1cmd)
} err = CreateAccount(&ac2cmd)
ac2 := m.Account{
Login: "ac2",
Email: "ac2@test.com",
Name: "ac2_name",
}
err := SaveAccount(&ac1)
err = SaveAccount(&ac2)
So(err, ShouldBeNil) So(err, ShouldBeNil)
ac1 := ac1cmd.Result
ac2 := ac2cmd.Result
Convey("Should be able to read account info projection", func() { Convey("Should be able to read account info projection", func() {
query := m.GetAccountInfoQuery{Id: ac1.Id} query := m.GetAccountInfoQuery{Id: ac1.Id}
err = GetAccountInfo(&query) err = GetAccountInfo(&query)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(query.Result.Name, ShouldEqual, "ac1_name") So(query.Result.Email, ShouldEqual, "ac1@test.com")
}) })
Convey("Can add collaborator", func() { Convey("Can add collaborator", func() {
......
...@@ -35,7 +35,6 @@ func init() { ...@@ -35,7 +35,6 @@ func init() {
} }
func Init() { func Init() {
m.SaveAccount = SaveAccount
m.GetAccount = GetAccount m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin m.GetAccountByLogin = GetAccountByLogin
m.GetDashboard = GetDashboard 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