call transaction Dao events only after commit, added totalPages to the search response and updated the tests
This commit is contained in:
24
daos/base.go
24
daos/base.go
@@ -65,12 +65,11 @@ func (dao *Dao) RunInTransaction(fn func(txDao *Dao) error) error {
|
||||
// so execute the function within the current transaction
|
||||
return fn(dao)
|
||||
case *dbx.DB:
|
||||
afterCalls := []afterCallGroup{}
|
||||
|
||||
return txOrDB.Transactional(func(tx *dbx.Tx) error {
|
||||
txError := txOrDB.Transactional(func(tx *dbx.Tx) error {
|
||||
txDao := New(tx)
|
||||
|
||||
afterCalls := []afterCallGroup{}
|
||||
|
||||
if dao.BeforeCreateFunc != nil {
|
||||
txDao.BeforeCreateFunc = func(eventDao *Dao, m models.Model) error {
|
||||
return dao.BeforeCreateFunc(eventDao, m)
|
||||
@@ -103,23 +102,24 @@ func (dao *Dao) RunInTransaction(fn func(txDao *Dao) error) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := fn(txDao); err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(txDao)
|
||||
})
|
||||
|
||||
// execute after event calls on successfull transaction
|
||||
if txError == nil {
|
||||
// execute after event calls on successful transaction
|
||||
for _, call := range afterCalls {
|
||||
if call.Action == "create" {
|
||||
switch call.Action {
|
||||
case "create":
|
||||
dao.AfterCreateFunc(call.EventDao, call.Model)
|
||||
} else if call.Action == "update" {
|
||||
case "update":
|
||||
dao.AfterUpdateFunc(call.EventDao, call.Model)
|
||||
} else if call.Action == "delete" {
|
||||
case "delete":
|
||||
dao.AfterDeleteFunc(call.EventDao, call.Model)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return txError
|
||||
}
|
||||
|
||||
return errors.New("Failed to start transaction (unknown dao.db)")
|
||||
|
||||
Reference in New Issue
Block a user