Skip to content

Commit

Permalink
Merge pull request #6 from tonybka/feature/duy/domain-event
Browse files Browse the repository at this point in the history
Entity, Aggregate Root and Domain Event
  • Loading branch information
tonybka authored Jan 11, 2023
2 parents 7516744 + 9da79fc commit 26250d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
20 changes: 20 additions & 0 deletions domain/entity/base_aggregate_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@ import "github.com/tonybka/go-base-ddd/domain/event"

type BaseAggregateRoot struct {
BaseEntity

domainEvents []event.IBaseDomainEvent
}

func NewBaseAggregateRoot() (BaseAggregateRoot, error) {
base, err := NewBaseEntity()
if err != nil {
return BaseAggregateRoot{}, nil
}

events := make([]event.IBaseDomainEvent, 0)

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

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

func (aggregate *BaseAggregateRoot) PendingEvents() []event.IBaseDomainEvent {
return aggregate.domainEvents
}
10 changes: 10 additions & 0 deletions domain/entity/base_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ import "github.com/google/uuid"
type BaseEntity struct {
ID uuid.UUID
}

func NewBaseEntity() (BaseEntity, error) {
id, err := uuid.NewUUID()

if err != nil {
return BaseEntity{}, err
}

return BaseEntity{ID: id}, nil
}
6 changes: 5 additions & 1 deletion domain/event/base_domain_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package event

type IBaseDomainEvent struct {
import "time"

type IBaseDomainEvent interface {
EventName() string
OccurredAt() time.Time
}

0 comments on commit 26250d1

Please sign in to comment.