More refactoring and aligning code to the command query model

This commit is contained in:
Torkel Ödegaard
2014-12-19 11:53:27 +01:00
parent d5a59ac6b7
commit 607b0c0c0e
6 changed files with 53 additions and 67 deletions
+18 -26
View File
@@ -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
+8 -13
View File
@@ -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() {
-1
View File
@@ -35,7 +35,6 @@ func init() {
}
func Init() {
m.SaveAccount = SaveAccount
m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin
m.GetDashboard = GetDashboard