Skip to content

Commit

Permalink
Merge pull request #12 from tonybka/enhancement/tonybka/domain-event-…
Browse files Browse the repository at this point in the history
…in-aggregate

AddEvent is only available in Aggregate Root
  • Loading branch information
tonybka authored Apr 14, 2023
2 parents 72f8b96 + acc0141 commit 02a1134
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
14 changes: 3 additions & 11 deletions domain/entity/base_aggregate_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ import "github.com/tonybka/go-base-ddd/domain/event"

type BaseAggregateRoot struct {
BaseEntity

domainEvents []event.IBaseDomainEvent
}

func NewBaseAggregateRoot(rootEntityId uint) BaseAggregateRoot {
base := NewBaseEntity(rootEntityId)
events := make([]event.IBaseDomainEvent, 0)

return BaseAggregateRoot{
BaseEntity: base,
domainEvents: events,
BaseEntity: base,
}
}

func (aggregate *BaseAggregateRoot) AddEvent(event event.IBaseDomainEvent) {
aggregate.domainEvents = append(aggregate.domainEvents, event)
}

func (aggregate *BaseAggregateRoot) PendingEvents() []event.IBaseDomainEvent {
return aggregate.domainEvents
func (base *BaseAggregateRoot) AddEvent(tableName string, domainEvent event.IBaseDomainEvent) {
event.EventSource.AddEvent(tableName, domainEvent)
}
6 changes: 0 additions & 6 deletions domain/entity/base_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package entity

import (
"time"

"github.com/tonybka/go-base-ddd/domain/event"
)

type BaseEntity struct {
Expand All @@ -15,7 +13,3 @@ type BaseEntity struct {
func NewBaseEntity(id uint) BaseEntity {
return BaseEntity{ID: id, CreatedAt: time.Now()}
}

func (base *BaseEntity) AddEvent(tableName string, domainEvent event.IBaseDomainEvent) {
event.EventSource.AddEvent(tableName, domainEvent)
}
2 changes: 1 addition & 1 deletion samples/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const DBTblNameAccounts = "accounts"

type Account struct {
entity.BaseEntity
entity.BaseAggregateRoot
AccountName string
}

Expand Down
8 changes: 4 additions & 4 deletions samples/account_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (ts *AccountRepositoryTestSuite) TestCreateAccount() {
randId := rand.Intn(99999)

account := Account{
BaseEntity: entity.NewBaseEntity(uint(randId)),
AccountName: tests.RandomString(),
BaseAggregateRoot: entity.NewBaseAggregateRoot(uint(randId)),
AccountName: tests.RandomString(),
}

result, err := ts.accountRepo.Create(account)
Expand Down Expand Up @@ -119,8 +119,8 @@ func (ts *AccountRepositoryTestSuite) TestAccountWithEvent() {
randId := rand.Intn(99999)

account := Account{
BaseEntity: entity.NewBaseEntity(uint(randId)),
AccountName: tests.RandomString(),
BaseAggregateRoot: entity.NewBaseAggregateRoot(uint(randId)),
AccountName: tests.RandomString(),
}

account.AddEvent(DBTblNameAccounts, NewAccountCreatedEvent(uint(randId), nil))
Expand Down

0 comments on commit 02a1134

Please sign in to comment.